A - Past ABCs
Problem Statement
You are given a string
S
S
S of length
6
6
6. It is guaranteed that the first three characters of
S
S
S are ABC
and the last three characters are digits.
Determine if
S
S
S is the abbreviation of a contest held and concluded on AtCoder before the start of this contest.
Here, a string
T
T
T is “the abbreviation of a contest held and concluded on AtCoder before the start of this contest” if and only if it equals one of the following
348
348
348 strings:
ABC001
, ABC002
,
…
\ldots
…, ABC314
, ABC315
, ABC317
, ABC318
,
…
\ldots
…, ABC348
, ABC349
.
Note that ABC316
is not included.
Constraints
S
S
S is a string of length
6
6
6 where the first three characters are ABC
and the last three characters are digits.
Input
The input is given from Standard Input in the following format:
S S S
Output
If
S
S
S is the abbreviation of a contest held and concluded on AtCoder before the start of this contest, print Yes
; otherwise, print No
.
Sample Input 1
ABC349
Sample Output 1
Yes
ABC349
is the abbreviation of a contest held and concluded on AtCoder last week.
Sample Input 2
ABC350
Sample Output 2
No
ABC350
is this contest, which has not concluded yet.
Sample Input 3
ABC316
Sample Output 3
No
ABC316
was not held on AtCoder.
Solution
具体见文末视频。
Code
#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;
cin >> s;
s.erase(0, 3);
if (stoi(s) < 350 && s != "316" && s != "000") cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
B - Dentist Aoki
Problem Statement
Takahashi has
N
N
N teeth, one in each of the holes numbered
1
,
2
,
…
,
N
1, 2, \dots, N
1,2,…,N.
Dentist Aoki will perform
Q
Q
Q treatments on these teeth and holes.
In the
i
i
i-th treatment, hole
T
i
T_i
Ti is treated as follows:
If there is a tooth in hole
T
i
T_i
Ti, remove the tooth from hole
T
i
T_i
Ti.
If there is no tooth in hole
T
i
T_i
Ti (i.e., the hole is empty), grow a tooth in hole
T
i
T_i
Ti.
After all treatments are completed, how many teeth does Takahashi have?
Constraints
All input values are integers.
1
≤
N
,
Q
≤
1000
1 \le N, Q \le 1000
1≤N,Q≤1000
1
≤
T
i
≤
N
1 \le T_i \le N
1≤Ti≤N
Input
The input is given from Standard Input in the following format:
N
N
N
Q
Q
Q
T
1
T_1
T1
T
2
T_2
T2
…
\dots
…
T
Q
T_Q
TQ
Output
Print the number of teeth as an integer.
Sample Input 1
30 6
2 9 18 27 18 9
Sample Output 1
28
Initially, Takahashi has
30
30
30 teeth, and Aoki performs six treatments.
In the first treatment, hole
2
2
2 is treated. There is a tooth in hole
2
2
2, so it is removed.
In the second treatment, hole
9
9
9 is treated. There is a tooth in hole
9
9
9, so it is removed.
In the third treatment, hole
18
18
18 is treated. There is a tooth in hole
18
18
18, so it is removed.
In the fourth treatment, hole
27
27
27 is treated. There is a tooth in hole
27
27
27, so it is removed.
In the fifth treatment, hole
18
18
18 is treated. There is no tooth in hole
18
18
18, so a tooth is grown.
In the sixth treatment, hole
9
9
9 is treated. There is no tooth in hole
9
9
9, so a tooth is grown.
The final count of teeth is
28
28
28.
Sample Input 2
1 7
1 1 1 1 1 1 1
Sample Output 2
0
Sample Input 3
9 20
9 5 1 2 2 2 8 9 2 1 6 2 6 5 8 7 8 5 9 8
Sample Output 3
5
Solution
具体见文末视频。
Code
#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 = 1e3 + 10;
int n, q;
int cnt[N];
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i <= n; i ++)
cnt[i] = 1;
while (q --) {
int tooth;
cin >> tooth;
cnt[tooth] ^= 1;
}
int res = 0;
for (int i = 1; i <= n; i ++)
res += cnt[i];
cout << res << endl;
return 0;
}
C - Sort
Problem Statement
You are given a permutation
A
=
(
A
1
,
…
,
A
N
)
A=(A_1,\ldots,A_N)
A=(A1,…,AN) of
(
1
,
2
,
…
,
N
)
(1,2,\ldots,N)
(1,2,…,N).
Transform
A
A
A into
(
1
,
2
,
…
,
N
)
(1,2,\ldots,N)
(1,2,…,N) by performing the following operation between
0
0
0 and
N
−
1
N-1
N−1 times, inclusive:
Operation: Choose any pair of integers
(
i
,
j
)
(i,j)
(i,j) such that KaTeX parse error: Expected 'EOF', got '&' at position 9: 1\leq i &̲lt; j \leq N. Swap the elements at the
i
i
i-th and
j
j
j-th positions of
A
A
A.
It can be proved that under the given constraints, it is always possible to transform
A
A
A into
(
1
,
2
,
…
,
N
)
(1,2,\ldots,N)
(1,2,…,N).
Constraints
2
≤
N
≤
2
×
1
0
5
2 \leq N \leq 2\times 10^5
2≤N≤2×105
(
A
1
,
…
,
A
N
)
(A_1,\ldots,A_N)
(A1,…,AN) is a permutation of
(
1
,
2
,
…
,
N
)
(1,2,\ldots,N)
(1,2,…,N).
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
Output
Let
K
K
K be the number of operations. Print
K
+
1
K+1
K+1 lines.
The first line should contain
K
K
K.
The
(
l
+
1
)
(l+1)
(l+1)-th line (
1
≤
l
≤
K
1\leq l \leq K
1≤l≤K) should contain the integers
i
i
i and
j
j
j chosen for the
l
l
l-th operation, separated by a space.
Any output that satisfies the conditions in the problem statement will be considered correct.
Sample Input 1
5
3 4 1 2 5
Sample Output 1
2
1 3
2 4
The operations change the sequence as follows:
Initially,
A
=
(
3
,
4
,
1
,
2
,
5
)
A=(3,4,1,2,5)
A=(3,4,1,2,5).
The first operation swaps the first and third elements, making
A
=
(
1
,
4
,
3
,
2
,
5
)
A=(1,4,3,2,5)
A=(1,4,3,2,5).
The second operation swaps the second and fourth elements, making
A
=
(
1
,
2
,
3
,
4
,
5
)
A=(1,2,3,4,5)
A=(1,2,3,4,5).
Other outputs such as the following are also considered correct:
4
2 3
3 4
1 2
2 3
Sample Input 2
4
1 2 3 4
Sample Output 2
0
Sample Input 3
3
3 1 2
Sample Output 3
2
1 2
2 3
Solution
具体见文末视频。
Code
#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 = 2e5 + 10;
int n;
int a[N], p[N];
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], p[a[i]] = i;
std::vector<PII> res;
for (int i = 1; i <= n; i ++) {
if (p[i] == i) continue;
res.emplace_back(i, p[i]);
int pos = p[i];
swap(p[i], p[a[i]]), swap(a[i], a[pos]);
}
cout << res.size() << endl;
for (auto v : res)
cout << v.fi << " " << v.se << endl;
return 0;
}
D - New Friends
Problem Statement
There is an SNS used by
N
N
N users, labeled with numbers from
1
1
1 to
N
N
N.
In this SNS, two users can become friends with each other.
Friendship is bidirectional; if user X is a friend of user Y, user Y is always a friend of user X.
Currently, there are
M
M
M pairs of friendships on the SNS, with the
i
i
i-th pair consisting of users
A
i
A_i
Ai and
B
i
B_i
Bi.
Determine the maximum number of times the following operation can be performed:
Operation: Choose three users X, Y, and Z such that X and Y are friends, Y and Z are friends, but X and Z are not. Make X and Z friends.
Constraints
2
≤
N
≤
2
×
1
0
5
2 \leq N \leq 2 \times 10^5
2≤N≤2×105
0
≤
M
≤
2
×
1
0
5
0 \leq M \leq 2 \times 10^5
0≤M≤2×105
KaTeX parse error: Expected 'EOF', got '&' at position 12: 1 \leq A_i &̲lt; B_i \leq N
The pairs
(
A
i
,
B
i
)
(A_i, B_i)
(Ai,Bi) are distinct.
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
N
N
M
M
M
A
1
A_1
A1
B
1
B_1
B1
⋮
\vdots
⋮
A
M
A_M
AM
B
M
B_M
BM
Output
Print the answer.
Sample Input 1
4 3
1 2
2 3
1 4
Sample Output 1
3
Three new friendships with a friend’s friend can occur as follows:
User
1
1
1 becomes friends with user
3
3
3, who is a friend of their friend (user
2
2
2)
User
3
3
3 becomes friends with user
4
4
4, who is a friend of their friend (user
1
1
1)
User
2
2
2 becomes friends with user
4
4
4, who is a friend of their friend (user
1
1
1)
There will not be four or more new friendships.
Sample Input 2
3 0
Sample Output 2
0
If there are no initial friendships, no new friendships can occur.
Sample Input 3
10 8
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10
Sample Output 3
12
Solution
具体见文末视频。
Code
#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 = 2e5 + 10;
int n, m;
int p[N], sz[N], cnt[N];
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; i ++)
p[i] = i, sz[i] = 1;
for (int i = 1; i <= m; i ++) {
int u, v;
cin >> u >> v;
int f1 = find(u), f2 = find(v);
if (f1 == f2) {
cnt[f1] ++;
} else {
p[f1] = f2;
sz[f2] += sz[f1];
cnt[f2] += cnt[f1];
}
}
int res = 0;
for (int i = 1; i <= n; i ++)
if (find(i) == i) {
res += sz[i] * (sz[i] - 1) / 2 - sz[i] + 1 - cnt[i];
}
cout << res << endl;
return 0;
}
E - Toward 0
Problem Statement
You are given an integer
N
N
N. You can perform the following two types of operations:
Pay
X
X
X yen to replace
N
N
N with
⌊
N
A
⌋
\displaystyle\left\lfloor\frac{N}{A}\right\rfloor
⌊AN⌋.
Pay
Y
Y
Y yen to roll a die (dice) that shows an integer between
1
1
1 and
6
6
6, inclusive, with equal probability. Let
b
b
b be the outcome of the die, and replace
N
N
N with
⌊
N
b
⌋
\displaystyle\left\lfloor\frac{N}{b}\right\rfloor
⌊bN⌋.
Here,
⌊
s
⌋
\lfloor s \rfloor
⌊s⌋ denotes the greatest integer less than or equal to
s
s
s. For example,
⌊
3
⌋
=
3
\lfloor 3 \rfloor=3
⌊3⌋=3 and
⌊
2.5
⌋
=
2
\lfloor 2.5 \rfloor=2
⌊2.5⌋=2.
Determine the minimum expected cost paid before
N
N
N becomes
0
0
0 when optimally choosing operations.
The outcome of the die in each operation is independent of other rolls, and the choice of operation can be made after observing the results of the previous operations.
Constraints
1
≤
N
≤
1
0
18
1 \leq N \leq 10^{18}
1≤N≤1018
2
≤
A
≤
6
2 \leq A \leq 6
2≤A≤6
1
≤
X
,
Y
≤
1
0
9
1 \leq X, Y \leq 10^9
1≤X,Y≤109
All input values are integers.
Input
The input is given from Standard Input in the following format:
N N N A A A X X X Y Y Y
Output
Print the answer.
Your output will be considered correct if the absolute or relative error from the true answer is at most
1
0
−
6
10^{-6}
10−6.
Sample Input 1
3 2 10 20
Sample Output 1
20.000000000000000
The available operations are as follows:
Pay
10
10
10 yen. Replace
N
N
N with
⌊
N
2
⌋
\displaystyle\left\lfloor\frac{N}{2}\right\rfloor
⌊2N⌋.
Pay
20
20
20 yen. Roll a die. Let
b
b
b be the outcome, and replace
N
N
N with
⌊
N
b
⌋
\displaystyle\left\lfloor\frac{N}{b}\right\rfloor
⌊bN⌋.
The optimal strategy is to perform the first operation twice.
Sample Input 2
3 2 20 20
Sample Output 2
32.000000000000000
The available operations are as follows:
Pay
20
20
20 yen. Replace
N
N
N with
⌊
N
2
⌋
\displaystyle\left\lfloor\frac{N}{2}\right\rfloor
⌊2N⌋.
Pay
20
20
20 yen. Roll a die. Let
b
b
b be the outcome, and replace
N
N
N with
⌊
N
b
⌋
\displaystyle\left\lfloor\frac{N}{b}\right\rfloor
⌊bN⌋.
The optimal strategy is as follows:
First, perform the second operation to roll the die.
If the outcome is
4
4
4 or greater, then
N
N
N becomes
0
0
0.
If the outcome is
2
2
2 or
3
3
3, then
N
N
N becomes
1
1
1. Now, perform the first operation to make
N
=
0
N = 0
N=0.
If the outcome is
1
1
1, restart from the beginning.
Sample Input 3
314159265358979323 4 223606797 173205080
Sample Output 3
6418410657.7408381
Solution
具体见文末视频。
Code
#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;
int n, a, x, y;
unordered_map<int, double> f;
double DP(int m) {
if (!m) return 0;
if (f.count(m)) return f[m];
double sum = y;
for (int i = 2; i <= 6; i ++)
sum += DP(m / i) + y;
sum /= 5;
f[m] = min(sum, DP(m / a) + x);
return f[m];
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n >> a >> x >> y;
printf("%.8lf\n", DP(n));
return 0;
}
F - Transpose
Problem Statement
You are given a string
S
=
S
1
S
2
S
3
…
S
∣
S
∣
S = S_1 S_2 S_3 \dots S_{|S|}
S=S1S2S3…S∣S∣ consisting of uppercase and lowercase English letters, (
, and )
.
The parentheses in the string
S
S
S are properly matched.
Repeat the following operation until no more operations can be performed:
First, select one pair of integers
(
l
,
r
)
(l, r)
(l,r) that satisfies all of the following conditions:
KaTeX parse error: Expected 'EOF', got '&' at position 3: l &̲lt; r
S
l
=
S_l =
Sl= (
S
r
=
S_r =
Sr= )
Each of the characters
S
l
+
1
,
S
l
+
2
,
…
,
S
r
−
1
S_{l+1}, S_{l+2}, \dots, S_{r-1}
Sl+1,Sl+2,…,Sr−1 is an uppercase or lowercase English letter.
Let
T
=
S
r
−
1
S
r
−
2
…
S
l
+
1
‾
T = \overline{S_{r-1} S_{r-2} \dots S_{l+1}}
T=Sr−1Sr−2…Sl+1.
Here,
x
‾
\overline{x}
x denotes the string obtained by toggling the case of each character in
x
x
x (uppercase to lowercase and vice versa).
Then, delete the
l
l
l-th through
r
r
r-th characters of
S
S
S and insert
T
T
T at the position where the deletion occurred.
Refer to the sample inputs and outputs for clarification.
It can be proved that it is possible to remove all (
s and )
s from the string using the above operations, and the final string is independent of how and in what order the operations are performed.
Determine the final string.
(
, $A$,
)
in this order. There exist non-empty correct parenthesis sequences $A$ and $B$, and the string is formed by concatenating $A$ and $B$ in this order. The parentheses in $S$ are properly matched if and only if the
(
s and
)
s extracted from $S$ without changing the order form a correct parenthesis sequence. ## Constraints
1
≤
∣
S
∣
≤
5
×
1
0
5
1 \le |S| \le 5 \times 10^5
1≤∣S∣≤5×105
S
S
S consists of uppercase and lowercase English letters, (
, and )
.
The parentheses in
S
S
S are properly matched.
Input
The input is given from Standard Input in the following format:
S S S
Output
Print the final string.
Sample Input 1
((A)y)x
Sample Output 1
YAx
Let us perform the operations for
S
=
S =
S= ((A)y)x
.
Choose
l
=
2
l=2
l=2 and
r
=
4
r=4
r=4. The substring (A)
is removed and replaced with a
.
After this operation,
S
=
S =
S= (ay)x
.
Choose
l
=
1
l=1
l=1 and
r
=
4
r=4
r=4. The substring (ay)
is removed and replaced with YA
.
After this operation,
S
=
S =
S= YAx
.
After removing the parentheses, the string becomes YAx
, which should be printed.
Sample Input 2
((XYZ)n(X(y)Z))
Sample Output 2
XYZNXYZ
Let us perform the operations for
S
=
S =
S= ((XYZ)n(X(y)Z))
.
Choose
l
=
10
l=10
l=10 and
r
=
12
r=12
r=12. The substring (y)
is removed and replaced with Y
.
After this operation,
S
=
S =
S= ((XYZ)n(XYZ))
.
Choose
l
=
2
l=2
l=2 and
r
=
6
r=6
r=6. The substring (XYZ)
is removed and replaced with zyx
.
After this operation,
S
=
S =
S= (zyxn(XYZ))
.
Choose
l
=
6
l=6
l=6 and
r
=
10
r=10
r=10. The substring (XYZ)
is removed and replaced with zyx
.
After this operation,
S
=
S =
S= (zyxnzyx)
.
Choose
l
=
1
l=1
l=1 and
r
=
9
r=9
r=9. The substring (zyxnzyx)
is removed and replaced with XYZNXYZ
.
After this operation,
S
=
S =
S= XYZNXYZ
.
After removing the parentheses, the string becomes XYZNXYZ
, which should be printed.
Sample Input 3
(((()))(()))(())
Sample Output 3
The final outcome may be an empty string.
Sample Input 4
dF(qT(plC())NnnfR(GsdccC))PO()KjsiI((ysA)eWW)ve
Sample Output 4
dFGsdccCrFNNnplCtQPOKjsiIwwEysAve
Solution
后期补一下这题目的视频
Code
#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 = 5e5 + 10;
int n;
string s, tmp, ans = " ";
struct Splay {
int s[2], p, val;
int sz, flg;
void init(int _val, int _p) { p = _p, val = _val, sz = 1; }
}tr[N];
int root, idx;
void pushup(int u) {
tr[u].sz = tr[tr[u].s[0]].sz + 1 + tr[tr[u].s[1]].sz;
}
void pushdown(int u) {
if (tr[u].flg) {
swap(tr[u].s[0], tr[u].s[1]);
tr[tr[u].s[0]].flg ^= 1;
tr[tr[u].s[1]].flg ^= 1;
tr[u].flg = 0;
}
}
void rotate(int x) {
int y = tr[x].p, z = tr[y].p;
int k = tr[y].s[1] == x;
tr[x].p = z, tr[z].s[tr[z].s[1] == y] = x;
tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y;
tr[x].s[k ^ 1] = y, tr[y].p = x;
pushup(y), pushup(x);
}
void splay(int x, int k) {
while (tr[x].p != k) {
int y = tr[x].p, z = tr[y].p;
if (z != k) {
if ((tr[z].s[0] == y) ^ (tr[y].s[0] == x)) rotate(x);
else rotate(y);
}
rotate(x);
}
if (!k) root = x;
}
void insert(int val) {
int u = root, p = 0;
while (u) p = u, u = tr[u].s[val > tr[u].val];
u = ++ idx;
if (p) tr[p].s[val > tr[p].val] = u;
tr[u].init(val, p);
splay(u, 0);
}
int get_k(int k) {
int u = root;
while (1) {
pushdown(u);
if (tr[tr[u].s[0]].sz >= k) u = tr[u].s[0];
else if (tr[tr[u].s[0]].sz + 1 == k) return u;
else k -= tr[tr[u].s[0]].sz + 1, u = tr[u].s[1];
}
}
void output(int u) {
pushdown(u);
if (tr[u].s[0]) output(tr[u].s[0]);
if (tr[u].val >= 1 && tr[u].val <= n) cout << ans[tr[u].val];
if (tr[u].s[1]) output(tr[u].s[1]);
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> s;
n = s.size(), s = ' ' + s;
int cnt = 0;
std::vector<int> l;
std::vector<PII> seg;
for (int i = 1; i <= n; i ++) {
if (s[i] == '(') {
if (cnt & 1) {
for (auto &v : tmp)
if (v >= 'A' && v <= 'Z')
v = v - 'A' + 'a';
else
v = v - 'a' + 'A';
}
ans += tmp, tmp.clear();
l.emplace_back(ans.size());
cnt ++;
}
else if (isalpha(s[i])) {
tmp += s[i];
} else {
if (cnt & 1) {
for (auto &v : tmp)
if (v >= 'A' && v <= 'Z')
v = v - 'A' + 'a';
else
v = v - 'a' + 'A';
}
ans += tmp, tmp.clear();
seg.emplace_back(l.back(), ans.size() - 1);
cnt --, l.pop_back();
}
}
if (tmp.size()) ans += tmp;
n = ans.size() - 1;
// for (auto v : seg)
// cout << v.fi << " " << v.se << endl;
for (int i = 0; i <= n + 1; i ++)
insert(i);
for (auto v : seg) {
int l = v.fi, r = v.se;
l = get_k(l), r = get_k(r + 2);
splay(r, 0), splay(l, r);
tr[tr[l].s[1]].flg ^= 1;
}
output(root);
return 0;
}
视频题解
Atcoder Beginner Contest 350(A ~ F 题)
最后祝大家早日