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 1→2→…→n→1→2→…1→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→(n−1)→…→1→n→(n−1)→…n→(n−1)→…→1→n→(n−1)→… (so the next stop after station xx is equal to (x−1)(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 (4≤n≤1004≤n≤100, 1≤a,x,b,y≤n1≤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.
a会往++方向,b会往--方向,所以直接模拟即可
代码略
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 (1≤x<y≤n1≤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 (2≤n≤3000002≤n≤300000, 1≤m≤3000001≤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 (1≤ai,bi≤n,ai≠bi1≤ai,bi≤n,ai≠bi) — the integers in the ii-th pair.
Output
Output "YES" if there exist two integers xx and yy (1≤x<y≤n1≤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.
很容易可以发现,第一组数里一定有一个数是答案,那我们check一下这两个数,哪个是,所以分别试一下,然后把当前这个数不满足条件的一些数对存起来,用map记录一下次数,然后再随便找存起来不满足的两个数,检测一下出现次数是否和出现不满足的对数相同即可
P p[maxn];
int main()
{
int a,b,x,y,n,m;
while(cin >> n >> m){
set<int>s;
for(int i = 1;i <= m;i++) cin >> p[i].fi >> p[i].se,s.insert(p[i].fi),s.insert(p[i].se);
if(m == 2 || m == 1){
cout << "YES" << endl;
continue;
}
map<int,int>mp;
vector<P>v;
int num = p[1].fi,cnt = 0;
for(int i = 1;i <= m;i++){
if(num == p[i].fi || num == p[i].se) continue;
cnt++;
v.pb(p[i]);
mp[p[i].fi]++;
mp[p[i].se]++;
}
if(cnt == 0 || mp[v[0].fi] == cnt || mp[v[0].se] == cnt){
wt("YES");
continue;
}
num = p[1].se,cnt = 0;
mp.clear(); v.clear();
// wt(num);
for(int i = 1;i <= m;i++){
if(num == p[i].fi || num == p[i].se) continue;
cnt++;
v.pb(p[i]);
mp[p[i].fi]++;
mp[p[i].se]++;
}
// wt(cnt);
if(cnt == 0 || mp[v[0].fi] == cnt || mp[v[0].se] == cnt){
wt("YES");
continue;
}
wt("NO");
}
return 0;
}
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 m−1m−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 1≤i1<i2<…<ik≤n1≤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 (1≤n,m≤3000001≤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 (0≤ai<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 1
题意比较难理解,就是你可以任意选择一些数,让他们每个值++,问你最少进行多少次操作后,使得所有的数变成非递减的序列
二分答案+贪心,二分操作数,然后check,因为我们肯定是要让当前的数尽量的小,假如我们现在二分的操作数是k,那么如果k足够使得b[i] + k - 一些数,变成 b[i - 1],也就是让b[i]变得最少,我们就让他改变,然后判断一下就可以了,还要注意b[i]和b[i - 1]的大小关系
int a[maxn],n,m,b[maxn];
bool check(int k){
for(int i = 1;i <= n;i++) b[i] = a[i];
for(int i = 1;i <= n;i++){
if(b[i] >= b[i - 1]){
if(b[i] + k - m >= b[i - 1]) b[i] = b[i - 1];
}else if(b[i] + k >= b[i - 1]) b[i] = b[i - 1];
if(b[i] < b[i - 1]) return false;
}
return true;
}
int main()
{
while(cin >> n >> m){
for(int i = 1;i <= n;i++) cin >> a[i];
int l = 0,r = 1e9,ans = 0;
while(l <= r){
int mid = (l + r) / 2;
if(check(mid)){
ans = mid;
r = mid - 1;
}else l = mid + 1;
}
wt(ans);
}
return 0;
}
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 1≤l≤r≤n1≤l≤r≤n and there is at least one pair of integers xx, kk such that 1≤x,k≤n1≤x,k≤n, l≤x<x+2k≤rl≤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 1≤l≤r≤n1≤l≤r≤n and there is at least one pair of integers xx, kk such that 1≤x,k≤n1≤x,k≤n, l≤x<x+2k≤rl≤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 l, r 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 0.
给一个01串,让你找出来有多少个l到r区间,符合条件
条件就是需要有三个相同的字符,并且他们的间隔相同
首先可以暴力枚举每个位置,先找出来对于每个位置,枚举k的间隔,找到最小的符合的区间,然后就会变成一些i到r[i]的一些区间段,那么就可以计数了,用后缀记录i到n的最少值,一遍扫过去,每次i位置的答案就是len - min,求和就可以了
int a[maxn],n,m,r[maxn];
string str;
int main()
{
while(cin >> str){
ll ans = 0;
int len = str.size();
memset(r,INF,sizeof(r));
for(int i = 0;i < len;i++){
for(int k = 1; i + k + k < len;k++){
if(str[i] == str[i + k] && str[i + k + k] == str[i]){
r[i] = i + k + k;
break;
}
}
}
for(int i = len;i >= 0;i--) r[i] = min(r[i + 1],r[i]);
for(int i = 0;i < len;i++){
if(r[i] != INF) ans += len - r[i];
}wt(ans);
}
return 0;
}