Codeforces Round #753 (Div.3) A~D problem solution
A. Linear Keyboard
meaning of the title
Given a keyboard composed of 26 lowercase letters in a certain order, each group gives a word, and calculates the distance that the hand moves to complete the word.
thinking
First, create two strings a and s to store the keyboard and words respectively.
If you only store the keyboard in the string, you need to traverse the string to find the position of a specific letter in the process. In case of a large amount of data, it may timeout.
Therefore, you can create an integer array k, store the corresponding positions of a to e with its subscripts from 0 to 25, and then simulate the value of k [s [i] - [a '] in the process of hand movement for subtraction.
summary
When doing questions, you need to have the awareness of preprocessing the stored data.
You can also use map. You should learn c + + related knowledge as soon as possible, rather than continue to use c in c + + programs.
AC code
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
A - Linear Keyboard | GNU C++17 | Accepted | 0 ms | 0 KB |
#include<bits/stdc++.h> using namespace std; int t; char a[30]; char s[55]; int k[30]; void solve() { scanf("%s", a); scanf("%s", s); int sum = 0; memset(k, 0, sizeof(k)); for (int i = 0; i < strlen(a); i++) { k[a[i] - 'a']=i; } for (int i = 1; i < strlen(s); i++) { sum+=abs(k[s[i]-'a'] - k[s[i - 1]-'a']); } printf("%d\n", sum); } int main() { scanf("%d", &t); while (t--) { solve(); } return 0; }
B. Odd Grasshopper
meaning of the title
Given initial position $x_0 $and number of exercises n n n. The rule of motion is that if the coordinates are even, the direction of motion is left, otherwise the direction of motion is right; The distance of movement is the corresponding number of movements, that is, one unit for the first movement n n n movements n n n units. Find the final position.
common
t
t
t tests, one set of $X each time_ 0 $and
n
n
n. The data range is as follows:
t
(
1
≤
t
≤
1
0
4
)
⋯
⋯
x
0
(
−
1
0
14
≤
x
0
≤
1
0
14
)
⋯
⋯
n
(
0
≤
n
≤
1
0
14
)
t(1≤t≤10^4)\cdots\cdots x_0(−10^{14}≤x_0≤10^{14})\cdots\cdots n(0≤n≤10^{14})
t(1≤t≤104)⋯⋯x0(−1014≤x0≤1014)⋯⋯n(0≤n≤1014)
thinking
As you can see, x 0 x_0 x0 and n n The data range of n is very large, which exceeds the range of int. you should pay attention to using long long to define variables.
In addition, such large data can also remind us that the operation in the topic should be periodic.
So it can be found that, T = 4 T=4 T=4, that is, it will return to its original position after four operations.
So will n n After n takes the remaining 4, it can be discussed by classification.
summary
When you see that the number of operations or data is very large, you should immediately realize that there is a great possibility of periodicity or certain regularity.
You should pay attention to the range of variables and think about whether int will be exceeded during the operation.
AC code
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
B - Odd Grasshopper | GNU C++17 | Accepted | 15 ms | 0 KB |
#include<bits/stdc++.h> using namespace std; int t; long long n; long long x; void solve() { scanf("%lld%lld", &x,&n); long long left = n % 4; if (x % 2) { if (left == 3)x -= n+1; if (left == 2)x -= 1; if (left == 1)x += n; } else { if (left == 3)x += n+1; if (left == 2)x += 1; if (left == 1)x -= n; } printf("%lld\n", x); } int main() { scanf("%d", &t); while (t--) { solve(); } return 0; }
C. Minimum Extraction
meaning of the title
Give an array, which can be operated as follows;
Select the smallest element in the array, make the remaining elements subtract this element respectively, and then delete this element (if there is more than one same smallest element, delete only one). After any number of operations, what is the maximum value of the smallest element in the array after each operation.
t
t
t group test, with
n
n
n elements, array elements
a
i
a_i
ai indicates that the data range is as follows:
t
(
1
≤
t
≤
1
0
4
)
⋯
⋯
n
(
1
≤
n
≤
2
⋅
1
0
5
)
⋯
⋯
a
i
(
−
1
0
9
≤
a
i
≤
1
0
9
)
t(1≤t≤10^4)\cdots\cdots n(1≤n≤2⋅10^5)\cdots\cdots a_i (−10^9≤a_i≤10^9)
t(1≤t≤104)⋯⋯n(1≤n≤2⋅105)⋯⋯ai(−109≤ai≤109)
thinking
The problem can be transformed into: after sorting the array from small to large, what is the maximum value of the difference between adjacent elements.
summary
When doing questions, the most difficult thing is to see the essence of the problem. We need to improve the ability to transform the problem.
AC code
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
C - Minimum Extraction | GNU C++17 | Accepted | 78 ms | 800 KB |
#include<bits/stdc++.h> using namespace std; int t; int n; int a[200010]; void solve() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); int Max = a[0]; for (int i = 0; i < n-1; i++) { Max = max(a[i + 1] - a[i],Max); } printf("%d\n", Max); } int main() { scanf("%d", &t); while (t--) { solve(); } return 0; }
D. Blue-Red Permutation
meaning of the title
Give an array containing n n n elements. Each element has two attributes: value and color. The color is divided into blue or red.
You can operate as follows:
(1) : select any number of blue elements and subtract their values by one.
(2) : select any number of red elements so that their values are increased by one.
Ask if you can make the array eventually become 1 to 2 n n An arrangement of n?
The data range is as follows:
Number of test groups t t t ( 1 ≤ t ≤ 1 0 4 ) (1≤t≤10^4) (1 ≤ t ≤ 104), number of elements n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) n(1≤n≤2⋅10^5) n(1 ≤ n ≤ 2 ⋅ 105), element a i ( − 1 0 9 ≤ a i ≤ 1 0 9 ) a_i(−10^9≤a_i≤10^9) ai(−109≤ai≤109)
thinking
In other words, the blue element can become any value smaller than it, and the red element can become any value larger than it.
It can be easily found that the maximum value of blue elements needs to be greater than or equal to the number of blue elements. Similarly, the minimum value of red elements also needs to be less than or equal to n n n minus the number of red elements.
It can be further found that each element needs to meet similar conditions, otherwise it is obviously impossible to appear similar to blue 1 and blue 1. The second blue 1 needs to be greater than or equal to 2.
So what's the number here? Is the position of the blue element after sorting it from small to large, the second n n n elements must be greater than or equal to n n n ; Similarly, the red element is a similar condition, and the opposite is OK.
Therefore, save the red and blue elements in two arrays respectively, sort them, and then traverse the judgment. All meet the output of yes, and one does not meet the output of no.
summary
Attention to data preprocessing is sometimes the key to solving problems.
AC code
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
D - Blue-Red Permutation | GNU C++17 | Accepted | 78 ms | 2600 KB |
#include<bits/stdc++.h> using namespace std; int t; int n; int a[200010]; int a1[200010]; int a2[200010]; char s[200010]; void solve() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); scanf("%s", s);//Store a string similar to "BRBR" int k1 = 0, k2 = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') { a1[k1] = a[i]; k1++; } else { a2[k2] = a[i]; k2++; } } sort(a1, a1 + k1);//The default is to sort from small to large sort(a2, a2 + k2,greater<int>());//greater is the sort from big to small //Test procedure /*for (int i = 0; i < k1; i++) printf("%d ", a1[i]); printf("\n"); for (int i = 0; i < k2; i++) printf("%d ", a2[i]);*/ int flag = 1; for (int i = 0; i < k1; i++) { if (a1[i] < i+1) { flag = 0; break; } } for (int i = 0; i < k2; i++) { if (flag == 0)break; if (a2[i] > n-i) { flag = 0; break; } } if (flag)printf("YES\n"); else printf("NO\n"); } int main() { scanf("%d", &t); while (t--) { solve(); } return 0; }
by syj
tip: I hope I can have a a E