すのはら荘春原庄的雪

Codeforces Round #779 (Div. 2) A-D1题解

Toretto·2022-03-29 20:55·100 次阅读

Codeforces Round #779 (Div. 2) A-D1题解

A. Marin and Photoshoot

Today, Marin is at a cosplay exhibition and is preparing for a group photoshoot!

For the group picture, the cosplayers form a horizontal line. A group picture is considered beautiful if for every contiguous segment of at least 22 cosplayers, the number of males does not exceed the number of females (obviously).

Currently, the line has nn cosplayers which can be described by a binary string ss. The ii-th cosplayer is male if si=0si=0 and female if si=1si=1. To ensure that the line is beautiful, you can invite some additional cosplayers (possibly zero) to join the line at any position. You can't remove any cosplayer from the line.

Marin wants to know the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful. She can't do this on her own, so she's asking you for help. Can you help her?

Input

The first line contains a single integer tt (1t1031≤t≤103) — the number of test cases.

The first line of each test case contains a positive integer nn (1n1001≤n≤100) — the number of cosplayers in the initial line.

The second line of each test case contains a binary string ss of length nn — describing the cosplayers already in line. Each character of the string is either 0 describing a male, or 1 describing a female.

Note that there is no limit on the sum of nn.

Output

For each test case, print the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful.


题目大意:在‘01‘串中添加最小数量个‘1’使其任意长度不小于2的子串中‘1’的数量大于等于2。
思路:只需保证两个‘0’之间最少有两个‘1’即可。值得注意的是当n等于1时不论是有一个男性或者一个女性都输出0即可。(当时因为判断只有一个男性输出1而wa了3次)。

AC代码:

复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int t;
 4 int main()
 5 {
 6     std::ios::sync_with_stdio(false);
 7     cin>>t;
 8     while(t--){
 9         int n;
10         cin>>n;
11         string s;
12         cin>>s;
13         if(n==1){
14             cout<<"0"<<endl;
15             continue;
16         }
17         int z=0;
18         for(int i=0;i<n;i++){
19             if(s[i]=='0')
20                 z++;
21         }
22         if(z==0||z==1&&n>=2){
23             cout<<"0"<<endl;
24             continue;
25         }
26         int ans=0;
27         for(int i=0;i<n;i++){
28             if(s[i]=='0')
29             {
30                 for(int j=i+1;j<n;j++){
31                     if(s[j]=='0'){
32                         if(j-i==1)
33                             ans+=2;
34                         else if(j-i==2)
35                             ans+=1;
36                         i=j-1;
37                         break;
38                     }
39                 }
40             }
41         }
42         cout<<ans<<endl;
43     }
44     return 0;
45 }
复制代码

B. Marin and Anti-coprime Permutation

Today, Marin is at a cosplay exhibition and is preparing for a group photoshoot!

For the group picture, the cosplayers form a horizontal line. A group picture is considered beautiful if for every contiguous segment of at least 22 cosplayers, the number of males does not exceed the number of females (obviously).

Currently, the line has nn cosplayers which can be described by a binary string ss. The ii-th cosplayer is male if si=0si=0 and female if si=1si=1. To ensure that the line is beautiful, you can invite some additional cosplayers (possibly zero) to join the line at any position. You can't remove any cosplayer from the line.

Marin wants to know the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful. She can't do this on her own, so she's asking you for help. Can you help her?

Input

The first line contains a single integer tt (1t1031≤t≤103) — the number of test cases.

The first line of each test case contains a positive integer nn (1n1001≤n≤100) — the number of cosplayers in the initial line.

The second line of each test case contains a binary string ss of length nn — describing the cosplayers already in line. Each character of the string is either 0 describing a male, or 1 describing a female.

Note that there is no limit on the sum of nn.

Output

For each test case, print the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful.

题目大意:给定一个正整数n,将1~n任意排序并分别乘上其下标(下标从1开始)后得到一个新的序列,判断有多少个这样的序列满足整体最大公约数不等于1。
思路:我们可以设一个x,则1~n中有n/x个数包含x的因子,在加上该乘的数(下标1~n)即有2*(n/x)个含x的因子,则满足的序列需要满足2*(n/x)>=n。可得x只能等于2。即序列全是奇数时不满足,全是偶数时满足。因此当n为奇数时答案为零,为偶数时只需让奇数位乘上偶数,偶数位乘上奇数即可,答案为(n/2)!*(n/2)!。

AC代码:

复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int t;
 4 typedef long long ll;
 5 ll n, ans;
 6 ll sss(ll k){
 7     ll iu=1;
 8     for(ll i=1;i<=k;i++){
 9         iu=(iu*(i%98244353))%998244353;
10     }
11     return iu;
12 }
13 int main()
14 {
15     std::ios::sync_with_stdio(false);
16     cin>>t;
17     while(t--){
18         cin>>n;
19         if(n%2!=0){
20             cout<<"0"<<endl;
21             continue;
22         }
23         ll op=n/2;
24         ll kl=sss(op);
25         ans=(kl*kl)%998244353;
26         cout<<ans<<endl;
27     }
28     return 0;
29 }
复制代码

C. Shinju and the Lost Permutation

Shinju loves permutations very much! Today, she has borrowed a permutation pp from Juju to play with.

