A. Linear Keyboard
General idea of the topic
The first line gives you a string with a length of 26, representing the order of 26 letters, and the distance between adjacent letters is 1.
The second line gives you a string and asks how long it took to walk from beginning to end.
Problem solving ideas
Just enumerate.
AC code
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { string s1, s2; cin >> s1 >> s2; int ans = 0; for (int i = 1; i < s2.size(); i++) { int k = s1.find(s2[i]) - s1.find(s2[i - 1]); ans += abs(k); } cout << ans << endl; } return 0; }
B. Odd Grasshopper
General idea of the topic
Give you a starting coordinate x and give you a number of jumps n. The length of the ith jump is i. Then the direction of the jump depends on the parity of the jump position. Even number jumps to the left, odd number jumps to the right, and ask the last position.
Problem solving ideas
Discuss the classification of parity of x and find the law.
AC code
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 10; int main() { int t; scanf("%d", &t); while (t--) { ll x, n; scanf("%lld%lld", &x, &n); ll ans; if (x % 2 == 0) { if (n % 4 == 0) ans = x; if (n % 4 == 1) ans = x - n; if (n % 4 == 2) ans = x + 1; if (n % 4 == 3)ans = n + 1 + x; } else { if (n % 4 == 0) ans = x; if (n % 4 == 1) ans = x + n; if (n % 4 == 2) ans = x - 1; if (n % 4 == 3) ans = x - 1 - n; } cout << ans << endl; } return 0; }
C. Minimum Extraction
General idea of the topic
Give you an array of length n, find the smallest element x from the array each time, then delete the element, and all the remaining elements are - x until the length of the array is 1. Find the maximum value of the smallest element in the array after each change.
Problem solving ideas
Set an ordered array: A1 A2 A3 A4 A5.
Step 1: A1 A2 A3 A4 A5
Step 2: A2-A1 A3-A1 A4-A1 A5-A1
Step 3: A3-A2 A4-A2 A5-A2
Step 4: A4-A3 A5-A3
Step 5: A5-A4
To sum up: the answer is max(A1, A2-A1, A3-A2, A4-A3, A5-A4)
AC code
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5 + 10; int n; int a[N]; int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + 1 + n); int mx = a[1]; for (int i = 1; i <= n; i++) mx = max(mx, a[i] - a[i - 1]); printf("%d\n", mx); } return 0; }
D. Blue-Red Permutation
General idea of the topic
Give you an array with a length of N, and then give you a string with a length of N. the string is composed of 'B' and 'R'. Each element in the array wants to correspond to the string. If the corresponding character is' B ', it means that the element can be reduced by 1 every time. On the contrary, if it corresponds to' R ', it means that the element can be increased by 1 every time, and the number of times is unlimited. Ask if you can change the array into an arrangement of 1~n.
Problem solving ideas
This is a greedy problem.
Let the number of blue elements be len1 and the number of red elements be len2
Judge whether all blue elements can form 1~len1 and whether all red elements can form len+1 ~ n. If not, the answer is NO, otherwise YES.
AC code
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5 + 10; int a[N]; int main() { int t; cin >> t; while (t--) { vector<int> blue, red; int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); string s; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == 'B') blue.push_back(a[i]); else red.push_back(a[i]); } sort(blue.begin(), blue.end()); sort(red.begin(), red.end(), greater<int>()); bool bug = true; for (int i = 0; i < blue.size(); i++) { if (blue[i] <= i) { bug = false; break; } } for (int i = 0; i < red.size(); i++) { if (red[i] > n - i) { bug = false; break; } } puts(bug ? "YES" : "NO"); } return 0; }
E. Robot on the Board 1
General idea of the topic
Give you an n * m grid and a string to represent the walking path of the robot. The robot can't go out of the grid. Ask the robot where to start to walk to make the walking distance longer.
Problem solving ideas
Just enumerate the violence. Use four variables Mn1, MX1, Mn2 and MX2 to record the longest distance to go left, right, up and down. Jump out of the loop when mx1-mn1 + 1 > m or mx2-mn2 + 1 > n.
AC code
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, m; scanf("%d%d", &n, &m); string s; cin >> s; int mn1 = 0, mn2 = 0, mx1 = 0, mx2 = 0; int h = 0, l = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == 'R') h++; if (s[i] == 'L') h--; if (s[i] == 'U') l++; if (s[i] == 'D') l--; mn1 = min(mn1, h); mx1 = max(mx1, h); mn2 = min(mn2, l); mx2 = max(mx2, l); if (mx1 - mn1 + 1 > m) { if (s[i] == 'R') mx1--; else mn1++; break; } if (mx2 - mn2 + 1 > n) { if (s[i] == 'U') mx2--; else mn2++; break; } } printf("%d %d\n", 1 + mx2, 1 - mn1); } return 0; }