A - Raise Both Hands
Problem Statement
Takahashi decided to make takoyaki (octopus balls) and serve it to Snuke. Takahashi instructed Snuke to raise only his left hand if he wants to eat takoyaki, and only his right hand otherwise.
You are given the information about which hand Snuke is raising as two integers L L L and R R R.
He is raising his left hand if and only if L = 1 L = 1 L=1, and raising his right hand if and only if R = 1 R = 1 R=1. He might not follow the instructions and could raise both hands or not raise any hand at all.
If Snuke is raising only one hand, print Yes
if he wants to eat takoyaki, and No
if he does not. If he is raising both hands or not raising any hand, print Invalid
.
Assume that if Snuke is raising only one hand, he is always following the instructions.
Constraints
Each of L L L and R R R is 0 0 0 or 1 1 1.
Input
The input is given from Standard Input in the following format:
L L L R R R
Output
Print Yes
, No
, or Invalid
according to the instructions in the problem statement.
Sample Input 1
1 0
Sample Output 1
Yes
Snuke wants to eat takoyaki, so he is raising only his left hand.
Sample Input 2
1 1
Sample Output 2
Invalid
Snuke is raising both hands.
Solution
具体见文末视频。
Code
cpp
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int a, b;
cin >> a >> b;
if (a > b) swap(a, b);
set<int> S;
if ((a + b) % 2 == 0) S.insert(a + b >> 1);
S.insert(b + b - a), S.insert(a - (b - a));
cout << S.size() << endl;
return 0;
}
B - Binary Alchemy
Problem Statement
There are N N N types of elements numbered 1 , 2 , ... , N 1, 2, \ldots, N 1,2,...,N.
Elements can be combined with each other. When elements i i i and j j j are combined, they transform into element A i , j A_{i, j} Ai,j if i ≥ j i \geq j i≥j, and into element A j , i A_{j, i} Aj,i if KaTeX parse error: Expected 'EOF', got '&' at position 3: i &̲lt; j.
Starting with element 1 1 1, combine it with elements 1 , 2 , ... , N 1, 2, \ldots, N 1,2,...,N in this order. Find the final element obtained.
Constraints
1 ≤ N ≤ 100 1 \leq N \leq 100 1≤N≤100
1 ≤ A i , j ≤ N 1 \leq A_{i, j} \leq N 1≤Ai,j≤N
All input values are integers.
Input
The input is given from Standard Input in the following format:
N N N
A 1 , 1 A_{1, 1} A1,1
A 2 , 1 A_{2, 1} A2,1 A 2 , 2 A_{2, 2} A2,2
⋮ \vdots ⋮
A N , 1 A_{N, 1} AN,1 A N , 2 A_{N, 2} AN,2 ... \ldots ... A N , N A_{N, N} AN,N
Output
Print the number representing the final element obtained.
Sample Input 1
4
3
2 4
3 1 2
2 1 2 4
Sample Output 1
2
Combining element 1 1 1 with element 1 1 1 results in element 3 3 3.
Combining element 3 3 3 with element 2 2 2 results in element 1 1 1.
Combining element 1 1 1 with element 3 3 3 results in element 3 3 3.
Combining element 3 3 3 with element 4 4 4 results in element 2 2 2.
Therefore, the value to be printed is 2 2 2.
Sample Input 2
5
5
5 5
5 5 5
5 5 5 5
5 5 5 5 5
Sample Output 2
5
Sample Input 3
6
2
1 5
1 6 3
2 6 1 4
2 1 1 1 6
5 6 1 2 2 5
Sample Output 3
5
Solution
具体见文末视频。
Code
cpp
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
std::vector<int> a(n);
std::vector<char> s(n);
for (int i = 0; i < n; i ++)
cin >> a[i] >> s[i];
int res = 1e18;
for (int i = 1; i <= 100; i ++)
for (int j = 1; j <= 100; j ++) {
int lo = i, ro = j, ans = 0;
for (int k = 0; k < n; k ++) {
if (s[k] == 'L') ans += abs(a[k] - lo), lo = a[k];
else ans += abs(a[k] - ro), ro = a[k];
}
res = min(res, ans);
}
cout << res << endl;
return 0;
}
C - Word Ladder
Problem Statement
You are given two strings S S S and T T T consisting of lowercase English letters. Here, S S S and T T T have equal lengths.
Let X X X be an empty array, and repeat the following operation until S S S equals T T T:
Change one character in S S S, and append S S S to the end of X X X.
Find the array of strings X X X with the minimum number of elements obtained in this way. If there are multiple such arrays with the minimum number of elements, find the lexicographically smallest one among them.
What is lexicographical order on arrays of strings? A string S = S_1 S_2 \\ldots S_N of length N is lexicographically smaller than a string T = T_1 T_2 \\ldots T_N of length N if there exists an integer 1 \\leq i \\leq N such that both of the following are satisfied: S_1 S_2 \\ldots S_{i-1} = T_1 T_2 \\ldots T_{i-1} S_i comes earlier than T_i in alphabetical order. An array of strings X = (X_1,X_2,\\ldots,X_M) with M elements is lexicographically smaller than an array of strings Y = (Y_1,Y_2,\\ldots,Y_M) with M elements if there exists an integer 1 \\leq j \\leq M such that both of the following are satisfied: (X_1,X_2,\\ldots,X_{j-1}) = (Y_1,Y_2,\\ldots,Y_{j-1}) X_j is lexicographically smaller than Y_j. ## Constraints
S S S and T T T are strings consisting of lowercase English letters with length between 1 1 1 and 100 100 100, inclusive.
The lengths of S S S and T T T are equal.
Input
The input is given from Standard Input in the following format:
S S S
T T T
Output
Let M M M be the number of elements in the desired array. Print M + 1 M + 1 M+1 lines.
The first line should contain the value of M M M.
The i + 1 i + 1 i+1-th line ( 1 ≤ i ≤ M ) (1 \leq i \leq M) (1≤i≤M) should contain the i i i-th element of the array.
Sample Input 1
adbe
bcbc
Sample Output 1
3
acbe
acbc
bcbc
Initially, S = S = S= adbe
.
We can obtain X = ( X = ( X=( acbe
, , , acbc
, , , bcbc
) ) ) by performing the following operations:
Change S S S to acbe
and append acbe
to the end of X X X.
Change S S S to acbc
and append acbc
to the end of X X X.
Change S S S to bcbc
and append bcbc
to the end of X X X.
Sample Input 2
abcde
abcde
Sample Output 2
0
Sample Input 3
afwgebrw
oarbrenq
Sample Output 3
8
aawgebrw
aargebrw
aarbebrw
aarbebnw
aarbebnq
aarbeenq
aarbrenq
oarbrenq
Solution
具体见文末视频。
Code
cpp
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
string s, t;
cin >> s >> t;
std::vector<string> res;
while (s != t) {
int ok = 0;
for (int i = 0; i < s.size(); i ++)
if (s[i] > t[i]) {
s[i] = t[i], ok = 1;
break;
}
if (ok) {
res.push_back(s);
continue;
}
for (int i = s.size() - 1; i >= 0; i --)
if (s[i] < t[i]) {
s[i] = t[i];
res.push_back(s);
break;
}
}
cout << res.size() << endl;
for (auto v : res)
cout << v << endl;
return 0;
}
D - Cross Explosion
Problem Statement
There is a grid with H H H rows and W W W columns. Let ( i , j ) (i, j) (i,j) denote the cell at the i i i-th row from the top and j j j-th column from the left.
Initially, there is one wall in each cell.
After processing Q Q Q queries explained below in the order they are given, find the number of remaining walls.
In the q q q-th query, you are given two integers R q R_q Rq and C q C_q Cq.
You place a bomb at ( R q , C q ) (R_q, C_q) (Rq,Cq) to destroy walls. As a result, the following process occurs.
If there is a wall at ( R q , C q ) (R_q, C_q) (Rq,Cq), destroy that wall and end the process.
If there is no wall at ( R q , C q ) (R_q, C_q) (Rq,Cq), destroy the first walls that appear when looking up, down, left, and right from ( R q , C q ) (R_q, C_q) (Rq,Cq). More precisely, the following four processes occur simultaneously:
If there exists an i < R q i \lt R_q i<Rq such that a wall exists at ( i , C q ) (i, C_q) (i,Cq) and no wall exists at ( k , C q ) (k, C_q) (k,Cq) for all i < k < R q i \lt k \lt R_q i<k<Rq, destroy the wall at ( i , C q ) (i, C_q) (i,Cq).
If there exists an i > R q i \gt R_q i>Rq such that a wall exists at ( i , C q ) (i, C_q) (i,Cq) and no wall exists at ( k , C q ) (k, C_q) (k,Cq) for all R q < k < i R_q \lt k \lt i Rq<k<i, destroy the wall at ( i , C q ) (i, C_q) (i,Cq).
If there exists a j < C q j \lt C_q j<Cq such that a wall exists at ( R q , j ) (R_q, j) (Rq,j) and no wall exists at ( R q , k ) (R_q, k) (Rq,k) for all j < k < C q j \lt k \lt C_q j<k<Cq, destroy the wall at ( R q , j ) (R_q, j) (Rq,j).
If there exists a j > C q j \gt C_q j>Cq such that a wall exists at ( R q , j ) (R_q, j) (Rq,j) and no wall exists at ( R q , k ) (R_q, k) (Rq,k) for all C q < k < j C_q \lt k \lt j Cq<k<j, destroy the wall at ( R q , j ) (R_q, j) (Rq,j).
Constraints
1 ≤ H , W 1 \leq H, W 1≤H,W
H × W ≤ 4 × 1 0 5 H \times W \leq 4 \times 10^5 H×W≤4×105
1 ≤ Q ≤ 2 × 1 0 5 1 \leq Q \leq 2 \times 10^5 1≤Q≤2×105
1 ≤ R q ≤ H 1 \leq R_q \leq H 1≤Rq≤H
1 ≤ C q ≤ W 1 \leq C_q \leq W 1≤Cq≤W
All input values are integers.
Input
The input is given from Standard Input in the following format:
H H H W W W Q Q Q
R 1 R_1 R1 C 1 C_1 C1
R 2 R_2 R2 C 2 C_2 C2
⋮ \vdots ⋮
R Q R_Q RQ C Q C_Q CQ
Output
Print the number of remaining walls after processing all queries.
Sample Input 1
2 4 3
1 2
1 2
1 3
Sample Output 1
2
The process of handling the queries can be explained as follows:
In the 1st query, ( R 1 , C 1 ) = ( 1 , 2 ) (R_1, C_1) = (1, 2) (R1,C1)=(1,2). There is a wall at ( 1 , 2 ) (1, 2) (1,2), so the wall at ( 1 , 2 ) (1, 2) (1,2) is destroyed.
In the 2nd query, ( R 2 , C 2 ) = ( 1 , 2 ) (R_2, C_2) = (1, 2) (R2,C2)=(1,2). There is no wall at ( 1 , 2 ) (1, 2) (1,2), so the walls at ( 2 , 2 ) , ( 1 , 1 ) , ( 1 , 3 ) (2,2),(1,1),(1,3) (2,2),(1,1),(1,3), which are the first walls that appear when looking up, down, left, and right from ( 1 , 2 ) (1, 2) (1,2), are destroyed.
In the 3rd query, ( R 3 , C 3 ) = ( 1 , 3 ) (R_3, C_3) = (1, 3) (R3,C3)=(1,3). There is no wall at ( 1 , 3 ) (1, 3) (1,3), so the walls at ( 2 , 3 ) , ( 1 , 4 ) (2,3),(1,4) (2,3),(1,4), which are the first walls that appear when looking up, down, left, and right from ( 1 , 3 ) (1, 3) (1,3), are destroyed.
After processing all queries, there are two remaining walls, at ( 2 , 1 ) (2, 1) (2,1) and ( 2 , 4 ) (2, 4) (2,4).
Sample Input 2
5 5 5
3 3
3 3
3 2
2 2
1 2
Sample Output 2
10
Sample Input 3
4 3 10
2 2
4 1
1 1
4 2
2 1
3 1
1 3
1 2
4 3
4 2
Sample Output 3
2
Solution
具体见文末视频。
Code
cpp
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n, m, q;
cin >> n >> m >> q;
vector<set<int>> col(m + 1), lin(n + 1);
std::vector<vector<bool>> st(n + 1, vector<bool>(m + 1, 0));
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++) col[j].insert(i), lin[i].insert(j), st[i][j] = 1;
int res = n * m;
while (q -- ) {
int x, y;
cin >> x >> y;
if (st[x][y]) {
res --, st[x][y] = 0;
col[y].erase(x), lin[x].erase(y);
continue;
}
auto up = col[y].upper_bound(x), down = up, left = lin[x].upper_bound(y), right = left;
if (up != col[y].begin()) {
up --, res --, st[*up][y] = 0;
col[y].erase(up), lin[*up].erase(y);
}
if (down != col[y].end()) {
res --, st[*down][y] = 0;
col[y].erase(down), lin[*down].erase(y);
}
if (left != lin[x].begin()) {
left --, res --, st[x][*left] = 0;
lin[x].erase(left), col[*left].erase(x);
}
if (right != lin[x].end()) {
res --, st[x][*right] = 0;
lin[x].erase(right), col[*right].erase(x);
}
}
cout << res << endl;
return 0;
}
E - Avoid K Partition
Problem Statement
You are given a sequence A = ( A 1 , A 2 , ... , A N ) A = (A_1, A_2, \dots, A_N) A=(A1,A2,...,AN) of length N N N and an integer K K K.
There are 2 N − 1 2^{N-1} 2N−1 ways to divide A A A into several contiguous subsequences. How many of these divisions have no subsequence whose elements sum to K K K? Find the count modulo 998244353 998244353 998244353.
Here, "to divide A A A into several contiguous subsequences" means the following procedure.
Freely choose the number k k k ( 1 ≤ k ≤ N ) (1 \leq k \leq N) (1≤k≤N) of subsequences and an integer sequence ( i 1 , i 2 , ... , i k , i k + 1 ) (i_1, i_2, \dots, i_k, i_{k+1}) (i1,i2,...,ik,ik+1) satisfying 1 = i 1 < i 2 < ⋯ < i k < i k + 1 = N + 1 1 = i_1 \lt i_2 \lt \dots \lt i_k \lt i_{k+1} = N+1 1=i1<i2<⋯<ik<ik+1=N+1.
For each 1 ≤ n ≤ k 1 \leq n \leq k 1≤n≤k, the n n n-th subsequence is formed by taking the i n i_n in-th through ( i n + 1 − 1 ) (i_{n+1} - 1) (in+1−1)-th elements of A A A, maintaining their order.
Here are some examples of divisions for A = ( 1 , 2 , 3 , 4 , 5 ) A = (1, 2, 3, 4, 5) A=(1,2,3,4,5):
( 1 , 2 , 3 ) , ( 4 ) , ( 5 ) (1, 2, 3), (4), (5) (1,2,3),(4),(5)
( 1 , 2 ) , ( 3 , 4 , 5 ) (1, 2), (3, 4, 5) (1,2),(3,4,5)
( 1 , 2 , 3 , 4 , 5 ) (1, 2, 3, 4, 5) (1,2,3,4,5)
Constraints
1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2 \times 10^5 1≤N≤2×105
− 1 0 15 ≤ K ≤ 1 0 15 -10^{15} \leq K \leq 10^{15} −1015≤K≤1015
− 1 0 9 ≤ A i ≤ 1 0 9 -10^9 \leq A_i \leq 10^9 −109≤Ai≤109
All input values are integers.
Input
The input is given from Standard Input in the following format:
$N$ $K$
$A_1$ $A_2$ $\dots$ $A_N$
Output
Print the count modulo 998244353 998244353 998244353.
Sample Input 1
3 3
1 2 3
Sample Output 1
2
There are two divisions that satisfy the condition in the problem statement:
( 1 ) , ( 2 , 3 ) (1), (2, 3) (1),(2,3)
( 1 , 2 , 3 ) (1, 2, 3) (1,2,3)
Sample Input 2
5 0
0 0 0 0 0
Sample Output 2
0
Sample Input 3
10 5
-5 -1 -7 6 -6 -2 -5 10 2 -10
Sample Output 3
428
Solution
具体见文末视频。
Code
cpp
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int mod = 998244353;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n, k;
cin >> n >> k;
std::vector<int> a(n + 1), s(n + 1, 0);
for (int i = 1; i <= n; i ++)
cin >> a[i], s[i] = s[i - 1] + a[i];
std::vector<int> dp(n + 1, 0);
unordered_map<int, int> tot;
dp[0] = 1, tot[0] = 1;
int sum = 1;
for (int i = 1; i <= n; i ++) {
dp[i] = (sum - tot[s[i] - k] + mod) % mod;
(sum += dp[i]) %= mod, (tot[s[i]] += dp[i]) %= mod;
}
cout << dp[n] << endl;
return 0;
}
F - Cake Division
Problem Statement
There is a circular cake divided into N N N pieces by cut lines. Each cut line is a line segment connecting the center of the circle to a point on the arc.
The pieces and cut lines are numbered 1 , 2 , ... , N 1, 2, \ldots, N 1,2,...,N in clockwise order, and piece i i i has a mass of A i A_i Ai. Piece 1 1 1 is also called piece N + 1 N + 1 N+1.
Cut line i i i is between pieces i i i and i + 1 i + 1 i+1, and they are arranged clockwise in this order: piece 1 1 1, cut line 1 1 1, piece 2 2 2, cut line 2 2 2, ... \ldots ..., piece N N N, cut line N N N.
We want to divide this cake among K K K people under the following conditions. Let w i w_i wi be the sum of the masses of the pieces received by the i i i-th person.
Each person receives one or more consecutive pieces.
There are no pieces that no one receives.
Under the above two conditions, min ( w 1 , w 2 , ... , w K ) \min(w_1, w_2, \ldots, w_K) min(w1,w2,...,wK) is maximized.
Find the value of min ( w 1 , w 2 , ... , w K ) \min(w_1, w_2, \ldots, w_K) min(w1,w2,...,wK) in a division that satisfies the conditions, and the number of cut lines that are never cut in the divisions that satisfy the conditions. Here, cut line i i i is considered cut if pieces i i i and i + 1 i + 1 i+1 are given to different people.
Constraints
2 ≤ K ≤ N ≤ 2 × 1 0 5 2 \leq K \leq N \leq 2 \times 10^5 2≤K≤N≤2×105
1 ≤ A i ≤ 1 0 4 1 \leq A_i \leq 10^4 1≤Ai≤104
All input values are integers.
Input
The input is given from Standard Input in the following format:
N N N K K K
A 1 A_1 A1 A 2 A_2 A2 ... \ldots ... A N A_N AN
Output
Let x x x be the value of min ( w 1 , w 2 , ... , w K ) \min(w_1, w_2, \ldots, w_K) min(w1,w2,...,wK) in a division that satisfies the conditions, and y y y be the number of cut lines that are never cut. Print x x x and y y y in this order, separated by a space.
Sample Input 1
5 2
3 6 8 6 4
Sample Output 1
13 1
The following divisions satisfy the conditions:
Give pieces 2 , 3 2, 3 2,3 to one person and pieces 4 , 5 , 1 4, 5, 1 4,5,1 to the other. Pieces 2 , 3 2, 3 2,3 have a total mass of 14 14 14, and pieces 4 , 5 , 1 4, 5, 1 4,5,1 have a total mass of 13 13 13.
Give pieces 3 , 4 3, 4 3,4 to one person and pieces 5 , 1 , 2 5, 1, 2 5,1,2 to the other. Pieces 3 , 4 3, 4 3,4 have a total mass of 14 14 14, and pieces 5 , 1 , 2 5, 1, 2 5,1,2 have a total mass of 13 13 13.
The value of min ( w 1 , w 2 ) \min(w_1, w_2) min(w1,w2) in divisions satisfying the conditions is 13 13 13, and there is one cut line that is not cut in either division: cut line 5 5 5.
Sample Input 2
6 3
4 7 11 3 9 2
Sample Output 2
11 1
Sample Input 3
10 3
2 9 8 1 7 9 1 3 5 8
Sample Output 3
17 4
Solution
具体见文末视频。
Code
cpp
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int N = 4e5 + 10;
int n, m;
int a[N], b[N];
bool check(int x) {
std::vector<vector<int>> go(2 * n + 2, vector<int>(20, 2 * n + 1));
for (int i = 1, j = 1; i <= 2 * n; i ++) {
while (j <= 2 * n && b[j] - b[i - 1] < x) j ++;
if (j > 2 * n) go[i][0] = j;
else go[i][0] = j + 1;
}
for (int j = 1; j < 20; j ++)
for (int i = 1; i <= 2 * n; i ++)
go[i][j] = go[go[i][j - 1]][j - 1];
for (int i = 1; i <= n; i ++) {
int po = i;
for (int j = 19; j >= 0; j --)
if (m >> j & 1) po = go[po][j];
if (po - 1 <= i + n - 1) return 1;
}
return 0;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; i ++)
cin >> a[i], a[i + n] = a[i];
for (int i = 1; i <= 2 * n; i ++) b[i] = b[i - 1] + a[i];
int lo = 1, ro = 4e9, res;
while (lo <= ro) {
int mid = lo + ro >> 1;
if (check(mid)) lo = mid + 1, res = mid;
else ro = mid - 1;
}
cout << res << " ";
int cnt = 0;
std::vector<vector<int>> go(2 * n + 2, vector<int>(20, 2 * n + 1));
for (int i = 1, j = 1; i <= 2 * n; i ++) {
while (j <= 2 * n && b[j] - b[i - 1] < res) j ++;
if (j > 2 * n) go[i][0] = j;
else go[i][0] = j + 1;
}
for (int j = 1; j < 20; j ++)
for (int i = 1; i <= 2 * n; i ++)
go[i][j] = go[go[i][j - 1]][j - 1];
for (int i = 1; i <= n; i ++) {
int po = i;
for (int j = 19; j >= 0; j --)
if (m >> j & 1) po = go[po][j];
if (po - 1 <= i + n - 1) cnt ++;
}
cout << n - cnt << endl;
return 0;
}
视频题解
AtCoder Beginner Contest 370(A ~ F 题讲解)
最后祝大家早日