[Codeforces Round #562 (Div. 2)]

https://codeforc.es/contest/1169

A. Circle Metro
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The circle line of the Roflanpolis subway has nn stations.

There are two parallel routes in the subway. The first one visits stations in order 12n121→2→…→n→1→2→… (so the next stop after station xx is equal to (x+1)(x+1) if x<nx<n and 11 otherwise). The second route visits stations in order n(n1)1n(n1)n→(n−1)→…→1→n→(n−1)→… (so the next stop after station xx is equal to (x1)(x−1) if x>1x>1 and nn otherwise). All trains depart their stations simultaneously, and it takes exactly 11 minute to arrive at the next station.

Two toads live in this city, their names are Daniel and Vlad.

Daniel is currently in a train of the first route at station aa and will exit the subway when his train reaches station xx.

Coincidentally, Vlad is currently in a train of the second route at station bb and he will exit the subway when his train reaches station yy.

Surprisingly, all numbers a,x,b,ya,x,b,y are distinct.

Toad Ilya asks you to check if Daniel and Vlad will ever be at the same station at the same time during their journey. In other words, check if there is a moment when their trains stop at the same station. Note that this includes the moments when Daniel or Vlad enter or leave the subway.

Input

The first line contains five space-separated integers nn, aa, xx, bb, yy (4n1004≤n≤100, 1a,x,b,yn1≤a,x,b,y≤n, all numbers among aa, xx, bb, yy are distinct) — the number of stations in Roflanpolis, Daniel's start station, Daniel's finish station, Vlad's start station and Vlad's finish station, respectively.

Output

Output "YES" if there is a time moment when Vlad and Daniel are at the same station, and "NO" otherwise. You can print each letter in any case (upper or lower).

Examples
input
Copy
5 1 4 3 2
output
Copy
YES
input
Copy
10 2 1 9 10
output
Copy
NO
Note

In the first example, Daniel and Vlad start at the stations (1,3)(1,3). One minute later they are at stations (2,2)(2,2). They are at the same station at this moment. Note that Vlad leaves the subway right after that.

Consider the second example, let's look at the stations Vlad and Daniel are at. They are:

  • initially (2,9)(2,9),
  • after 11 minute (3,8)(3,8),
  • after 22 minutes (4,7)(4,7),
  • after 33 minutes (5,6)(5,6),
  • after 44 minutes (6,5)(6,5),
  • after 55 minutes (7,4)(7,4),
  • after 66 minutes (8,3)(8,3),
  • after 77 minutes (9,2)(9,2),
  • after 88 minutes (10,1)(10,1),
  • after 99 minutes (1,10)(1,10).

After that, they both leave the subway because they are at their finish stations, so there is no moment when they both are at the same station.

题意:车站有n(4<=n<=100)个站点,两个人坐车,一个人按照x->a+1->....n->1..->a的路线移动,另一个按照y->y-1->....1->n...->b,问是否会存在两个人在同一站点的时刻

题解:n范围很小直接模拟即可

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define debug(x) cout<<#x<<" is "<<x<<endl;
 5 int w[105];
 6 int main(){
 7     int n,a,x,b,y;
 8     cin>>n>>a>>x>>b>>y;
 9     if(x<a){
10         x=x+n;
11     }
12     int t=1;
13     for(int i=a;i<=x;i++){
14         int xx=i%n;
15         if(xx==0)xx=n;
16         w[xx]=t;
17         t++;
18     }
19     if(y>b)b=b+n;
20     int t2=1;
21     int f=0;
22     for(int i=b;i>=y;i--){
23         int xx=i%n;
24         if(xx==0)xx=n;
25         if(w[xx]==t2){
26             f=1;
27            // debug(t2);
28            // debug(xx);
29             break;
30         }
31         t2++;
32     }
33     if(f)printf("YES\n");
34     else printf("NO\n");
35     return 0;
36 }
View Code
B. Pairs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Toad Ivan has mm pairs of integers, each integer is between 11 and nn, inclusive. The pairs are (a1,b1),(a2,b2),,(am,bm)(a1,b1),(a2,b2),…,(am,bm).

He asks you to check if there exist two integers xx and yy (1x<yn1≤x<y≤n) such that in each given pair at least one integer is equal to xx or yy.

Input

The first line contains two space-separated integers nn and mm (2n3000002≤n≤300000, 1m3000001≤m≤300000) — the upper bound on the values of integers in the pairs, and the number of given pairs.

The next mm lines contain two integers each, the ii-th of them contains two space-separated integers aiai and bibi (1ai,bin,aibi1≤ai,bi≤n,ai≠bi) — the integers in the ii-th pair.

Output

Output "YES" if there exist two integers xx and yy (1x<yn1≤x<y≤n) such that in each given pair at least one integer is equal to xx or yy. Otherwise, print "NO". You can print each letter in any case (upper or lower).

Examples
input
Copy
4 6
1 2
1 3
1 4
2 3
2 4
3 4
output
Copy
NO
input
Copy
5 4
1 2
2 3
3 4
4 5
output
Copy
YES
input
Copy
300000 5
1 2
1 2
1 2
1 2
1 2
output
Copy
YES
Note

In the first example, you can't choose any xx, yy because for each such pair you can find a given pair where both numbers are different from chosen integers.

In the second example, you can choose x=2x=2 and y=4y=4.

In the third example, you can choose x=1x=1 and y=2y=2.

题意:有m个数对(ai,bi),问是否存在两个数x,y使每个数对至少有一个数字=x或者=y

题解:通过枚举第一个数对中的数字可以确定后面的数字。即如果a1=x,那么把所有不含a1的数对存起来,判断数对中数字最多的数量是否=不含a1的数的总个数,同理可以枚举b1=x。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define debug(x) cout<<#x<<" is "<<x<<endl;
 5 int x[300005],y[300005],in[300005],in2[300005];
 6 vector<int>g;
 7 int main(){
 8     int n,m;
 9     cin>>n>>m;
10     for(int i=1;i<=m;i++){
11         scanf("%d%d",&x[i],&y[i]);
12     }
13     for(int i=2;i<=m;i++){
14         if(x[i]!=x[1]&&y[i]!=x[1]){
15             g.push_back(i);
16         }
17     }
18     int f=0;
19     for(int i=0;i<g.size();i++){
20         in[x[g[i]]]++;
21         in[y[g[i]]]++;
22         if(in[x[g[i]]]==g.size()||in[y[g[i]]]==g.size()){
23             f=1;
24         }
25     }
26     if(g.size()==0)f=1;
27     g.clear();
28    // int maxx=0;
29     for(int i=2;i<=m;i++){
30         if(x[i]!=y[1]&&y[i]!=y[1]){
31             g.push_back(i);
32         }
33     }
34     for(int i=0;i<g.size();i++){
35         in2[x[g[i]]]++;
36         in2[y[g[i]]]++;
37         if(in2[x[g[i]]]==g.size()||in2[y[g[i]]]==g.size()){
38             f=1;
39         }
40     }
41     if(g.size()==0)f=1;
42     if(f)printf("YES\n");
43     else printf("NO\n");
44     return 0;
45 }
View Code
C. Increasing by Modulo
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Toad Zitz has an array of integers, each integer is between 00 and m1m−1 inclusive. The integers are a1,a2,,ana1,a2,…,an.

In one operation Zitz can choose an integer kk and kk indices i1,i2,,iki1,i2,…,ik such that 1i1<i2<<ikn1≤i1<i2<…<ik≤n. He should then change aijaij to ((aij+1)modm)((aij+1)modm) for each chosen integer ijij. The integer mm is fixed for all operations and indices.

Here xmodyxmody denotes the remainder of the division of xx by yy.

Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations.

Input

The first line contains two integers nn and mm (1n,m3000001≤n,m≤300000) — the number of integers in the array and the parameter mm.

The next line contains nn space-separated integers a1,a2,,ana1,a2,…,an (0ai<m0≤ai<m) — the given array.

Output

Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 00.

It is easy to see that with enough operations Zitz can always make his array non-decreasing.

Examples
input
Copy
5 3
0 0 0 1 2
output
Copy
0
input
Copy
5 7
0 6 1 3 2
output
Copy
1
Note

In the first example, the array is already non-decreasing, so the answer is 00.

In the second example, you can choose k=2k=2, i1=2i1=2, i2=5i2=5, the array becomes [0,0,1,3,3][0,0,1,3,3]. It is non-decreasing, so the answer is 11

 题意:给一个数组,数组中元素范围为0~n,每次你可以选择若干元素进行(ai+1)%n的操作,问至少多少次可以使数组元素呈现非严格单调递减的状态

题解:二分+贪心。二分次数,然后尽可能让当前元素小,但要大于前一个元素。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define debug(x) cout<<#x<<" is "<<x<<endl;
 5 ll a[300005],b[300005];
 6 int n,m;
 7 bool check(ll x){
 8     ll maxx=0;
 9     for(int i=1;i<=n;i++){
10         b[i]=a[i];
11     }
12     for(int i=1;i<=n;i++){
13         if(b[i]<maxx){
14             if(b[i]+x<maxx){
15                 return false;
16             }
17             else{
18                 b[i]=maxx;
19             }
20         }
21         else if(b[i]>maxx){
22             if(b[i]+x>=maxx+m){
23                 continue;
24             }
25             else{
26                 maxx=max(maxx,b[i]);
27             }
28         }
29     }
30     return true;
31 }
32 int main(){
33    // int n,m;
34     scanf("%d%d",&n,&m);
35     for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
36     ll r=10000000;
37     ll l=0;
38     ll ans;
39     while(l<=r){
40         ll mid=(l+r)/2;
41         if(check(mid)){
42             ans=mid;
43             r=mid-1;
44         }
45         else{
46             l=mid+1;
47         }
48     }
49     printf("%lld\n",ans);
50     return 0;
51 }
View Code
D. Good Triple
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Toad Rash has a binary string ss. A binary string consists only of zeros and ones.

Let nn be the length of ss.

Rash needs to find the number of such pairs of integers ll, rr that 1lrn1≤l≤r≤n and there is at least one pair of integers xx, kk such that 1x,kn1≤x,k≤n, lx<x+2krl≤x<x+2k≤r, and sx=sx+k=sx+2ksx=sx+k=sx+2k.

Find this number of pairs for Rash.

Input

The first line contains the string ss (1|s|3000001≤|s|≤300000), consisting of zeros and ones.

Output

Output one integer: the number of such pairs of integers ll, rr that 1lrn1≤l≤r≤n and there is at least one pair of integers xx, kk such that 1x,kn1≤x,k≤n, lx<x+2krl≤x<x+2k≤r, and sx=sx+k=sx+2ksx=sx+k=sx+2k.

Examples
input
Copy
010101
output
Copy
3
input
Copy
11001100
output
Copy
0
Note

In the first example, there are three ll, rr pairs we need to count: 11, 66; 22, 66; and 11, 55.

In the second example, there are no values xx, kk for the initial string, so the answer is 00.

题意:给一个01字符串,求字符串内有多少个区间满足区间内有ch[i]=ch[i+k]=ch[i+2*k]。

题解:可以知道01字符串在长度很短的时候就一定存在这样的区间,所以就算暴力枚举左端点,满足条件的右端点也一定会在左端点不远处,所以可以直接暴力枚举左端点i,一点点检查恰好成立的右端点j,则有ans+=len-j+1

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define debug(x) cout<<#x<<" is "<<x<<endl;
 5 char ch[300005];
 6 int main(){
 7     scanf("%s",ch+1);
 8     ll ans=0;
 9     int len=strlen(ch+1);
10     for(int i=1;i<=len;i++){
11         int f=0;
12         for(int j=i;j<=len;j++){
13             for(int k=1;j-2*k>=i;k++){
14                 if(ch[j]==ch[j-k]&&ch[j-k]==ch[j-2*k]){
15                     ans+=len-j+1;
16                     f=1;
17                     break;
18                 }
19             }
20             if(f)break;
21         }
22     }
23     printf("%lld\n",ans);
24     return 0;
25 }
View Code

猜你喜欢

转载自www.cnblogs.com/MekakuCityActor/p/10929932.html