A - 11/22 String
Problem Statement
The definition of an 11/22 string in this problem is the same as in Problems C and E.A string $T$ is called an 11/22 string when it satisfies all of the following conditions: $|T|$ is odd. Here, $|T|$ denotes the length of $T$. The $1$-st through $(\frac{|T|+1}{2} - 1)$-th characters are all
1
. The $(\frac{|T|+1}{2})$-th character is
/
. The $(\frac{|T|+1}{2} + 1)$-th through $|T|$-th characters are all
2
. For example,
11/22
,
111/222
, and
/
are 11/22 strings, but
1122
,
1/22
,
11/2222
,
22/11
, and
//2/2/211
are not. Given a string $S$ of length $N$ consisting of
1
,
2
, and
/
, determine whether $S$ is an 11/22 string. ## Constraints
1
≤
N
≤
100
1 \leq N \leq 100
1≤N≤100
S
S
S is a string of length
N
N
N consisting of 1
, 2
, and /
.
Input
The input is given from Standard Input in the following format:
N
N
N
S
S
S
Output
If
S
S
S is an 11/22 string, print Yes
; otherwise, print No
.
Sample Input 1
5
11/22
Sample Output 1
Yes
11/22
satisfies the conditions for an 11/22 string in the problem statement.
Sample Input 2
1
/
Sample Output 2
Yes
/
satisfies the conditions for an 11/22 string.
Sample Input 3
4
1/22
Sample Output 3
No
1/22
does not satisfy the conditions for an 11/22 string.
Sample Input 4
5
22/11
Sample Output 4
No
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);
int n;
string s;
cin >> n >> s;
if (n % 2 == 0) {
cout << "No" << endl;
return 0;
}
bool ok = 1;
for (int i = 0; i < n / 2; i ++) ok &= (s[i] == '1');
ok &= s[n / 2] == '/';
for (int i = n / 2 + 1; i < n; i ++) ok &= (s[i] == '2');
if (ok) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
B - 1122 String
Problem Statement
A string
T
T
T is called a 1122 string if and only if it satisfies all of the following three conditions:
∣
T
∣
\lvert T \rvert
∣T∣ is even. Here,
∣
T
∣
\lvert T \rvert
∣T∣ denotes the length of
T
T
T.
For each integer
i
i
i satisfying
1
≤
i
≤
∣
T
∣
2
1\leq i\leq \frac{|T|}{2}
1≤i≤2∣T∣, the
(
2
i
−
1
)
(2i-1)
(2i−1)-th and
2
i
2i
2i-th characters of
T
T
T are equal.
Each character appears in
T
T
T exactly zero or two times. That is, every character contained in
T
T
T appears exactly twice in
T
T
T.
Given a string
S
S
S consisting of lowercase English letters, print Yes
if
S
S
S is a 1122 string, and No
otherwise.
Constraints
S S S is a string of length between 1 1 1 and 100 100 100, inclusive, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S S S
Output
If
S
S
S is a 1122 string, print Yes
; otherwise, print No
.
Sample Input 1
aabbcc
Sample Output 1
Yes
S
=
S=
S=aabbcc
satisfies all the conditions for a 1122 string, so print Yes
.
Sample Input 2
aab
Sample Output 2
No
S
=
S=
S=aab
has an odd length and does not satisfy the first condition, so print No
.
Sample Input 3
zzzzzz
Sample Output 3
No
S
=
S=
S=zzzzzz
contains six z
s and does not satisfy the third condition, so print No
.
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;
if (s.size() & 1) {
cout << "No" << endl;
return 0;
}
unordered_map<char, int> vis;
for (int i = 0; i < s.size() / 2; i ++)
if (s[i << 1] != s[i << 1 | 1] || vis[s[i << 1]]) {
cout << "No" << endl;
return 0;
} else vis[s[i << 1]] = 1;
cout << "Yes" << endl;
return 0;
}
C - 11/22 Substring
Problem Statement
The definition of an 11/22 string in this problem is the same as in Problems A and E.A string $T$ is called an 11/22 string when it satisfies all of the following conditions: $|T|$ is odd. Here, $|T|$ denotes the length of $T$. The $1$-st through $(\frac{|T|+1}{2} - 1)$-th characters are all
1
. The $(\frac{|T|+1}{2})$-th character is
/
. The $(\frac{|T|+1}{2} + 1)$-th through $|T|$-th characters are all
2
. For example,
11/22
,
111/222
, and
/
are 11/22 strings, but
1122
,
1/22
,
11/2222
,
22/11
, and
//2/2/211
are not. You are given a string $S$ of length $N$ consisting of
1
,
2
, and
/
, where $S$ contains at least one
/
.
Find the maximum length of a (contiguous) substring of $S$ that is an 11/22 string. ## Constraints
1
≤
N
≤
2
×
1
0
5
1 \leq N \leq 2 \times 10^5
1≤N≤2×105
S
S
S is a string of length
N
N
N consisting of 1
, 2
, and /
.
S
S
S contains at least one /
.
Input
The input is given from Standard Input in the following format:
N
N
N
S
S
S
Output
Print the maximum length of a (contiguous) substring of S S S that is an 11/22 string.
Sample Input 1
8
211/2212
Sample Output 1
5
The substring from the
2
2
2-nd to
6
6
6-th character of
S
S
S is 11/22
, which is an 11/22 string. Among all substrings of
S
S
S that are 11/22 strings, this is the longest. Therefore, the answer is
5
5
5.
Sample Input 2
5
22/11
Sample Output 2
1
Sample Input 3
22
/1211/2///2111/2222/11
Sample Output 3
7
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);
int n;
string s;
cin >> n >> s;
vector<int> pre(n), suf(n + 1);
for (int i = 0; i < n; i ++)
if (s[i] == '1') pre[i] = (!i ? 0 : pre[i - 1]) + 1;
else pre[i] = 0;
for (int i = n - 1; i >= 0; i --)
if (s[i] == '2') suf[i] = suf[i + 1] + 1;
else suf[i] = 0;
int res = 0;
for (int i = 0; i < n; i ++)
if (s[i] == '/') {
if (!i || i == n - 1) res = max(res, 1ll);
else res = max(res, min(pre[i - 1], suf[i + 1]) * 2 + 1);
}
cout << res << endl;
return 0;
}
D - 1122 Substring
Problem Statement
A sequence
X
=
(
X
1
,
X
2
,
…
)
X = (X_1, X_2, \ldots)
X=(X1,X2,…) of positive integers (possibly empty) is called a 1122 sequence if and only if it satisfies all of the following three conditions: (The definition of a 1122 sequence is the same as in Problem F.)
∣
X
∣
\lvert X \rvert
∣X∣ is even. Here,
∣
X
∣
\lvert X \rvert
∣X∣ denotes the length of
X
X
X.
For each integer
i
i
i satisfying
1
≤
i
≤
∣
X
∣
2
1\leq i\leq \frac{|X|}{2}
1≤i≤2∣X∣,
X
2
i
−
1
X_{2i-1}
X2i−1 and
X
2
i
X_{2i}
X2i are equal.
Each positive integer appears in
X
X
X either not at all or exactly twice. That is, every positive integer contained in
X
X
X appears exactly twice in
X
X
X.
Given a sequence
A
=
(
A
1
,
A
2
,
…
,
A
N
)
A = (A_1, A_2, \ldots, A_N)
A=(A1,A2,…,AN) of length
N
N
N consisting of positive integers, print the maximum length of a (contiguous) subarray of
A
A
A that is a 1122 sequence.
Constraints
1
≤
N
≤
2
×
1
0
5
1\leq N \leq 2 \times 10^5
1≤N≤2×105
1
≤
A
i
≤
N
1\leq A_i \leq N
1≤Ai≤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
A
2
A_2
A2
…
\ldots
…
A
N
A_N
AN
Output
Print the maximum length of a (contiguous) subarray of A A A that is a 1122 sequence.
Sample Input 1
8
2 3 1 1 2 2 1 1
Sample Output 1
4
For example, taking the subarray from the
3
3
3-rd to
6
6
6-th elements of
A
A
A, we get
(
1
,
1
,
2
,
2
)
(1, 1, 2, 2)
(1,1,2,2), which is a 1122 sequence of length
4
4
4.
There is no longer (contiguous) subarray that satisfies the conditions for a 1122 sequence, so the answer is
4
4
4.
Sample Input 2
3
1 2 2
Sample Output 2
2
Sample Input 3
1
1
Sample Output 3
0
Note that a sequence of length 0 0 0 also satisfies the conditions for a 1122 sequence.
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);
int n;
cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i ++)
cin >> a[i], a[i] --;
int res = 0;
for (int turn = 0; turn < 2; turn ++) {
std::vector<int> vis(n, 0);
for (int i = turn, j = -1; i < n; i += 2) {
j = max(j, i - 1);
while (j + 2 < n && a[j + 2] == a[j + 1] && !vis[a[j + 1]]) j += 2, vis[a[j]] = 1;
res = max(res, j - i + 1);
if (i + 1 < n) vis[a[i]] = 0, vis[a[i + 1]] = 0;
}
}
cout << res << endl;
return 0;
}
E - 11/22 Subsequence
Problem Statement
The definition of an 11/22 string in this problem is the same as in Problems A and C.A string $T$ is called an 11/22 string when it satisfies all of the following conditions: $|T|$ is odd. Here, $|T|$ denotes the length of $T$. The $1$-st through $(\frac{|T|+1}{2} - 1)$-th characters are all
1
. The $(\frac{|T|+1}{2})$-th character is
/
. The $(\frac{|T|+1}{2} + 1)$-th through $|T|$-th characters are all
2
. For example,
11/22
,
111/222
, and
/
are 11/22 strings, but
1122
,
1/22
,
11/2222
,
22/11
, and
//2/2/211
are not. Given a string $S$ of length $N$ consisting of
1
,
2
, and
/
, process $Q$ queries. Each query provides two integers $L$ and $R$. Let $T$ be the
(contiguous) substring of $S$ from the $L$-th through $R$-th character. Find the maximum length of a subsequence
(not necessarily contiguous) of $T$ that is an 11/22 string. If no such subsequence exists, print
0
. ## Constraints
1
≤
N
≤
1
0
5
1 \leq N \leq 10^5
1≤N≤105
1
≤
Q
≤
1
0
5
1 \leq Q \leq 10^5
1≤Q≤105
S
S
S is a string of length
N
N
N consisting of 1
, 2
, and /
.
1
≤
L
≤
R
≤
N
1 \leq L \leq R \leq N
1≤L≤R≤N
N
N
N,
Q
Q
Q,
L
L
L, and
R
R
R are integers.
Input
The input is given from Standard Input in the following format. Here, q u e r y i \mathrm{query}_i queryi denotes the i i i-th query.
N
N
N
Q
Q
Q
S
S
S
q
u
e
r
y
1
\mathrm{query}_1
query1
q
u
e
r
y
2
\mathrm{query}_2
query2
⋮
\vdots
⋮
q
u
e
r
y
Q
\mathrm{query}_Q
queryQ
Each query is given in the following format:
L L L R R R
Output
Print Q Q Q lines. The i i i-th line should contain the answer to the i i i-th query.
Sample Input 1
12 5
111/212/1122
1 7
9 12
3 6
4 10
1 12
Sample Output 1
5
0
3
1
7
For the first query, the substring from the
1
1
1-st to
7
7
7-th character of
S
S
S is 111/212
. This string contains 11/22
as a subsequence, which is the longest subsequence that is an 11/22 string. Therefore, the answer is
5
5
5.
For the second query, the substring from the
9
9
9-th to
12
12
12-th character of
S
S
S is 1122
. This string does not contain any subsequence that is an 11/22 string, so the answer is
0
0
0.
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);
int n, q;
string s;
cin >> n >> q >> s, s = ' ' + s;
std::vector<int> A(n + 1), B(n + 2), C(n + 2);
for (int i = 1; i <= n; i ++)
A[i] = A[i - 1] + (s[i] == '1');
for (int i = n; i >= 1; i --)
B[i] = B[i + 1] + (s[i] == '2');
for (int i = 1; i <= n; i ++)
C[i] = C[i - 1] + (s[i] == '/');
while (q -- ) {
int L, R;
cin >> L >> R;
int lo = 0, ro = n, res = -1;
while (lo <= ro) {
int x = lo + ro >> 1;
int I1 = lower_bound(A.begin(), A.end(), x + A[L - 1]) - A.begin();
if (I1 + 1 > n) {
ro = x - 1;
continue;
}
int l = 1, r = n, I2 = -1;
while (l <= r) {
int mid = l + r >> 1;
if (B[mid + 1] >= B[R + 1] + x) l = mid + 1, I2 = mid;
else r = mid - 1;
}
if (I2 == -1) {
ro = x - 1;
continue;
}
if (C[min(I2, R)] - C[max(I1, L - 1)] > 0) lo = x + 1, res = x;
else ro = x - 1;
}
if (res == -1) cout << 0 << endl;
else cout << res * 2 + 1 << endl;
}
return 0;
}
F - 1122 Subsequence
Problem Statement
A sequence
X
=
(
X
1
,
X
2
,
…
)
X = (X_1, X_2, \ldots)
X=(X1,X2,…) of positive integers (possibly empty) is called a 1122 sequence if and only if it satisfies all of the following three conditions: (The definition of a 1122 sequence is the same as in Problem D.)
∣
X
∣
\lvert X \rvert
∣X∣ is even. Here,
∣
X
∣
\lvert X \rvert
∣X∣ denotes the length of
X
X
X.
For each integer
i
i
i satisfying
1
≤
i
≤
∣
X
∣
2
1\leq i\leq \frac{|X|}{2}
1≤i≤2∣X∣,
X
2
i
−
1
X_{2i-1}
X2i−1 and
X
2
i
X_{2i}
X2i are equal.
Each positive integer appears in
X
X
X either not at all or exactly twice. That is, every positive integer contained in
X
X
X appears exactly twice in
X
X
X.
Given a sequence
A
=
(
A
1
,
A
2
,
…
,
A
N
)
A = (A_1, A_2, \ldots, A_N)
A=(A1,A2,…,AN) of length
N
N
N consisting of positive integers, print the maximum length of a subsequence (not necessarily contiguous) of
A
A
A that is a 1122 sequence.
Constraints
1
≤
N
≤
2
×
1
0
5
1\leq N \leq 2 \times 10^5
1≤N≤2×105
1
≤
A
i
≤
20
1\leq A_i \leq 20
1≤Ai≤20
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
A
2
A_2
A2
…
\ldots
…
A
N
A_N
AN
Output
Print the maximum length of a (not necessarily contiguous) subsequence of A A A that is a 1122 sequence.
Sample Input 1
7
1 3 3 1 2 2 1
Sample Output 1
4
For example, choosing the
1
1
1-st,
4
4
4-th,
5
5
5-th, and
6
6
6-th elements of
A
A
A, we get
(
1
,
1
,
2
,
2
)
(1, 1, 2, 2)
(1,1,2,2), which is a 1122 sequence of length
4
4
4.
There is no longer subsequence that satisfies the conditions for a 1122 sequence, so the answer is
4
4
4.
Sample Input 2
1
20
Sample Output 2
0
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 pt[20][200000];
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
std::vector<int> a(n);
std::vector<vector<int>> pos(20);
for (int i = 0; i < n; i ++)
cin >> a[i], a[i] --, pos[a[i]].push_back(i);
for (int k = 0; k < 20; k ++)
for (int i = 0, j = 0; i < n; i ++) {
while (j < pos[k].size() && pos[k][j] < i) j ++;
if (j == pos[k].size()) pt[k][i] = -1;
else pt[k][i] = j;
}
std::vector<int> dp(1 << 20, 1E18);
for (int i = 0; i < 20; i ++)
if (pos[i].size() >= 2)
dp[1 << i] = pos[i][1];
for (int i = 0; i < 1 << 20; i ++)
for (int k = 0; k < 20; k ++)
if (!(i >> k & 1)) {
if (dp[i] >= n) break;
int it = pt[k][dp[i]];
if (~it && it + 1 < pos[k].size())
dp[i | (1 << k)] = min(dp[i | (1 << k)], pos[k][it + 1]);
}
int res = 0;
for (int i = 0; i < 1 << 20; i ++)
if (dp[i] < n) res = max(res, (int)__builtin_popcount(i));
cout << res * 2 << endl;
return 0;
}
视频题解
AtCoder Beginner Contest 381(A ~ F 题讲解)
最后祝大家早日