A - Spoiler
Problem Statement
You are given a string S S S consisting of lowercase English letters and |
. S S S is guaranteed to contain exactly two |
s.
Remove the characters between the two |
s, including the |
s themselves, and print the resulting string.
Constraints
S S S is a string of length between 2 2 2 and 100 100 100, inclusive, consisting of lowercase English letters and |
.
S S S contains exactly two |
s.
Input
The input is given from Standard Input in the following format:
S S S
Output
Print the answer.
Sample Input 1
atcoder|beginner|contest
Sample Output 1
atcodercontest
Remove all the characters between the two |
s and print the result.
Sample Input 2
|spoiler|
Sample Output 2
It is possible that all characters are removed.
Sample Input 3
||xyz
Sample Output 3
xyz
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#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;
cin >> s;
int cnt = 0;
for (auto v : s)
if (v == '|')
cnt ++;
else if (cnt != 1)
cout << v;
return 0;
}
B - Delimiter
Problem Statement
You are given N N N integers A 1 , A 2 , … , A N A_1,A_2,\dots,A_N A1,A2,…,AN, one per line, over N N N lines. However, N N N is not given in the input.
Furthermore, the following is guaranteed:
A i ≠ 0 A_i \neq 0 Ai=0 ( 1 ≤ i ≤ N − 1 1 \le i \le N-1 1≤i≤N−1 )
A N = 0 A_N = 0 AN=0
Print A N , A N − 1 , … , A 1 A_N, A_{N-1},\dots,A_1 AN,AN−1,…,A1 in this order.
Constraints
All input values are integers.
1 ≤ N ≤ 100 1 \le N \le 100 1≤N≤100
1 ≤ A i ≤ 1 0 9 1 \le A_i \le 10^9 1≤Ai≤109 ( 1 ≤ i ≤ N − 1 1 \le i \le N-1 1≤i≤N−1 )
A N = 0 A_N = 0 AN=0
Input
The input is given from Standard Input in the following format:
A 1 A_1 A1
A 2 A_2 A2
⋮ \vdots ⋮
A N A_N AN
Output
Print A N , A N − 1 , … , A 1 A_N, A_{N-1}, \dots, A_1 AN,AN−1,…,A1 in this order, as integers, separated by newlines.
Sample Input 1
3
2
1
0
Sample Output 1
0
1
2
3
Note again that N N N is not given in the input.
Here, N = 4 N=4 N=4 and A = ( 3 , 2 , 1 , 0 ) A=(3,2,1,0) A=(3,2,1,0).
Sample Input 2
0
Sample Output 2
0
A = ( 0 ) A=(0) A=(0).
Sample Input 3
123
456
789
987
654
321
0
Sample Output 3
0
321
654
987
789
456
123
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#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 x;
std::vector<int> a;
cin >> x;
while (x != 0) {
a.emplace_back(x);
cin >> x;
}
a.emplace_back(x);
reverse(a.begin(), a.end());
for (auto v : a)
cout << v << endl;
return 0;
}
C - A+B+C
Problem Statement
You are given three sequences A = ( A 1 , … , A N ) A=(A_1,\ldots,A_N) A=(A1,…,AN), B = ( B 1 , … , B M ) B=(B_1,\ldots,B_M) B=(B1,…,BM), and C = ( C 1 , … , C L ) C=(C_1,\ldots,C_L) C=(C1,…,CL).
Additionally, a sequence X = ( X 1 , … , X Q ) X=(X_1,\ldots,X_Q) X=(X1,…,XQ) is given. For each i = 1 , … , Q i=1,\ldots,Q i=1,…,Q, solve the following problem:
Problem: Is it possible to select one element from each of A A A, B B B, and C C C so that their sum is X i X_i Xi?
Constraints
1 ≤ N , M , L ≤ 100 1 \leq N,M,L \leq 100 1≤N,M,L≤100
0 ≤ A i , B i , C i ≤ 1 0 8 0 \leq A_i, B_i ,C_i \leq 10^8 0≤Ai,Bi,Ci≤108
1 ≤ Q ≤ 2 × 1 0 5 1 \leq Q \leq 2\times 10^5 1≤Q≤2×105
0 ≤ X i ≤ 3 × 1 0 8 0 \leq X_i \leq 3\times 10^8 0≤Xi≤3×108
All input values are integers.
Input
The input is given from Standard Input in the following format:
N N N
A 1 A_1 A1 … \ldots … A N A_N AN
M M M
B 1 B_1 B1 … \ldots … B M B_M BM
L L L
C 1 C_1 C1 … \ldots … C L C_L CL
Q Q Q
X 1 X_1 X1 … \ldots … X Q X_Q XQ
Output
Print Q Q Q lines.
The i i i-th line should contain Yes
if it is possible to select one element from each of A A A, B B B, and C C C so that their sum is X i X_i Xi, and No
otherwise.
Sample Input 1
3
1 2 3
2
2 4
6
1 2 4 8 16 32
4
1 5 10 50
Sample Output 1
No
Yes
Yes
No
It is impossible to select one element from each of A A A, B B B, and C C C so that their sum is 1 1 1.
Selecting 1 1 1, 2 2 2, and 2 2 2 from A A A, B B B, and C C C, respectively, makes the sum 5 5 5.
Selecting 2 2 2, 4 4 4, and 4 4 4 from A A A, B B B, and C C C, respectively, makes the sum 10 10 10.
It is impossible to select one element from each of A A A, B B B, and C C C so that their sum is 50 50 50.
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#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, l;
cin >> n;
std::vector<int> a(n + 1);
for (int i = 1; i <= n; i ++)
cin >> a[i];
cin >> m;
std::vector<int> b(m + 1);
for (int i = 1; i <= m; i ++)
cin >> b[i];
cin >> l;
std::vector<int> c(l + 1);
for (int i = 1; i <= l; i ++)
cin >> c[i];
unordered_map<int, int> vis;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++)
for (int k = 1; k <= l; k ++)
vis[a[i] + b[j] + c[k]] = 1;
int q;
cin >> q;
while (q --)
{
int x;
cin >> x;
if (vis[x]) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
D - String Bags
Problem Statement
You initially have an empty string S S S.
Additionally, there are bags 1 , 2 , … , N 1, 2, \dots, N 1,2,…,N, each containing some strings.
Bag i i i contains A i A_i Ai strings S i , 1 , S i , 2 , … , S i , A i S_{i,1}, S_{i,2}, \dots, S_{i,A_i} Si,1,Si,2,…,Si,Ai.
You will repeat the following steps for i = 1 , 2 , … , N i = 1, 2, \dots, N i=1,2,…,N:
Choose and perform one of the following two actions:
Pay 1 1 1 yen, select exactly one string from bag i i i, and concatenate it to the end of S S S.
Do nothing.
Given a string T T T, find the minimum amount of money required to make the final S S S equal T T T.
If there is no way to make the final S S S equal T T T, print -1
.
Constraints
T T T is a string consisting of lowercase English letters with length between 1 1 1 and 100 100 100, inclusive.
N N N is an integer between 1 1 1 and 100 100 100, inclusive.
A i A_i Ai is an integer between 1 1 1 and 10 10 10, inclusive.
S i , j S_{i,j} Si,j is a string consisting of lowercase English letters with length between 1 1 1 and 10 10 10, inclusive.
Input
The input is given from Standard Input in the following format:
T T T
N N N
A 1 A_1 A1 S 1 , 1 S_{1,1} S1,1 S 1 , 2 S_{1,2} S1,2 … \dots … S 1 , A 1 S_{1,A_1} S1,A1
A 2 A_2 A2 S 2 , 1 S_{2,1} S2,1 S 2 , 2 S_{2,2} S2,2 … \dots … S 2 , A 2 S_{2,A_2} S2,A2
⋮ \vdots ⋮
A N A_N AN S N , 1 S_{N,1} SN,1 S N , 2 S_{N,2} SN,2 … \dots … S N , A N S_{N,A_N} SN,AN
Output
Print the answer as an integer.
Sample Input 1
abcde
3
3 ab abc abcd
4 f c cd bcde
2 e de
Sample Output 1
2
For example, doing the following makes the final S S S equal T T T with two yen, which can be shown to be the minimum amount required.
For i = 1 i=1 i=1, select abc
from bag 1 1 1 and concatenate it to the end of S S S, making S = S= S= abc
.
For i = 2 i=2 i=2, do nothing.
For i = 3 i=3 i=3, select de
from bag 3 3 3 and concatenate it to the end of S S S, making S = S= S= abcde
.
Sample Input 2
abcde
3
2 ab abc
3 f c bcde
1 e
Sample Output 2
-1
There is no way to make the final S S S equal T T T, so print -1
.
Sample Input 3
aaabbbbcccc
6
2 aa aaa
2 dd ddd
2 ab aabb
4 bbaa bbbc bbb bbcc
2 cc bcc
3 ccc cccc ccccc
Sample Output 3
4
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int N = 5e2 + 10;
string final;
int n;
int a[N], f[N][N];
string s[N][N];
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> final;
cin >> n;
int m = final.size();
final = ' ' + final;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
for (int j = 1; j <= a[i]; j ++)
cin >> s[i][j], s[i][j] = ' ' + s[i][j];
}
memset(f, 0x3f, sizeof f);
for (int i = 0; i <= n; i ++) f[i][0] = 0;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= a[i]; j ++) {
for (int k = 1; k <= m - (int)s[i][j].size() + 2; k ++) {
bool flg = 1;
for (int l = 1; l < (int)s[i][j].size(); l ++)
if (final[k + l - 1] != s[i][j][l]) {
flg = 0;
break;
}
if (flg) f[i][k + (int)s[i][j].size() - 2] = min(min(f[i - 1][k + (int)s[i][j].size() - 2], f[i - 1][k - 1] + 1), f[i][k + (int)s[i][j].size() - 2]);
}
for (int k = 1; k <= m; k ++)
f[i][k] = min(f[i][k], f[i - 1][k]);
}
}
if (f[n][m] >= 1e18) cout << -1 << endl;
else cout << f[n][m] << endl;
return 0;
}
E - Insert or Erase
Problem Statement
You are given a sequence A = ( A 1 , … , A N ) A=(A_1,\ldots,A_N) A=(A1,…,AN) of length N N N. The elements of A A A are distinct.
Process Q Q Q queries in the order they are given. Each query is of one of the following two types:
1 x y
: Insert y y y immediately after the element x x x in A A A. It is guaranteed that x x x exists in A A A when this query is given.
2 x
: Remove the element x x x from A A A. It is guaranteed that x x x exists in A A A when this query is given.
It is guaranteed that after processing each query, A A A will not be empty, and its elements will be distinct.
Print A A A after processing all the queries.
Constraints
1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2\times 10^5 1≤N≤2×105
1 ≤ Q ≤ 2 × 1 0 5 1 \leq Q \leq 2\times 10^5 1≤Q≤2×105
1 ≤ A i ≤ 1 0 9 1 \leq A_i \leq 10^9 1≤Ai≤109
A i ≠ A j A_i \neq A_j Ai=Aj
For queries of the first type, 1 ≤ x , y ≤ 1 0 9 1 \leq x,y \leq 10^9 1≤x,y≤109.
When a query of the first type is given, x x x exists in A A A.
For queries of the second type, 1 ≤ x ≤ 1 0 9 1 \leq x \leq 10^9 1≤x≤109.
When a query of the second type is given, x x x exists in A A A.
After processing each query, A A A is not empty, and its elements are distinct.
All input values are integers.
Input
The input is given from Standard Input in the following format:
N N N
A 1 A_1 A1 … \ldots … A N A_N AN
Q Q Q
Q u e r y 1 \mathrm{Query}_1 Query1
⋮ \vdots ⋮
Q u e r y Q \mathrm{Query}_Q QueryQ
Here, Q u e r y i \mathrm{Query}_i Queryi represents the i i i-th query and is given in one of the following formats:
1 1 1 x x x y y y
2 2 2 x x x
Output
Let A = ( A 1 , … , A K ) A=(A_1,\ldots,A_K) A=(A1,…,AK) be the sequence after processing all the queries. Print A 1 , … , A K A_1,\ldots,A_K A1,…,AK in this order, separated by spaces.
Sample Input 1
4
2 1 4 3
4
2 1
1 4 5
2 2
1 5 1
Sample Output 1
4 5 1 3
The queries are processed as follows:
Initially, A = ( 2 , 1 , 4 , 3 ) A=(2,1,4,3) A=(2,1,4,3).
The first query removes 1 1 1, making A = ( 2 , 4 , 3 ) A=(2,4,3) A=(2,4,3).
The second query inserts 5 5 5 immediately after 4 4 4, making A = ( 2 , 4 , 5 , 3 ) A=(2,4,5,3) A=(2,4,5,3).
The third query removes 2 2 2, making A = ( 4 , 5 , 3 ) A=(4,5,3) A=(4,5,3).
The fourth query inserts 1 1 1 immediately after 5 5 5, making A = ( 4 , 5 , 1 , 3 ) A=(4,5,1,3) A=(4,5,1,3).
Sample Input 2
6
3 1 4 5 9 2
7
2 5
1 3 5
1 9 7
2 9
2 3
1 2 3
2 4
Sample Output 2
5 1 7 2 3
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int N = 2e5 + 10;
int n, q;
int a[N];
unordered_map<int, int> lst, nxt;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i ++)
cin >> a[i];
nxt[0] = a[1];
for (int i = 1; i <= n; i ++)
lst[a[i]] = a[i - 1], nxt[a[i]] = a[i + 1];
nxt[a[n]] = -1;
cin >> q;
while (q --) {
int op, x, y;
cin >> op >> x;
if (op == 1) {
cin >> y;
lst[y] = x;
nxt[y] = nxt[x];
lst[nxt[x]] = y;
nxt[x] = y;
}
else {
int last = lst[x], next = nxt[x];
nxt[last] = next, lst[next] = last;
}
}
for (int i = nxt[0]; ~i; i = nxt[i])
cout << i << " ";
return 0;
}
F - Earn to Advance
Problem Statement
There is a grid with N N N rows and N N N columns. Let ( i , j ) (i,j) (i,j) denote the square at the i i i-th row from the top and j j j-th column from the left.
Takahashi is initially at square ( 1 , 1 ) (1,1) (1,1) with zero money.
When Takahashi is at square ( i , j ) (i,j) (i,j), he can perform one of the following in one action:
Stay at the same square and increase his money by P i , j P_{i,j} Pi,j.
Pay R i , j R_{i,j} Ri,j from his money and move to square ( i , j + 1 ) (i,j+1) (i,j+1).
Pay D i , j D_{i,j} Di,j from his money and move to square ( i + 1 , j ) (i+1,j) (i+1,j).
He cannot make a move that would make his money negative or take him outside the grid.
If Takahashi acts optimally, how many actions does he need to reach square ( N , N ) (N,N) (N,N)?
Constraints
2 ≤ N ≤ 80 2 \leq N \leq 80 2≤N≤80
1 ≤ P i , j ≤ 1 0 9 1 \leq P_{i,j} \leq 10^9 1≤Pi,j≤109
1 ≤ R i , j , D i , j ≤ 1 0 9 1 \leq R_{i,j},D_{i,j} \leq 10^9 1≤Ri,j,Di,j≤109
All input values are integers.
Input
The input is given from Standard Input in the following format:
$N$
$P_{1,1}$ $\ldots$ $P_{1,N}$
$\vdots$
$P_{N,1}$ $\ldots$ $P_{N,N}$
$R_{1,1}$ $\ldots$ $R_{1,N-1}$
$\vdots$
$R_{N,1}$ $\ldots$ $R_{N,N-1}$
$D_{1,1}$ $\ldots$ $D_{1,N}$
$\vdots$
$D_{N-1,1}$ $\ldots$ $D_{N-1,N}$
Output
Print the answer.
Sample Input 1
3
1 2 3
3 1 2
2 1 1
1 2
4 3
4 2
1 5 7
5 3 3
Sample Output 1
8
![Figure](https://img-blog.csdnimg.cn/img_convert/ee5c4cb3658d1f7d31147bfce34b11b2.png)
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int N = 85;
int n;
int p[N][N], r[N][N], d[N][N];
int f[N][N][N * N], g[N][N][N * N];
std::vector<int> num;
int id(int x) { return lower_bound(num.begin(), num.end(), x) - num.begin(); }
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
cin >> p[i][j], num.emplace_back(p[i][j]);
for (int i = 1; i <= n; i ++)
for (int j = 1; j < n; j ++)
cin >> r[i][j];
for (int i = 1; i < n; i ++)
for (int j = 1; j <= n; j ++)
cin >> d[i][j];
sort(num.begin(), num.end());
num.erase(unique(num.begin(), num.end()), num.end());
memset(f, 0x3f, sizeof f);
memset(g, -0x3f, sizeof g);
f[1][1][id(p[1][1])] = 0, g[1][1][id(p[1][1])] = 0;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++) {
for (int k = 0; k < num.size(); k ++) {
if (f[i][j][k] >= 1e18) continue;
int ir = max(k, id(p[i][j + 1])), tr = (r[i][j] - g[i][j][k] + num[k] - 1) / num[k],
jd = max(k, id(p[i + 1][j])), td = (d[i][j] - g[i][j][k] + num[k] - 1) / num[k];
auto &right = f[i][j + 1][ir];
auto &down = f[i + 1][j][jd];
if (j < n) {
if (r[i][j] < g[i][j][k]) {
if (right > f[i][j][k] + 1) g[i][j + 1][ir] = g[i][j][k] - r[i][j], right = f[i][j][k] + 1;
else if (right == f[i][j][k] + 1) g[i][j + 1][ir] = max(g[i][j][k] - r[i][j], g[i][j + 1][ir]);
}
if (right > f[i][j][k] + tr + 1) g[i][j + 1][ir] = tr * num[k] - (r[i][j] - g[i][j][k]), right = f[i][j][k] + tr + 1;
if (right == f[i][j][k] + tr + 1 && tr * num[k] - (r[i][j] - g[i][j][k]) > g[i][j + 1][ir]) g[i][j + 1][ir] = tr * num[k] - (r[i][j] - g[i][j][k]);
}
if (i < n) {
if (d[i][j] < g[i][j][k]){
if (down > f[i][j][k] + 1) down = f[i][j][k] + 1, g[i + 1][j][jd] = g[i][j][k] - d[i][j];
else if (down == f[i][j][k] + 1) g[i + 1][j][jd] = max(g[i + 1][j][jd], g[i][j][k] - d[i][j]);
}
if (down > f[i][j][k] + td + 1) g[i + 1][j][jd] = td * num[k] - (d[i][j] - g[i][j][k]), down = f[i][j][k] + td + 1;
if (down == f[i][j][k] + td + 1 && td * num[k] - (d[i][j] - g[i][j][k]) > g[i][j + 1][jd]) g[i + 1][j][jd] = td * num[k] - (d[i][j] - g[i][j][k]);
}
}
}
int res = 1e18;
for (int i = 0; i < num.size(); i ++)
res = min(res, f[n][n][i]);
cout << res << endl;
return 0;
}
视频题解
Atcoder Beginner Contest 344(A ~ F 题讲解)
欢迎大家关注我的B站空间:https://space.bilibili.com/630340560
最后祝大家早日