Codeforces Round #496 (Div. 3) ABCDE
A. Tanya and Stairways
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from 11 to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains 33steps, and the second contains 44 steps, she will pronounce the numbers 1,2,3,1,2,3,41,2,3,1,2,3,4.
You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
The first line contains nn (1≤n≤10001≤n≤1000) — the total number of numbers pronounced by Tanya.
The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with xx steps, she will pronounce the numbers 1,2,…,x1,2,…,x in that order.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
In the first line, output tt — the number of stairways that Tanya climbed. In the second line, output tt numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.
7
1 2 3 1 2 3 4
2
3 4
4
1 1 1 1
4
1 1 1 1
5
1 2 3 4 5
1
5
5
1 2 1 2 1
3
2 2 1
签到题
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 1010; 5 int a[N]; 6 7 int main() { 8 int n; 9 cin >> n; 10 std::vector<int> v; 11 for(int i = 1; i <= n; i ++) cin >> a[i]; 12 int tmp = 1; 13 for(int i = 2; i <= n; i ++) { 14 if(a[i] == 1) v.push_back(a[i-1]); 15 } 16 v.push_back(a[n]); 17 cout << v.size() << endl; 18 for(int i = 0; i < v.size(); i ++) { 19 cout << v[i] << ' '; 20 } 21 22 return 0; 23 }
B. Delete from the Left
You are given two strings ss and tt. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by 11. You can't choose a string if it is empty.
For example:
- by applying a move to the string "where", the result is the string "here",
- by applying a move to the string "a", the result is an empty string "".
You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.
Write a program that finds the minimum number of moves to make two given strings ss and tt equal.
The first line of the input contains ss. In the second line of the input contains tt. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and 2⋅1052⋅105, inclusive.
Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings.
test
west
2
codeforces
yes
9
test
yes
7
b
ab
1
In the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to "est".
In the second example, the move should be applied to the string "codeforces" 88 times. As a result, the string becomes "codeforces" →→ "es". The move should be applied to the string "yes" once. The result is the same string "yes" →→ "es".
In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty.
In the fourth example, the first character of the second string should be deleted.
求从右边开始相同的数量是多少。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 2e5+10; 5 char s[N], t[N]; 6 7 int main() { 8 cin >> s >> t; 9 int lens = strlen(s), lent = strlen(t); 10 int i = lens-1, j = lent-1; 11 int ans = 0; 12 while(s[i] == t[j] && i >= 0 && j >= 0) { 13 i--;j--; 14 ans += 2; 15 } 16 cout << lens+lent-ans << endl; 17 return 0; 18 }
C. Summarize to the Power of Two
A sequence a1,a2,…,ana1,a2,…,an is called good if, for each element aiai, there exists an element ajaj (i≠ji≠j) such that ai+ajai+aj is a power of two (that is, 2d2d for some non-negative integer dd).
For example, the following sequences are good:
- [5,3,11][5,3,11] (for example, for a1=5a1=5 we can choose a2=3a2=3. Note that their sum is a power of two. Similarly, such an element can be found for a2a2 and a3a3),
- [1,1,1,1023][1,1,1,1023],
- [7,39,89,25,89][7,39,89,25,89],
- [][].
Note that, by definition, an empty sequence (with a length of 00) is good.
For example, the following sequences are not good:
- [16][16] (for a1=16a1=16, it is impossible to find another element ajaj such that their sum is a power of two),
- [4,16][4,16] (for a1=4a1=4, it is impossible to find another element ajaj such that their sum is a power of two),
- [1,3,2,8,8,8][1,3,2,8,8,8] (for a3=2a3=2, it is impossible to find another element ajaj such that their sum is a power of two).
You are given a sequence a1,a2,…,ana1,a2,…,an. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
The first line contains the integer nn (1≤n≤1200001≤n≤120000) — the length of the given sequence.
The second line contains the sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all nn elements, make it empty, and thus get a good sequence.
6
4 7 1 5 4 9
1
5
1 2 3 4 5
2
1
16
1
4
1 1 1 1023
0
In the first example, it is enough to delete one element a4=5a4=5. The remaining elements form the sequence [4,7,1,4,9][4,7,1,4,9], which is good
找两个能够成2d的数。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 2e5+10; 5 int a[N], b[33]; 6 map<int,int> mp; 7 int main() { 8 int n, x; 9 for(int i = 0; i < 31; i ++) b[i] = (1<<i); 10 cin >> n; 11 for(int i = 1; i <= n; i ++) { 12 scanf("%d", &a[i]); 13 mp[a[i]]++; 14 } 15 int ans = 0; 16 map<int,int>::iterator it = mp.begin(); 17 for(; it != mp.end(); it ++) { 18 bool flag = false; 19 for(int j = 30; j > 0; j --) { 20 int tmp = b[j] - (*it).first; 21 if(tmp <= 0) break; 22 if(mp.count(tmp)) { 23 if(tmp == (*it).first && mp[tmp] == 1) continue; 24 // cout << (*it).first << ' '<< tmp << endl; 25 flag = true; 26 break; 27 } 28 } 29 if(!flag) { 30 ans += (*it).second; 31 } 32 } 33 cout << ans << endl; 34 return 0; 35 }
D. Polycarp and Div 3
Polycarp likes numbers that are divisible by 3.
He has a huge number ss. Polycarp wants to cut from it the maximum number of numbers that are divisible by 33. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after mm such cuts, there will be m+1m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 33.
For example, if the original number is s=3121s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|213|1|21. As a result, he will get two numbers that are divisible by 33.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 33 that Polycarp can obtain?
The first line of the input contains a positive integer ss. The number of digits of the number ss is between 11 and 2⋅1052⋅105, inclusive. The first (leftmost) digit is not equal to 0.
Print the maximum number of numbers divisible by 33 that Polycarp can get by making vertical cuts in the given number ss.
3121
2
6
1
1000000000000000000000000000000000
33
201920181
4
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 33.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 3333 digits 0. Each of the 3333digits 0 forms a number that is divisible by 33.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 00, 99, 201201 and 8181 are divisible by 33.
最多能分成多少份总和模3等于0 的数
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 2e5+10; 5 char str[N]; 6 int main() { 7 scanf("%s", str); 8 int len = strlen(str); 9 int ans = 0, tmp = 0, sum = 0, n = 0; 10 for(int i = 0; i < len; i ++) { 11 tmp = (str[i]-'0') % 3; 12 sum += tmp; 13 n++; 14 if(tmp == 0 || sum%3==0 || n == 3) { 15 ans++; 16 sum = n = 0; 17 } 18 } 19 cout << ans << endl; 20 return 0; 21 }
D. Polycarp and Div 3
Polycarp likes numbers that are divisible by 3.
He has a huge number ss. Polycarp wants to cut from it the maximum number of numbers that are divisible by 33. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after mm such cuts, there will be m+1m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 33.
For example, if the original number is s=3121s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|213|1|21. As a result, he will get two numbers that are divisible by 33.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 33 that Polycarp can obtain?
The first line of the input contains a positive integer ss. The number of digits of the number ss is between 11 and 2⋅1052⋅105, inclusive. The first (leftmost) digit is not equal to 0.
Print the maximum number of numbers divisible by 33 that Polycarp can get by making vertical cuts in the given number ss.
3121
2
6
1
1000000000000000000000000000000000
33
201920181
4
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 33.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 3333 digits 0. Each of the 3333digits 0 forms a number that is divisible by 33.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 00, 99, 201201 and 8181 are divisible by 33.
找到m的位置,然后计算左边大于m的数与小于m的数的差值。接着从右边反向计算算出结果。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 2e5+10; 5 int a[N], n, m, k; 6 map<int,ll> mp; 7 int main() { 8 scanf("%d %d", &n, &m); 9 for(int i = 1; i <= n; i ++) { 10 scanf("%d", &a[i]); 11 if(a[i] == m) k = i; 12 } 13 int s = 1; 14 for(int i = k; i > 0; i --) { 15 if(a[i] > m) s++; 16 else s--; 17 mp[s]++; 18 } 19 s = -1; 20 ll ans = 0; 21 for(int i = k; i <= n; i ++) { 22 if(a[i] > m) s--; 23 else s++; 24 ans += mp[s] + mp[s+1]; 25 } 26 printf("%lld\n",ans); 27 return 0; 28 }