The ii-th cyclic shift of a permutation pp is a transformation on the permutation such that p=[p1,p2,,pn]p=[p1,p2,…,pn] will now become p=[pni+1,,pn,p1,p2,,pni]p=[pn−i+1,…,pn,p1,p2,…,pn−i].

Let's define the power of permutation pp as the number of distinct elements in the prefix maximums array bb of the permutation. The prefix maximums array bb is the array of length nn such that bi=max(p1,p2,,pi)bi=max(p1,p2,…,pi). For example, the power of [1,2,5,4,6,3][1,2,5,4,6,3] is 44 since b=[1,2,5,5,6,6]b=[1,2,5,5,6,6] and there are 44 distinct elements in bb.

Unfortunately, Shinju has lost the permutation pp! The only information she remembers is an array cc, where cici is the power of the (i1)(i−1)-th cyclic shift of the permutation pp. She's also not confident that she remembers it correctly, so she wants to know if her memory is good enough.

Given the array cc, determine if there exists a permutation pp that is consistent with cc. You do not have to construct the permutation pp.

A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

Input

The input consists of multiple test cases. The first line contains a single integer tt (1t51031≤t≤5⋅103) — the number of test cases.

The first line of each test case contains an integer nn (1n1051≤n≤105).

The second line of each test case contains nn integers c1,c2,,cnc1,c2,…,cn (1cin1≤ci≤n).

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case, print "YES" if there is a permutation pp exists that satisfies the array cc, and "NO" otherwise.


题目大意:这一题题目好绕,英语不好属实难受。大概就是由一个全排列得到一个数组,数组第i个数是全排列中1~i中最大的数。而数组中不相同的数的个数称为全排列的幂。那么可以将全排列整体右移一个单位(相当于最后一个提到第一)后等到另一个幂。将这些幂依次存入C数组,问是否存在一个全排列令移动i次的值等于Ci的值。
思路:判断两个特有情况。
1.出现幂等于1的情况有且只有一种。
2.因为每次移动相当于从后提出一位到最前方,所以如果右移后幂增加,那么增加的数量肯定不会超过1。
只有同时满足这两种情况的全排列才是满足题意的全排列。

AC代码:

复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int t,n,c[100001];
 4 int main()
 5 {
 6     std::ios::sync_with_stdio(false);
 7     cin.tie(0);
 8     cin>>t;
 9     while(t--){
10         cin>>n;
11         for(int i=1;i<=n;i++){
12             cin>>c[i];
13         }
14         c[n+1]=c[1];
15         int flag=1,sum=0;
16         for(int i=1;i<=n;i++){
17             if(c[i+1]-1>c[i])
18                 flag=0;
19             if(c[i]==1)
20                 sum++;
21         }
22         if(flag&&sum==1)
23             cout<<"YES"<<endl;
24         else
25             cout<<"NO"<<endl;
26     }
27     return 0;
28 }
复制代码

D1. 388535 (Easy Version)

This is the easy version of the problem. The difference in the constraints between both versions is colored below in red. You can make hacks only if all versions of the problem are solved.

Marin and Gojou are playing hide-and-seek with an array.

Gojou initially performs the following steps:

  • First, Gojou chooses 22 integers ll and rr such that lrl≤r.
  • Then, Gojou makes an array aa of length rl+1r−l+1 which is a permutation of the array [l,l+1,,r][l,l+1,…,r].
  • Finally, Gojou chooses a secret integer xx and sets aiai to aixai⊕x for all ii (where ⊕ denotes the bitwise XOR operation).

Marin is then given the values of l,rl,r and the final array aa. She needs to find the secret integer xx to win. Can you help her?

Note that there may be multiple possible xx that Gojou could have chosen. Marin can find any possible xx that could have resulted in the final value of aa.

Input

The first line contains a single integer tt (1t1051≤t≤105) — the number of test cases.

In the first line of each test case contains two integers ll and rr (0=lr<2170=l≤r<217).

The second line contains rl+1r−l+1 integers of a1,a2,,arl+1a1,a2,…,ar−l+1 (0ai<2170≤ai<217). It is guaranteed that aa can be generated using the steps performed by Gojou.

It is guaranteed that the sum of rl+1r−l+1 over all test cases does not exceed 217217.

Output

For each test case print an integer xx. If there are multiple answers, print any.

D
题目大意:给出一个0到r的序列(题目给的是l,但l恒等于0)另外给你一个序列a,找出一个整数与序列a整体异或为0到r的一个全排列。
思路:观察0~7的二进制可发现每一位上0的数量肯定大于等于1,所以对于给定的序列,对于每一位0>=1的位置取0,反之取1便得到答案的二进制形式。

AC代码:

复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int t,op[20][2];
 4 int main()
 5 {
 6     std::ios::sync_with_stdio(false);
 7     cin.tie(0),cout.tie(0);
 8     cin>>t;
 9     while(t--)
10     {
11         int l,r;
12         cin>>l>>r;
13         memset(op,0,sizeof(op));
14         for(int i=0;i<=r;i++)
15         {
16             int a;
17             cin>>a;
18             for(int j=0;j<=17;j++)
19                 op[j][a>>j&1]++;
20         }
21         int ans=0;
22         for(int i=0;i<=17;i++)
23         {
24             if(op[i][0]<op[i][1])
25                 ans+=1<<i;
26         }
27         cout<<ans<<endl;
28     }
29     return 0;
30 }
复制代码

 

 

 

 

posted @   cbmango  阅读(100)  评论(3编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示