A. Maximize the Last Element
Problem Statement
You are given an array a a a of n n n integers, where n n n is odd.
In one operation, you will remove two adjacent elements from the array a a a, and then concatenate the remaining parts of the array. For example, given the array [ 4 , 7 , 4 , 2 , 9 ] [4,7,4,2,9] [4,7,4,2,9], we can obtain the arrays [ 4 , 2 , 9 ] [4,2,9] [4,2,9] and [ 4 , 7 , 9 ] [4,7,9] [4,7,9] by the operations [ 4 , 7 ‾ , 4 , 2 , 9 ] → [ 4 , 2 , 9 ] [\underline{4,7}, 4,2,9] \to [4,2,9] [4,7,4,2,9]→[4,2,9] and [ 4 , 7 , 4 , 2 ‾ , 9 ] → [ 4 , 7 , 9 ] [4,7,\underline{4,2},9] \to [4,7,9] [4,7,4,2,9]→[4,7,9] respectively. However, we cannot obtain the array [ 7 , 2 , 9 ] [7,2,9] [7,2,9] as it requires deleting non-adjacent elements [ 4 ‾ , 7 , 4 ‾ , 2 , 9 ] [\underline{4},7,\underline{4},2,9] [4,7,4,2,9].
You will repeatedly perform this operation until exactly one element remains in a a a.
Find the maximum possible value of the remaining element in a a a.
Input
Each test contains multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1≤t≤1000) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 99 1 \le n \le 99 1≤n≤99; n n n is odd) — 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 ≤ 100 1 \le a_i \le 100 1≤ai≤100) — the elements of the array a a a.
Note that there is no bound on the sum of n n n over all test cases.
Output
For each test case, output a single integer — the maximum possible value of the remaining element in a a a.
Example
input |
---|
4 |
1 |
6 |
3 |
1 3 2 |
5 |
4 7 4 2 9 |
7 |
3 1 4 1 5 9 2 |
output |
---|
6 |
2 |
9 |
5 |
Note
In the first test case, the array a a a is [ 6 ] [6] [6]. Since there is only one element, no operations are needed. The maximum possible value of the remaining element is 6 6 6.
In the second test case, the array a a a is [ 1 , 3 , 2 ] [1, 3, 2] [1,3,2]. We can remove the first two elements [ 1 , 3 ‾ , 2 ] → [ 2 ] [\underline{1, 3}, 2] \to [2] [1,3,2]→[2], or remove the last two elements [ 1 , 3 , 2 ‾ ] → [ 1 ] [1, \underline{3, 2}] \to [1] [1,3,2]→[1]. Therefore, the maximum possible value of the remaining element is 2 2 2.
In the third test case, the array a a a is [ 4 , 7 , 4 , 2 , 9 ] [4, 7, 4, 2, 9] [4,7,4,2,9]. One way to maximize the remaining element is [ 4 , 7 , 4 ‾ , 2 , 9 ] → [ 4 , 2 ‾ , 9 ] → [ 9 ] [4, \underline{7, 4}, 2, 9] \to [\underline{4, 2}, 9] \to [9] [4,7,4,2,9]→[4,2,9]→[9]. Therefore, the maximum possible value of the remaining element is 9 9 9.
In the fourth test case, the array a a a is [ 3 , 1 , 4 , 1 , 5 , 9 , 2 ] [3, 1, 4, 1, 5, 9, 2] [3,1,4,1,5,9,2]. It can be shown that the maximum possible value of the remaining element is 5 5 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 = 1e5 + 10;
int n;
int a[N];
void solve() {
cin >> n;
for (int i = 1; i <= n; i ++)
cin >> a[i];
int res = 0;
for (int i = 1; i <= n; i += 2)
res = max(res, a[i]);
cout << res << endl;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int dt;
cin >> dt;
while (dt --)
solve();
return 0;
}
B. AND Reconstruction
Problem Statement
You are given an array b b b of n − 1 n - 1 n−1 integers.
An array a a a of n n n integers is called good if b i = a i & a i + 1 b_i = a_i \, \& \, a_{i + 1} bi=ai&ai+1 for 1 ≤ i ≤ n − 1 1 \le i \le n-1 1≤i≤n−1, where & \& & denotes the bitwise AND operator.
Construct a good array, or report that no good arrays exist.
Input
Each test contains multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 1 0 5 2 \le n \le 10^5 2≤n≤105) — the length of the array a a a.
The second line of each test case contains n − 1 n - 1 n−1 integers b 1 , b 2 , … , b n − 1 b_1, b_2, \ldots, b_{n - 1} b1,b2,…,bn−1 ( 0 ≤ b i < 2 30 0 \le b_i < 2^{30} 0≤bi<230) — the elements of the array b b b.
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 a single integer − 1 -1 −1 if no good arrays exist.
Otherwise, output n n n space-separated integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 0 ≤ a i < 2 30 0 \le a_i < 2^{30} 0≤ai<230) — the elements of a good array a a a.
If there are multiple solutions, you may output any of them.
Example
input |
---|
4 |
2 |
1 |
3 |
2 0 |
4 |
1 2 3 |
5 |
3 5 4 2 |
output |
---|
5 3 |
3 2 1 |
-1 |
3 7 5 6 3 |
Note
In the first test case, b = [ 1 ] b = [1] b=[1]. A possible good array is a = [ 5 , 3 ] a=[5, 3] a=[5,3], because a 1 & a 2 = 5 & 3 = 1 = b 1 a_1 \, \& \, a_2 = 5 \, \& \, 3 = 1 = b_1 a1&a2=5&3=1=b1.
In the second test case, b = [ 2 , 0 ] b = [2, 0] b=[2,0]. A possible good array is a = [ 3 , 2 , 1 ] a=[3, 2, 1] a=[3,2,1], because a 1 & a 2 = 3 & 2 = 2 = b 1 a_1 \, \& \, a_2 = 3 \, \& \, 2 = 2 = b_1 a1&a2=3&2=2=b1 and a 2 & a 3 = 2 & 1 = 0 = b 2 a_2 \, \& \, a_3 = 2 \, \& \, 1 = 0 = b_2 a2&a3=2&1=0=b2.
In the third test case, b = [ 1 , 2 , 3 ] b = [1, 2, 3] b=[1,2,3]. It can be shown that no good arrays exist, so the output is − 1 -1 −1.
In the fourth test case, b = [ 3 , 5 , 4 , 2 ] b = [3, 5, 4, 2] b=[3,5,4,2]. A possible good array is a = [ 3 , 7 , 5 , 6 , 3 ] a=[3, 7, 5, 6, 3] a=[3,7,5,6,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 = 1e5 + 10;
int n;
int a[N], b[N];
void solve() {
cin >> n;
for (int i = 1; i < n; i ++)
cin >> b[i];
for (int i = 1; i < n; i ++)
for (int j = 0; j <= 30; j ++)
if (b[i] >> j & 1) a[i] |= (1ll << j), a[i + 1] |= (1ll << j);
for (int i = 1; i < n; i ++)
if ((a[i] & a[i + 1]) != b[i]) {
cout << -1 << endl;
for (int i = 1; i <= n; i ++) a[i] = 0;
return;
}
for (int i = 1; i <= n; i ++)
cout << a[i] << " ", a[i] = 0;
cout << endl;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int dt;
cin >> dt;
while (dt --)
solve();
return 0;
}
C. Absolute Zero
Problem Statement
You are given an array a a a of n n n integers.
In one operation, you will perform the following two-step move:
- Choose an integer x x x ( 0 ≤ x ≤ 1 0 9 0 \le x \le 10^{9} 0≤x≤109).
- Replace each a i a_i ai with ∣ a i − x ∣ |a_i - x| ∣ai−x∣, where ∣ v ∣ |v| ∣v∣ denotes the absolute value of v v v.
For example, by choosing x = 8 x = 8 x=8, the array [ 5 , 7 , 10 ] [5, 7, 10] [5,7,10] will be changed into [ ∣ 5 − 8 ∣ , ∣ 7 − 8 ∣ , ∣ 10 − 8 ∣ ] = [ 3 , 1 , 2 ] [|5-8|, |7-8|, |10-8|] = [3,1,2] [∣5−8∣,∣7−8∣,∣10−8∣]=[3,1,2].
Construct a sequence of operations to make all elements of a a a equal to 0 0 0 in at most 40 40 40 operations or determine that it is impossible. You do not need to minimize the number of operations.
Input
Each test contains multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1≤n≤2⋅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 ( 0 ≤ a i ≤ 1 0 9 0 \le a_i \le 10^9 0≤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 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105.
Output
For each test case, output a single integer − 1 -1 −1 if it is impossible to make all array elements equal to 0 0 0 in at most 40 40 40 operations.
Otherwise, output two lines. The first line of output should contain a single integer k k k ( 0 ≤ k ≤ 40 0 \le k \le 40 0≤k≤40) — the number of operations. The second line of output should contain k k k integers x 1 , x 2 , … , x k x_1, x_2, \ldots, x_k x1,x2,…,xk ( 0 ≤ x i ≤ 1 0 9 0 \le x_i \le 10^{9} 0≤xi≤109) — the sequence of operations, denoting that on the i i i-th operation, you chose x = x i x=x_i x=xi.
If there are multiple solutions, output any of them.
You do not need to minimize the number of operations.
Example
input |
---|
5 |
1 |
5 |
2 |
0 0 |
3 |
4 6 8 |
4 |
80 40 20 10 |
5 |
1 2 3 4 5 |
output |
---|
1 |
5 |
0 |
3
6 1 1
7
60 40 20 10 30 25 5
-1
Note
In the first test case, we can perform only one operation by choosing x = 5 x = 5 x=5, changing the array from [ 5 ] [5] [5] to [ 0 ] [0] [0].
In the second test case, no operations are needed because all elements of the array are already 0 0 0.
In the third test case, we can choose x = 6 x = 6 x=6 to change the array from [ 4 , 6 , 8 ] [4, 6, 8] [4,6,8] to [ 2 , 0 , 2 ] [2, 0, 2] [2,0,2], then choose x = 1 x = 1 x=1 to change it to [ 1 , 1 , 1 ] [1, 1, 1] [1,1,1], and finally choose x = 1 x = 1 x=1 again to change the array into [ 0 , 0 , 0 ] [0, 0, 0] [0,0,0].
In the fourth test case, we can make all elements 0 0 0 by following the operation sequence ( 60 , 40 , 20 , 10 , 30 , 25 , 5 ) (60, 40, 20, 10, 30, 25, 5) (60,40,20,10,30,25,5).
In the fifth test case, it can be shown that it is impossible to make all elements 0 0 0 in at most 40 40 40 operations. Therefore, the output is − 1 -1 −1.
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];
void solve() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
std::vector<int> op;
for (int i = 0; i <= 40; i ++) {
int mx = *max_element(a + 1, a + 1 + n);
if (mx == 0) {
cout << op.size() << endl;
for (auto v : op) cout << v << " ";
cout << endl;
return;
}
for (int j = 1; j <= n; j ++) a[j] = abs(a[j] - (mx + 1) / 2);
op.push_back((mx + 1) / 2);
}
cout << -1 << endl;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int dt;
cin >> dt;
while (dt --)
solve();
return 0;
}
D. Prime XOR Coloring
Problem Statement
You are given an undirected graph with n n n vertices, numbered from 1 1 1 to n n n. There is an edge between vertices u u u and v v v if and only if u ⊕ v u \oplus v u⊕v is a prime number, where ⊕ \oplus ⊕ denotes the bitwise XOR operator.
Color all vertices of the graph using the minimum number of colors, such that no two vertices directly connected by an edge have the same color.
Input
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 500 1 \le t \le 500 1≤t≤500). The description of test cases follows.
The only line contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1≤n≤2⋅105) — the number of vertices in the graph.
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 two lines.
The first line should contain a single integer k k k ( 1 ≤ k ≤ n 1 \le k \le n 1≤k≤n) — the minimum number of colors required.
The second line should contain n n n integers c 1 , c 2 , … , c n c_1, c_2, \ldots, c_n c1,c2,…,cn ( 1 ≤ c i ≤ k 1 \le c_i \le k 1≤ci≤k) — the color of each vertex.
If there are multiple solutions, output any of them.
Example
input |
---|
6 |
1 |
2 |
3 |
4 |
5 |
6 |
output |
---|
1 |
1 |
2 |
1 2 |
2 |
1 2 2 |
3 |
1 2 2 3 |
3 |
1 2 2 3 3 |
4 |
1 2 2 3 3 4 |
Note
In the first test case, the minimum number of colors is 1 1 1, because there is only one vertex.
In the second test case, the minimum number of colors is 2 2 2, because there is an edge connecting 1 1 1 and 2 2 2 ( 1 ⊕ 2 = 3 1 \oplus 2 = 3 1⊕2=3, which is a prime number).
In the third test case, the minimum number of colors is still 2 2 2, because 2 2 2 and 3 3 3 can be colored the same since there is no edge between 2 2 2 and 3 3 3 ( 2 ⊕ 3 = 1 2 \oplus 3 = 1 2⊕3=1, which is not a prime number).
In the fourth test case, it can be shown that the minimum number of colors is 3 3 3.
In the fifth test case, it can be shown that the minimum number of colors is 3 3 3.
In the sixth test case, it can be shown that the minimum number of colors is 4 4 4.
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 prm[N], st[N], idx;
int res[N];
void prework() {
for (int i = 2; i < N; i ++) {
if (!st[i]) st[i] = true, prm[ ++ idx] = i;
for (int j = 1; prm[j] * i < N; j ++) {
st[prm[j] * i] = true;
if (i % prm[j] == 0) break;
}
}
}
void solve() {
int n;
cin >> n;
if (n == 1) cout << "1\n1\n";
else if (n == 2) cout << "2\n1 2\n";
else if (n == 3) cout << "2\n1 2 2\n";
else if (n == 4) cout << "3\n1 2 2 3\n";
else if (n == 5) cout << "3\n1 2 2 3 3\n";
else {
cout << 4 << endl;
for (int i = 1; i <= n; i ++)
cout << i % 4 + 1 << " ";
cout << endl;
}
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
prework();
int dt;
cin >> dt;
while (dt --)
solve();
return 0;
}
E. Coloring Game
Problem Statement
This is an interactive problem.
Consider an undirected connected graph consisting of n n n vertices and m m m edges. Each vertex can be colored with one of three colors: 1 1 1, 2 2 2, or 3 3 3. Initially, all vertices are uncolored.
Alice and Bob are playing a game consisting of n n n rounds. In each round, the following two-step process happens:
- Alice chooses two different colors.
- Bob chooses an uncolored vertex and colors it with one of the two colors chosen by Alice.
Alice wins if there exists an edge connecting two vertices of the same color. Otherwise, Bob wins.
You are given the graph. Your task is to decide which player you wish to play as and win the game.
Input
Each test contains multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1≤t≤1000) — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers n n n, m m m ( 1 ≤ n ≤ 1 0 4 1 \le n \le 10^4 1≤n≤104, n − 1 ≤ m ≤ min ( n ⋅ ( n − 1 ) 2 , 1 0 4 ) n - 1 \le m \le \min(\frac{n \cdot (n - 1)}{2}, 10^4) n−1≤m≤min(2n⋅(n−1),104)) — the number of vertices and the number of edges in the graph, respectively.
Each of the next m m m lines of each test case contains two integers u i u_i ui, v i v_i vi ( 1 ≤ u i , v i ≤ n 1 \le u_i, v_i \le n 1≤ui,vi≤n) — the edges of the graph. It is guaranteed that the graph is connected and there are no multiple edges or self-loops.
It is guaranteed that the sum of n n n and the sum of m m m over all test cases does not exceed 1 0 4 10^4 104.
Interaction
For each test case, you need to output a single line containing either “Alice” or “Bob”, representing the player you choose.
Then for each of the following n n n rounds, the following two-step process happens:
- Alice (either you or the interactor) will output two integers a a a and b b b ( 1 ≤ a , b ≤ 3 1 \le a, b \le 3 1≤a,b≤3, a ≠ b a \neq b a=b) — the colors chosen by Alice.
- Bob (either you or the interactor) will output two integers i i i and c c c ( 1 ≤ i ≤ n 1 \le i \le n 1≤i≤n, c = a c = a c=a or c = b c = b c=b) — the vertex and the color chosen by Bob. Vertex i i i must be a previously uncolored vertex.
If any of your outputs are invalid, the jury will output “-1” and you will receive a Wrong Answer verdict.
At the end of all n n n turns, if you have lost the game, the jury will output “-1” and you will receive a Wrong Answer verdict.
If your program has received a − 1 -1 −1 instead of a valid value, it must terminate immediately. Otherwise, you may receive an arbitrary verdict because your solution might be reading from a closed stream.
Note that if you are playing as Alice, and there already exists an edge connected two vertices of the same color, the interactor will not terminate early and you will keep playing all n n n rounds.
After outputting, do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see documentation for other languages.
In this problem, hacks are disabled.
Example
input |
---|
2 |
3 3 |
1 2 |
2 3 |
3 1 |
3 1
2 2
1 1
4 4
1 2
2 3
3 4
4 1
2 3
1 2
2 1
3 1
output |
---|
Alice
3 1
1 2
2 1
Bob
1 2
2 1
4 1
3 3
Note
Note that the sample test cases are example games and do not necessarily represent the optimal strategy for both players.
In the first test case, you choose to play as Alice.
- Alice chooses two colors: 3 3 3 and 1 1 1. Bob chooses vertex 3 3 3 and colors it with color 1 1 1.
- Alice chooses two colors: 1 1 1 and 2 2 2. Bob chooses vertex 2 2 2 and colors it with color 2 2 2.
- Alice chooses two colors: 2 2 2 and 1 1 1. Bob chooses vertex 1 1 1 and colors it with color 1 1 1.
Alice wins because the edge ( 3 , 1 ) (3, 1) (3,1) connects two vertices of the same color.
In the second test case, you choose to play as Bob.
- Alice chooses two colors: 2 2 2 and 3 3 3. Bob chooses vertex 1 1 1 and colors it with color 2 2 2.
- Alice chooses two colors: 1 1 1 and 2 2 2. Bob chooses vertex 2 2 2 and colors it with color 1 1 1.
- Alice chooses two colors: 2 2 2 and 1 1 1. Bob chooses vertex 4 4 4 and colors it with color 1 1 1.
- Alice chooses two colors: 3 3 3 and 1 1 1. Bob chooses vertex 3 3 3 and colors it with color 3 3 3.
Bob wins because there are no edges with vertices of the same color.
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 = 1e4 + 10;
int n, m;
int col[N], cnt[3];
std::vector<int> g[N], pt[3];
bool dfs(int u, int cl) {
col[u] = cl;
for (auto v : g[u])
if (!col[v] && !dfs(v, 3 - cl)) return 0;
else if (col[v] == cl) return 0;
return 1;
}
void solve() {
cin >> n >> m;
while (m -- ) {
int u, v;
cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
}
if (!dfs(1, 1)) {
cout << "Alice" << endl;
for (int i = 1; i <= n; i ++) {
cout << "1 2" << endl;
int a, b;
cin >> a >> b;
}
} else {
cout << "Bob" << endl;
for (int i = 1; i <= n; i ++) pt[col[i]].push_back(i);
for (int i = 1; i <= n; i ++) {
int a, b;
cin >> a >> b;
if (a > b) swap(a, b);
if (pt[a].size() == 0) cout << pt[3 - a].back() << " " << b << endl, pt[3 - a].pop_back();
else cout << pt[a].back() << " " << a << endl, pt[a].pop_back();
}
}
for (int i = 1; i <= n; i ++) g[i].clear(), col[i] = 0;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int dt;
cin >> dt;
while (dt --)
solve();
return 0;
}
F. Triangle Formation
Problem Statement
You are given n n n sticks, numbered from 1 1 1 to n n n. The length of the i i i-th stick is a i a_i ai.
You need to answer q q q queries. In each query, you are given two integers l l l and r r r ( 1 ≤ l < r ≤ n 1 \le l < r \le n 1≤l<r≤n, r − l + 1 ≥ 6 r - l + 1 \ge 6 r−l+1≥6). Determine whether it is possible to choose 6 6 6 distinct sticks from the sticks numbered l l l to r r r, to form 2 2 2 non-degenerate triangles ∗ ^{\text{∗}} ∗.
∗ ^{\text{∗}} ∗A triangle with side lengths a a a, b b b, and c c c is called non-degenerate if:
- a < b + c a < b + c a<b+c,
- b < a + c b < a + c b<a+c, and
- c < a + b c < a + b c<a+b.
Input
The first line contains two integers n n n and q q q ( 6 ≤ n ≤ 1 0 5 6 \le n \le 10^5 6≤n≤105, 1 ≤ q ≤ 1 0 5 1 \le q \le 10^5 1≤q≤105) — the number of sticks and the number of queries respectively.
The second line 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 \le a_i \le 10^9 1≤ai≤109) — a i a_i ai denotes the length of the i i i-th stick.
Each of the following q q q lines contains two integers l l l and r r r ( 1 ≤ l < r ≤ n 1 \le l < r \le n 1≤l<r≤n, r − l + 1 ≥ 6 r - l + 1 \ge 6 r−l+1≥6) — the parameters of each query.
Output
For each query, output “YES” (without quotes) if it is possible to form 2 2 2 triangles, and “NO” (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will be recognized as positive responses.
Example
input |
---|
10 5 |
5 2 2 10 4 10 6 1 5 3 |
1 6 |
2 7 |
2 8 |
5 10 |
4 10 |
output |
---|
YES |
NO |
YES |
NO |
YES |
Note
In the first query, the lengths of the sticks are [ 5 , 2 , 2 , 10 , 4 , 10 ] [5, 2, 2, 10, 4, 10] [5,2,2,10,4,10]. Two sets of sticks [ 2 , 4 , 5 ] [2, 4, 5] [2,4,5] and [ 2 , 10 , 10 ] [2, 10, 10] [2,10,10] can be selected to form 2 2 2 non-degenerate triangles.
In the second query, the lengths of the sticks are [ 2 , 2 , 10 , 4 , 10 , 6 ] [2, 2, 10, 4, 10, 6] [2,2,10,4,10,6]. It can be shown that it is impossible to form 2 2 2 non-degenerate triangles.
In the third query, the lengths of the sticks are [ 2 , 2 , 10 , 4 , 10 , 6 , 1 ] [2, 2, 10, 4, 10, 6, 1] [2,2,10,4,10,6,1]. Two sets of sticks [ 1 , 2 , 2 ] [1, 2, 2] [1,2,2] and [ 4 , 10 , 10 ] [4, 10, 10] [4,10,10] can be selected to form 2 2 2 non-degenerate triangles.
In the fourth query, the lengths of the sticks are [ 4 , 10 , 6 , 1 , 5 , 3 ] [4, 10, 6, 1, 5, 3] [4,10,6,1,5,3]. It can be shown that it is impossible to form 2 2 2 non-degenerate triangles.
In the fifth query, the lengths of the sticks are [ 10 , 4 , 10 , 6 , 1 , 5 , 3 ] [10, 4, 10, 6, 1, 5, 3] [10,4,10,6,1,5,3]. Two sets of sticks [ 1 , 10 , 10 ] [1, 10, 10] [1,10,10] and [ 3 , 4 , 5 ] [3, 4, 5] [3,4,5] can be selected to form 2 2 2 non-degenerate triangles.
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 = 1e5 + 10, B = 50;
int n, q;
int a[N];
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i <= n; i ++) cin >> a[i];
while (q -- ) {
int l, r;
cin >> l >> r;
if (r - l + 1 >= B) cout << "YES" << endl;
else {
std::vector<int> b;
for (int i = l; i <= r; i ++) b.push_back(a[i]);
sort(b.begin(), b.end());
std::vector<int> pos;
for (int i = 2; i < b.size(); i ++)
if (b[i - 2] + b[i - 1] > b[i]) pos.push_back(i);
if (pos.size() >= 2 && pos.back() - pos[0] >= 3) cout << "YES" << endl;
else if (pos.size() >= 2) {
if (pos.back() < 5) cout << "NO" << endl;
else {
int A = b[pos.back() - 5], B = b[pos.back() - 4], C = b[pos.back() - 3], D = b[pos.back() - 2], E = b[pos.back() - 1], F = b[pos.back()];
if (A + C > D && B + E > F) cout << "YES" << endl;
else if (A + C > E && B + D > F) cout << "YES" << endl;
else if (A + C > F && B + D > E) cout << "YES" << endl;
else if (A + D > E && B + C > F) cout << "YES" << endl;
else if (A + D > F && B + C > E) cout << "YES" << endl;
else if (A + E > F && B + C > D) cout << "YES" << endl;
else cout << "NO" << endl;
}
} else cout << "NO" << endl;
}
}
return 0;
}
视频讲解
Pinely Round 4 (Div. 1 + Div. 2)(A ~ F 题讲解)
最后祝大家早日