A. Bazoka and Mocha's Array
Problem Statement
Mocha likes arrays, so before her departure, Bazoka gave her an array a a a consisting of n n n positive integers as a gift.
Now Mocha wants to know whether array a a a could become sorted in non-decreasing order after performing the following operation some (possibly, zero) times:
- Split the array into two parts --- a prefix and a suffix, then swap these two parts. In other words, let a = x + y a=x+y a=x+y. Then, we can set a : = y + x a:= y+x a:=y+x. Here + + + denotes the array concatenation operation.
For example, if a = [ 3 , 1 , 4 , 1 , 5 ] a=[3,1,4,1,5] a=[3,1,4,1,5], we can choose x = [ 3 , 1 ] x=[3,1] x=[3,1] and y = [ 4 , 1 , 5 ] y=[4,1,5] y=[4,1,5], satisfying a = x + y a=x+y a=x+y. Then, we can set a : = y + x = [ 4 , 1 , 5 , 3 , 1 ] a:= y + x = [4,1,5,3,1] a:=y+x=[4,1,5,3,1]. We can also choose x = [ 3 , 1 , 4 , 1 , 5 ] x=[3,1,4,1,5] x=[3,1,4,1,5] and y = [ ] y=[\,] y=[], satisfying a = x + y a=x+y a=x+y. Then, we can set a : = y + x = [ 3 , 1 , 4 , 1 , 5 ] a := y+x = [3,1,4,1,5] a:=y+x=[3,1,4,1,5]. Note that we are not allowed to choose x = [ 3 , 1 , 1 ] x=[3,1,1] x=[3,1,1] and y = [ 4 , 5 ] y=[4,5] y=[4,5], neither are we allowed to choose x = [ 1 , 3 ] x=[1,3] x=[1,3] and y = [ 5 , 1 , 4 ] y=[5,1,4] y=[5,1,4], as both these choices do not satisfy a = x + y a=x+y a=x+y.
Input
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1000 1\leq t\leq 1000 1≤t≤1000). The description of the test cases follows.
The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 50 2\leq n\leq 50 2≤n≤50) --- the length of the array a a a.
The second line of each test case contains n n n integers a 1 , a 2 , ... , a n a_1,a_2,\ldots,a_n a1,a2,...,an ( 1 ≤ a i ≤ 1 0 6 1\leq a_i \leq 10^6 1≤ai≤106) --- the elements of array a a a.
Output
For each test case, output "Yes" if a a a could become non-decreasing after performing the operation any number of times, and output "No" if not.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
Example
Example
input |
---|
3 |
6 |
1 1 4 5 1 4 |
5 |
7 9 2 2 3 |
3 |
1 2 3 |
output |
---|
No |
Yes |
Yes |
Note
In the first test case, it can be proven that a a a cannot become non-decreasing after performing the operation any number of times.
In the second test case, we can perform the following operations to make a a a sorted in non-decreasing order:
- Split the array into two parts: x = [ 7 ] x=[7] x=[7] and y = [ 9 , 2 , 2 , 3 ] y=[9,2,2,3] y=[9,2,2,3], then swap these two parts. The array will become y + x = [ 9 , 2 , 2 , 3 , 7 ] y+x = [9,2,2,3,7] y+x=[9,2,2,3,7].
- Split the array into two parts: x = [ 9 ] x=[9] x=[9] and y = [ 2 , 2 , 3 , 7 ] y=[2,2,3,7] y=[2,2,3,7], then swap these two parts. The array will become y + x = [ 2 , 2 , 3 , 7 , 9 ] y+x=[2,2,3,7,9] y+x=[2,2,3,7,9], which is non-decreasing.
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 = 55;
int n;
int a[N];
void solve() {
cin >> n;
bool res = 1;
for (int i = 1; i <= n; i ++)
cin >> a[i], res &= (a[i] >= a[i - 1]);
if (res) {
cout << "Yes" << endl;
return;
} else if (a[1] < a[n]) {
cout << "No" << endl;
return;
}
for (int i = 1; i < n; i ++) {
if (a[i] < a[i - 1]) break;
bool flg = 1;
for (int j = n - 1; j > i; j --)
flg &= (a[j + 1] >= a[j]);
if (flg) {
cout << "Yes" << endl;
return;
}
}
cout << "No" << endl;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int dt;
cin >> dt;
while (dt --)
solve();
return 0;
}
B. 378QAQ and Mocha's Array
Problem Statement
Mocha likes arrays, so before her departure, 378QAQ gave her an array a a a consisting of n n n positive integers as a gift.
Mocha thinks that a a a is beautiful if there exist two numbers i i i and j j j ( 1 ≤ i , j ≤ n 1\leq i,j\leq n 1≤i,j≤n, i ≠ j i\neq j i=j) such that for all k k k ( 1 ≤ k ≤ n 1 \leq k \leq n 1≤k≤n), a k a_k ak is divisible † ^\dagger † by either a i a_i ai or a j a_j aj.
Determine whether a a a is beautiful.
† ^\dagger † x x x is divisible by y y y if there exists an integer z z z such that x = y ⋅ z x = y \cdot z x=y⋅z.
Input
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 500 1\leq t\leq 500 1≤t≤500). The description of the test cases follows.
The first line of each test case contains a single integer n n n ( 3 ≤ n ≤ 1 0 5 3\leq n\leq 10^5 3≤n≤105) --- the length of the array a a a.
The second line of each test case contains n n n integers a 1 , a 2 , ... , a n a_1,a_2,\ldots,a_n a1,a2,...,an ( 1 ≤ a i ≤ 1 0 9 1\leq a_i \leq 10^9 1≤ai≤109) --- the elements of the array a a a.
It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105.
Output
For each test case, output "Yes" if array a a a is beautiful, and output "No" otherwise.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
Example
Example
input |
---|
4 |
3 |
7 3 8 |
5 |
7 1 9 3 5 |
5 |
4 12 2 6 3 |
5 |
7 49 9 3 1000000000 |
output |
---|
No |
Yes |
Yes |
No |
Note
In the first test case, any two numbers in the array are coprime, so the answer is "No".
In the second test case, we can pick i = 2 i=2 i=2 and j = 1 j=1 j=1. Since every number in the array is divisible by a i = 1 a_i = 1 ai=1, the answer is "Yes".
In the third test case, we can pick i = 3 i=3 i=3 and j = 5 j=5 j=5. 2 2 2 and 4 4 4 is divisible by a i = 2 a_i = 2 ai=2 while 3 3 3, 6 6 6 and 12 12 12 is divisible by a j = 3 a_j = 3 aj=3, so the answer is "Yes".
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 = 1e5 + 10;
int n;
int a[N];
void solve() {
cin >> n;
for (int i = 1; i <= n; i ++)
cin >> a[i];
sort(a + 1, a + 1 + n);
int res = 1, tmp = -1;
for (int i = 2; i <= n; i ++)
if (a[i] % a[1] != 0 && (tmp == -1 || a[i] % tmp != 0)) {
res ++, tmp = a[i];
}
if (res <= 2) cout << "Yes" << endl;
else cout << "No" << endl;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int dt;
cin >> dt;
while (dt --)
solve();
return 0;
}
C. Chamo and Mocha's Array
Problem Statement
Mocha likes arrays, so before her departure, Chamo gave her an array a a a consisting of n n n positive integers as a gift.
Mocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha can perform the following three-step operation some (possibly, zero) times:
- Choose indices l l l and r r r ( 1 ≤ l < r ≤ n 1 \leq l < r \leq n 1≤l<r≤n)
- Let x x x be the median † ^\dagger † of the subarray [ a l , a l + 1 , ... , a r ] [a_l, a_{l+1},\ldots, a_r] [al,al+1,...,ar]
- Set all values a l , a l + 1 , ... , a r a_l, a_{l+1},\ldots, a_r al,al+1,...,ar to x x x
Suppose a = [ 1 , 2 , 3 , 4 , 5 ] a=[1,2,3,4,5] a=[1,2,3,4,5] initially:
- If Mocha chooses ( l , r ) = ( 3 , 4 ) (l,r)=(3,4) (l,r)=(3,4) in the first operation, then x = 3 x=3 x=3, the array will be changed into a = [ 1 , 2 , 3 , 3 , 5 ] a=[1,2,3,3,5] a=[1,2,3,3,5].
- If Mocha chooses ( l , r ) = ( 1 , 3 ) (l,r)=(1,3) (l,r)=(1,3) in the first operation, then x = 2 x=2 x=2, the array will be changed into a = [ 2 , 2 , 2 , 4 , 5 ] a=[2,2,2,4,5] a=[2,2,2,4,5].
Mocha will perform the operation until the array contains only the same number. Mocha wants to know what is the maximum possible value of this number.
† ^\dagger † The median in an array b b b of length m m m is an element that occupies position number ⌊ m + 1 2 ⌋ \lfloor \frac{m+1}{2} \rfloor ⌊2m+1⌋ after we sort the elements in non-decreasing order. For example, the median of [ 3 , 1 , 4 , 1 , 5 ] [3,1,4,1,5] [3,1,4,1,5] is 3 3 3 and the median of [ 5 , 25 , 20 , 24 ] [5,25,20,24] [5,25,20,24] is 20 20 20.
Input
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 500 1\leq t\leq 500 1≤t≤500). The description of the test cases follows.
The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 1 0 5 2\leq n\leq 10^5 2≤n≤105) --- the length of the array a a a.
The second line of each test case contains n n n integers a 1 , a 2 , ... , a n a_1,a_2,\ldots,a_n a1,a2,...,an ( 1 ≤ a i ≤ 1 0 9 1\leq a_i \leq 10^9 1≤ai≤109) --- the elements of the array a a a.
It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105.
Output
For each test case, output the maximum value of the number.
Example
input |
---|
2 |
2 |
1 2 |
5 |
1 2 3 4 5 |
output |
---|
1 |
4 |
Note
In the first test case, a = [ 1 , 2 ] a=[1,2] a=[1,2]. Mocha can only choose the interval ( l , r ) = ( 1 , 2 ) (l,r)=(1,2) (l,r)=(1,2). The array will be changed to a = [ 1 , 1 ] a=[1,1] a=[1,1]. Therefore, the answer is 1 1 1.
In the second test case, Mocha can perform the following operations:
- Choose the interval ( l , r ) = ( 4 , 5 ) (l,r)=(4,5) (l,r)=(4,5), then a = [ 1 , 2 , 3 , 4 , 4 ] a=[1,2,3,4,4] a=[1,2,3,4,4].
- Choose the interval ( l , r ) = ( 3 , 5 ) (l,r)=(3,5) (l,r)=(3,5), then a = [ 1 , 2 , 4 , 4 , 4 ] a=[1,2,4,4,4] a=[1,2,4,4,4].
- Choose the interval ( l , r ) = ( 1 , 5 ) (l,r)=(1,5) (l,r)=(1,5), then a = [ 4 , 4 , 4 , 4 , 4 ] a=[4,4,4,4,4] a=[4,4,4,4,4].
The array contains only the same number, which is 4 4 4. It can be proven that the maximum value of the final number cannot be greater than 4 4 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 = 1e5 + 10;
int n;
int a[N], b[N];
bool check(int x) {
int mn = 0, sum = 0, lst = 1e18;
for (int i = 1; i <= n; i ++) {
sum += a[i] >= x ? 1 : -1;
if (sum - lst > 0) return 1;
lst = mn, mn = min(mn, sum);
}
return 0;
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i ++)
cin >> a[i], b[i] = a[i];
sort(b + 1, b + 1 + n);
int l = 1, r = n;
while (l < r) {
int mid = l + r + 1 >> 1;
if (check(b[mid])) l = mid;
else r = mid - 1;
}
cout << b[l] << endl;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int dt;
cin >> dt;
while (dt --)
solve();
return 0;
}
D. Paint the Tree
Problem Statement
378QAQ has a tree with n n n vertices. Initially, all vertices are white.
There are two chess pieces called P A P_A PA and P B P_B PB on the tree. P A P_A PA and P B P_B PB are initially located on vertices a a a and b b b respectively. In one step, 378QAQ will do the following in order:
- Move P A P_A PA to a neighboring vertex. If the target vertex is white, this vertex will be painted red.
- Move P B P_B PB to a neighboring vertex. If the target vertex is colored in red, this vertex will be painted blue.
Initially, the vertex a a a is painted red. If a = b a=b a=b, the vertex a a a is painted blue instead. Note that both the chess pieces must be moved in each step. Two pieces can be on the same vertex at any given time.
378QAQ wants to know the minimum number of steps to paint all vertices blue.
Input
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1\leq t\leq 10^4 1≤t≤104). The description of the test cases follows.
The first line of each test case contains one integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1\leq n\leq 2\cdot 10^5 1≤n≤2⋅105).
The second line of each test case contains two integers a a a and b b b ( 1 ≤ a , b ≤ n 1\leq a,b\leq n 1≤a,b≤n).
Then n − 1 n - 1 n−1 lines follow, each line contains two integers x i x_i xi and y i y_i yi ( 1 ≤ x i , y i ≤ n 1 \le x_i,y_i \le n 1≤xi,yi≤n), indicating an edge between vertices x i x_i xi and y i y_i yi. It is guaranteed that these edges form a tree.
It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2⋅105.
Output
For each test case, output the minimum number of steps to paint all vertices blue.
Example
input |
---|
3 |
2 |
1 2 |
1 2 |
5 |
1 2 |
1 2 |
1 3 |
1 4 |
1 5 |
8 |
5 4 |
7 1 |
1 5 |
1 8 |
8 3 |
7 2 |
8 6 |
3 4 |
output |
---|
2 |
8 |
13 |
Note
In the first test case, 378QAQ can paint all vertices blue in the following order:
- Initially, P A P_A PA is located on the vertex 1 1 1, and P B P_B PB is located on the vertex 2 2 2. The vertex 1 1 1 is painted red and the vertex 2 2 2 is white.
- 378QAQ moves P A P_A PA to the vertex 2 2 2 and paints it red. Then 378QAQ moves P B P_B PB to the vertex 1 1 1 and paints it blue.
- 378QAQ moves P A P_A PA to the vertex 1 1 1. Then 378QAQ moves P B P_B PB to the vertex 2 2 2 and paints it blue.
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 = 2e5 + 10;
int n, a, b;
std::vector<int> G[N];
int mid, dis, mx;
std::vector<int> path;
void dfs(int u, int fa) {
path.push_back(u);
if (u == b) mid = path[(path.size() + 1) / 2 - 1], dis = path.size();
for (auto v : G[u]) {
if (v == fa) continue;
dfs(v, u);
}
path.pop_back();
}
void dfs2(int u, int fa, int dep) {
if (dep > mx) mx = dep;
for (auto v : G[u]) {
if (v == fa) continue;
dfs2(v, u, dep + 1);
}
}
void solve() {
cin >> n >> a >> b, mx = 0;
for (int i = 1; i <= n; i ++)
G[i].clear();
int u, v;
for (int i = 1; i < n; i ++)
cin >> u >> v, G[u].push_back(v), G[v].push_back(u);
if (a == b) {
dfs2(a, -1, 0);
cout << 2 * n - 2 - mx << endl;
return;
}
dfs(a, -1);
int res = dis / 2;
dfs2(mid, -1, 0);
cout << 2 * n - 2 - mx + res << endl;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int dt;
cin >> dt;
while (dt --)
solve();
return 0;
}
E. Chain Queries
Problem Statement
You are given a tree of n n n vertices numbered from 1 1 1 to n n n. Initially, all vertices are colored white or black.
You are asked to perform q q q queries:
- "u" --- toggle the color of vertex u u u (if it was white, change it to black and vice versa).
After each query, you should answer whether all the black vertices form a chain. That is, there exist two black vertices such that the simple path between them passes through all the black vertices and only the black vertices. Specifically, if there is only one black vertex, they form a chain. If there are no black vertices, they do not form a chain.
Input
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1\leq t\leq 10^4 1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n n n and q q q ( 1 ≤ n , q ≤ 2 ⋅ 1 0 5 1\leq n,q\leq 2\cdot 10^5 1≤n,q≤2⋅105).
The second line of each test case contains n n n integers c 1 , c 2 , ... , c n c_1,c_2,\ldots,c_n c1,c2,...,cn ( c i ∈ { 0 , 1 } c_i \in \{ \mathtt{0}, \mathtt{1} \} ci∈{0,1}) --- the initial color of the vertices. c i c_i ci denotes the color of vertex i i i where 0 \mathtt{0} 0 denotes the color white, and 1 \mathtt{1} 1 denotes the color black.
Then n − 1 n - 1 n−1 lines follow, each line contains two integers x i x_i xi and y i y_i yi ( 1 ≤ x i , y i ≤ n 1 \le x_i,y_i \le n 1≤xi,yi≤n), indicating an edge between vertices x i x_i xi and y i y_i yi. It is guaranteed that these edges form a tree.
The following q q q lines each contain an integer u i u_i ui ( 1 ≤ u i ≤ n 1 \le u_i \le n 1≤ui≤n), indicating the color of vertex u i u_i ui needs to be toggled.
It is guaranteed that the sum of n n n and q q q over all test cases respectively does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2⋅105.
Output
For each query, output "Yes" if the black vertices form a chain, and output "No" otherwise.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
Example
input |
---|
2 |
2 1 |
1 0 |
1 2 |
1 |
5 4 |
1 0 0 0 0 |
1 2 |
1 3 |
1 5 |
3 4 |
4 |
3 |
2 |
5 |
output |
---|
No |
No |
Yes |
Yes |
No |
input |
---|
4 |
5 3 |
1 1 1 1 1 |
3 5 |
2 5 |
3 4 |
1 5 |
1 |
1 |
1 |
4 4 |
0 0 0 0 |
1 2 |
2 3 |
1 4 |
1 |
2 |
3 |
2 |
1 1 |
1 |
1 |
1 1 |
0 |
1 |
output |
---|
Yes |
No |
Yes |
Yes |
Yes |
Yes |
No |
No |
Yes |
Note
In the second test case, the color of the vertices are as follows:
The initial tree:
The first query toggles the color of vertex 4 4 4:
The second query toggles the color of vertex 3 3 3:
The third query toggles the color of vertex 2 2 2:
The fourth query toggles the color of vertex 5 5 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;
const int N = 2e5 + 10;
int n, q;
std::vector<int> g[N];
int col[N], fa[N], cnt[N];
void dfs(int u) {
for (auto v : g[u]) {
if (v == fa[u]) continue;
fa[v] = u;
dfs(v);
}
}
void solve() {
cin >> n >> q;
cnt[0] = 0;
for (int i = 1; i <= n; i ++)
cin >> col[i], g[i].clear(), cnt[i] = 0, fa[i] = 0;
int u, v;
for (int i = 1; i < n; i ++)
cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
dfs(1);
int thr = 0, wht = 0;
set<int> two;
for (int i = 1; i <= n; i ++)
if (col[i]) {
if (!col[fa[i]]) wht ++;
cnt[fa[i]] ++;
if (cnt[fa[i]] == 2) two.insert(fa[i]);
else if (cnt[fa[i]] == 3) thr ++;
}
while (q -- ) {
int u;
cin >> u;
col[u] ^= 1;
if (col[u]) {
if (!col[fa[u]]) wht ++;
wht -= cnt[u], cnt[fa[u]] ++;
if (cnt[fa[u]] == 2) two.insert(fa[u]);
else if (cnt[fa[u]] == 3) thr ++;
} else {
if (!col[fa[u]]) wht --;
wht += cnt[u], cnt[fa[u]] --;
if (cnt[fa[u]] == 2) thr --;
else if (cnt[fa[u]] == 1) two.erase(fa[u]);
}
// cout << wht << " " << (two.size() == 0 || two.size() == 1 && !col[fa[*two.begin()]]) << " " << thr << endl;
if (wht == 1 && (two.size() == 0 || two.size() == 1 && !col[fa[*two.begin()]]) && !thr) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int dt;
cin >> dt;
while (dt --)
solve();
return 0;
}
视频讲解
Codeforces Round 947 (Div. 1 + Div. 2)(A ~ E 题讲解)
最后祝大家早日