Codeforces Round 945 (Div. 2 ABCDE) 视频讲解

A. Chess For Three

Problem Statement

Three friends gathered to play a few games of chess together.

In every game, two of them play against each other. The winner gets 2 2 2 points while the loser gets 0 0 0, and in case of a draw, both players get 1 1 1 point each. Note that the same pair of players could have played any non-negative number of times (possibly zero). It is also possible that no games were played at all.

You’ve been told that their scores after all the games were played were p 1 p_1 p1, p 2 p_2 p2 and p 3 p_3 p3. Additionally, it is guaranteed that p 1 ≤ p 2 ≤ p 3 p_1 \leq p_2 \leq p_3 p1p2p3 holds.

Find the maximum number of draws that could have happened and print it. If there isn’t any way to obtain p 1 p_1 p1, p 2 p_2 p2 and p 3 p_3 p3 as a result of a non-negative number of games between the three players, print − 1 -1 1 instead.

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 1t500). The description of the test cases follows.

The first line of each test case contains three integers p 1 p_1 p1, p 2 p_2 p2 and p 3 p_3 p3 ( 0 ≤ p 1 ≤ p 2 ≤ p 3 ≤ 30 0 \leq p_1 \leq p_2 \leq p_3 \leq 30 0p1p2p330) — the scores of the three players, sorted non-decreasingly.

Output

For each testcase, print one number — the maximum possible number of draws that could’ve happened, or − 1 -1 1 if the scores aren’t consistent with any valid set of games and results.

Example

Example

input
7
0 0 0
0 1 1
1 1 1
1 1 2
3 3 3
3 4 5
1 1 10
output
0
1
-1
2
-1
6
2

Note

In the first example, no games were played at all, so no draws could occur either.

For the second example, exactly one game occurred between the second and the third player and it ended in draw, so the answer is 1 1 1.

It’s easy to see that there’s no set of games achieving the scores in third example, so the answer for it 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;

void solve() {
	int a, b, c;
	cin >> a >> b >> c;

	if ((a + b + c) & 1) cout << -1 << endl;
	else {
		if (c >= a + b) cout << a + b << endl;
		else cout << (a + b + c) / 2 << endl;
	}
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

B. Cat, Fox and the Lonely Array

Problem Statement

Today, Cat and Fox found an array a a a consisting of n n n non-negative integers.

Define the loneliness of a a a as the smallest positive integer k k k ( 1 ≤ k ≤ n 1 \le k \le n 1kn) such that for any two positive integers i i i and j j j ( 1 ≤ i , j ≤ n − k + 1 1 \leq i, j \leq n - k +1 1i,jnk+1), the following holds:

a i ∣ a i + 1 ∣ … ∣ a i + k − 1 = a j ∣ a j + 1 ∣ … ∣ a j + k − 1 , a_i | a_{i+1} | \ldots | a_{i+k-1} = a_j | a_{j+1} | \ldots | a_{j+k-1}, aiai+1ai+k1=ajaj+1aj+k1,
where x ∣ y x | y xy denotes the bitwise OR of x x x and y y y. In other words, for every k k k consecutive elements, their bitwise OR should be the same. Note that the loneliness of a a a is well-defined, because for k = n k = n k=n the condition is satisfied.

Cat and Fox want to know how lonely the array a a a is. Help them calculate the loneliness of the found array.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ($1 \leq t \leq 10^4 $ ) — the number of test cases. The description of the test cases follows.

The first line of each test case contains one integer n n n ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1n105) — 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 < 2 20 0 \leq a_i < 2^{20} 0ai<220) — the elements of the array.

It is guaranteed that the sum of n n n over all test cases doesn’t exceed 1 0 5 10^5 105.

Output

For each test case, print one integer — the loneliness of the given array.

Example

Example

input
7
1
0
3
2 2 2
3
1 0 2
5
3 0 1 4 2
5
2 0 4 0 2
7
0 0 0 0 1 2 4
8
0 1 3 2 2 1 0 3
output
1
1
3
4
4
7
3

Note

In the first example, the loneliness of an array with a single element is always 1 1 1, so the answer is 1 1 1.

In the second example, the OR of each subarray of length k = 1 k = 1 k=1 is 2 2 2, so the loneliness of the whole array is 1 1 1.

In the seventh example, it’s true that ( 0 ∣ 1 ∣ 3 ) = ( 1 ∣ 3 ∣ 2 ) = ( 3 ∣ 2 ∣ 2 ) = ( 2 ∣ 2 ∣ 1 ) = ( 2 ∣ 1 ∣ 0 ) = ( 1 ∣ 0 ∣ 3 ) = 3 (0 | 1 | 3) = (1 | 3 | 2) = (3 | 2 | 2) = (2 | 2 | 1) = (2 | 1 | 0) = (1 | 0 | 3) = 3 (0∣1∣3)=(1∣3∣2)=(3∣2∣2)=(2∣2∣1)=(2∣1∣0)=(1∣0∣3)=3, so the condition is satisfied for k = 3 k = 3 k=3. We can verify that the condition is not true for any smaller k k k, so the answer is indeed 3 3 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];

void solve() {
	cin >> n;
	for (int i = 1; i <= n; i ++)
		cin >> a[i];

	int res = 1;
	for (int i = 30; i >= 0; i --) {
		int len = 0, mx = 1;
		for (int j = 1; j <= n; j ++)
			if (a[j] >> i & 1) {
				mx = max(mx, len + 1);
				len = 0;
			} else {
				len ++;
			}
		mx = max(mx, len + 1);
		if (mx == n + 1) continue;
		res = max(res, mx);
	}

	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;
}

C. Cat, Fox and Double Maximum

Problem Statement

Fox loves permutations! She came up with the following problem and asked Cat to solve it:

You are given an even positive integer n n n and a permutation † ^\dagger p p p of length n n n.

The score of another permutation q q q of length n n n is the number of local maximums in the array a a a of length n n n, where a i = p i + q i a_i = p_i + q_i ai=pi+qi for all i i i ( 1 ≤ i ≤ n 1 \le i \le n 1in). In other words, the score of q q q is the number of i i i such that 1 < i < n 1 < i < n 1<i<n (note the strict inequalities), a i − 1 < a i a_{i-1} < a_i ai1<ai, and a i > a i + 1 a_i > a_{i+1} ai>ai+1 (once again, note the strict inequalities).

Find the permutation q q q that achieves the maximum score for given n n n and p p p. If there exist multiple such permutations, you can pick any of them.

† ^\dagger A permutation of length n n n is an array consisting of n n n distinct integers from 1 1 1 to n n n in arbitrary order. For example, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4] is a permutation, but [ 1 , 2 , 2 ] [1,2,2] [1,2,2] is not a permutation ( 2 2 2 appears twice in the array), and [ 1 , 3 , 4 ] [1,3,4] [1,3,4] is also not a permutation ( n = 3 n=3 n=3 but there is 4 4 4 in the array).

Input

The first line of input contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases in the input you will have to solve.

The first line of each test case contains one even integer n n n ( 4 ≤ n ≤ 1 0 5 4 \leq n \leq 10^5 4n105, n n n is even) — the length of the permutation p p p.

The second line of each test case contains the n n n integers p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,,pn ( 1 ≤ p i ≤ n 1 \leq p_i \leq n 1pin). It is guaranteed that p p p is a permutation of length n n n.

It is guaranteed that the sum of n n n across all test cases doesn’t exceed 1 0 5 10^5 105.

Output

For each test case, output one line containing any permutation of length n n n (the array q q q), such that q q q maximizes the score under the given constraints.

Example

input
4
4
1 2 3 4
4
4 3 1 2
6
6 5 1 4 2 3
8
1 2 4 5 7 6 8 3
output
2 4 1 3
3 1 4 2
2 5 1 4 3 6
5 4 8 2 7 1 6 3

Note

In the first example, a = [ 3 , 6 , 4 , 7 ] a = [3, 6, 4, 7] a=[3,6,4,7]. The array has just one local maximum (on the second position), so the score of the chosen permutation q q q is 1 1 1. It can be proven that this score is optimal under the constraints.

In the last example, the resulting array a = [ 6 , 6 , 12 , 7 , 14 , 7 , 14 , 6 ] a = [6, 6, 12, 7, 14, 7, 14, 6] a=[6,6,12,7,14,7,14,6] has 3 3 3 local maximums — on the third, fifth and seventh positions.

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 >> a[i];

	std::vector<PII> p;
	p.emplace_back(1, max(1ll, a[1] - a[2] + 1));
	for (int i = 4; i < n; i += 2)
		p.emplace_back(i - 1, max({1ll, a[i - 1] - a[i] + 1, a[i - 1] - a[i - 2] + 1}));
	p.emplace_back(n - 1, max(1ll, a[n - 1] - a[n - 2] + 1));
	sort(p.begin(), p.end(), [&](const PII &a, const PII &b) {
		return a.se > b.se; 
	});

	for (int i = 0; i < p.size(); i ++)
		b[p[i].fi] = i + 1;
	std::set<int> rest;
	for (int i = p.size() + 1; i <= n; i ++)
		rest.insert(i);
	bool flg = 1;
	for (int i = 2; i < n; i += 2) {
		auto it = rest.lower_bound(max(b[i - 1] + max(1ll, a[i - 1] - a[i] + 1), b[i + 1] + max(1ll, a[i + 1] - a[i] + 1)));
		if (it == rest.end()) {
			flg = 0;
			break;
		}
		b[i] = *it;
		rest.erase(b[i]);
	}

	if (flg) {
		for (int i = 1; i < n; i ++)
			cout << b[i] << " ";
		cout << *rest.begin() << endl;
	} else {
		p.clear();
		p.emplace_back(2, max(1ll, a[2] - a[3] + 1));
		for (int i = 5; i < n; i += 2)
			p.emplace_back(i - 1, max({1ll, a[i - 1] - a[i] + 1, a[i - 1] - a[i - 2] + 1}));
		p.emplace_back(n, max(1ll, a[n] - a[n - 1] + 1));
		sort(p.begin(), p.end(), [&](const PII &a, const PII &b) {
			return a.se > b.se; 
		});

		for (int i = 0; i < p.size(); i ++)
			b[p[i].fi] = i + 1;
		rest.clear();
		for (int i = p.size() + 1; i <= n; i ++)
			rest.insert(i);
		bool flg = 1;
		for (int i = 3; i < n; i += 2) {
			auto it = rest.lower_bound(max(b[i - 1] + max(1ll, a[i - 1] - a[i] + 1), b[i + 1] + max(1ll, a[i + 1] - a[i] + 1)));
			b[i] = *it;
			rest.erase(b[i]);
		}

		cout << *rest.begin() << " ";
		for (int i = 2; i <= n; i ++)
			cout << b[i] << " ";
		cout << endl;
	}
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

D. Cat, Fox and Maximum Array Split

Problem Statement

This is an interactive problem.

Fox gave Cat two positive integers n n n and k k k. She has a hidden array a 1 , … , a n a_1, \ldots , a_n a1,,an of length n n n, such that 1 ≤ a i ≤ n 1 \leq a_i \leq n 1ain for every i i i. Now they are going to play the following game:

For any two integers l , r l, r l,r such that 1 ≤ l ≤ r ≤ n 1 \leq l \leq r \leq n 1lrn, define f ( l , r ) = ( r − l + 1 ) ⋅ max ⁡ x = l r a x f(l, r) = (r - l + 1) \cdot \max\limits_{x=l}^r a_x f(l,r)=(rl+1)x=lmaxrax. In other words, f ( l , r ) f(l, r) f(l,r) is equal to the maximum of the subarray a l , … , a r a_l, \ldots, a_r al,,ar multiplied by its size.

Cat can ask Fox at most 2 n 2 n 2n questions about the array. He will tell her two integers l l l and x x x ( 1 ≤ l ≤ n , 1 ≤ x ≤ 1 0 9 1 \leq l \leq n, 1 \leq x \leq 10^9 1ln,1x109), and she will tell him one integer p p p as the answer — the smallest positive integer r r r such that f ( l , r ) = x f(l, r) = x f(l,r)=x, or n + 1 n+1 n+1 if no such r r r exists.

Now, Cat needs to find the largest value m m m such that there exists a sequence c 1 , … , c k − 1 c_1, \ldots, c_{k-1} c1,,ck1 such that 1 ≤ c 1 < … < c k − 1 < n 1 \leq c_1 < \ldots < c_{k-1} < n 1c1<<ck1<n and f ( 1 , c 1 ) = f ( c 1 + 1 , c 2 ) = … = f ( c k − 1 + 1 , n ) = m f(1, c_1) = f(c_1 + 1, c_2) = \ldots = f(c_{k-1}+1, n) = m f(1,c1)=f(c1+1,c2)==f(ck1+1,n)=m. If no such m m m exists, he should indicate this and take − 1 -1 1 as the answer. Note that for k = 1 k = 1 k=1, m m m is always equal to f ( 1 , n ) f(1, n) f(1,n).

In other words, the goal is to find the largest m m m such that you can split the array into exactly k k k subarrays ( k k k is the constant given to you in the beginning of the interaction) so that all the subarrays have the product of their length and their maximum equal to m m m, or determine that no such m m m exists. Every element should belong in exactly one of the subarrays.

Cat doesn’t know what he should do, so he asked you to play the game for him.

Interaction

Each test contains multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 3 1 \leq t \leq 10^3 1t103) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two positive integers n n n and k k k ( 1 ≤ k ≤ n ≤ 1 0 4 1 \leq k \leq n \leq 10^4 1kn104) — the length of the hidden array and the number of subarrays in the desired split.

Now you are allowed to make queries in the following way — print one line of the form “ ?   l   x \mathtt{?} \ l \ x ? l x” (it must hold that 1 ≤ l ≤ n 1 \leq l \leq n 1ln, 1 ≤ x ≤ 1 0 9 1 \leq x \leq 10^9 1x109) and you will receive the smallest integer r r r such that l ≤ r ≤ n l \leq r \leq n lrn and f ( l , r ) = x f(l, r) = x f(l,r)=x, or n + 1 n + 1 n+1 if no such r r r exists.

If you want to print the answer, output “ !   m \mathtt{!} \ m ! m” and you will recieve 1 1 1 if your answer is correct and − 1 -1 1 otherwise. In the first case, the interaction continues with the next test case. Note that printing the answer doesn’t count towards the number of queries made. Please note that you don’t receive the values for the next test case immediately, you will first have to read whether your answer to the last test case was correct.

If you receive the integer − 1 -1 1 at any moment, it means your program has made an invalid query, exceeded the query limit, or gave an incorrect answer. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

After printing a query, 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.

It is guaranteed that the total sum of n n n over the test cases won’t exceed 1 0 4 10^4 104.

Hacks

The format of the hacks should be the following: the first line should contain one integer t t t ( 1 ≤ t ≤ 1 0 3 1 \leq t \leq 10^3 1t103) — the number of test cases. The description of the test cases should follow.

The first line of each test case should contain two integers n n n and k k k ( 1 ≤ k ≤ n ≤ 1 0 4 1 \leq k \leq n \leq 10^4 1kn104) — the length of the array a a a and the number of subarrays you want to split it into.

The second line should contain n n n integers $a_1, a_2, \ldots, a_n $ ($1 \leq a_i \leq n $ ).

The sum of n n n over all test cases should not exceed 1 0 4 10^4 104.

Example

Example

input
3
1 1
1
2 2
1
3
1
6 3
7
2
3
6
1
output
! 1
? 1 1
? 2 1
! -1
? 1 9
? 1 6
? 3 6
? 4 6
! 6

Note

The hidden arrays in the three testcases are [ 1 ] [1] [1], [ 1 , 2 ] [1, 2] [1,2] and [ 1 , 3 , 6 , 1 , 2 , 1 ] [1, 3, 6, 1, 2, 1] [1,3,6,1,2,1]. In the second testcase, no split satisfies the constraints, so the answer is − 1 -1 1.

The answer for the first query of the third testcase is 7 7 7 since no valid r r r exists. For the second query of the third testcase, since 2 ⋅ max ⁡ ( 1 , 3 ) = 6 2 \cdot \max(1, 3) = 6 2max(1,3)=6, we will get 2 2 2 as the answer, since r = 1 r = 1 r=1 doesn’t satisfy the constraint.

The sample interaction guessed all three answers ( 1 , − 1 1, -1 1,1 and 6 6 6) correctly, so it received 1 1 1 after each answer.

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 ask(int l, int x) {
	cout << "? " << l << " " << x << endl;
	int r;
	cin >> r;
	return r;
}

void solve() {
	int n, k;
	cin >> n >> k;

	int val;
	for (int i = 1; i <= n; i ++) {
		if (ask(1, i * n) == n) {
			val = i;
			break;
		}
	}

	int res = -1;
	for (int i = 1; i <= n / k; i ++) {
		int cnt = 0, j = 1;
		while (cnt < k && j <= n) {
			int tmp = ask(j, i * val);
			if (tmp == n + 1) break;
			cnt ++;
			j = tmp + 1;
		}
		if (cnt == k && j == n + 1) {
			res = max(res, i * val);
			continue;
		}
	}

	cout << "! " << res << endl;
	cin >> n;
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

E. Cat, Fox and Swaps

Problem Statement

Fox has found an array p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,,pn, that is a permutation of length n † n^\dagger n of the numbers 1 , 2 , … , n 1, 2, \ldots, n 1,2,,n. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers x x x and y y y in the array, but only if l ≤ x + y ≤ r l \leq x + y \leq r lx+yr (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

They don’t know the numbers l l l, r r r yet, they only know that it’s true that 1 ≤ l ≤ r ≤ 2 ⋅ n 1 \leq l \leq r \leq 2 \cdot n 1lr2n.

You are given the number n n n and the array p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,,pn. Determine how many pairs ( l , r ) (l, r) (l,r) satisfying the conditions are there such that you can sort the permutation if you can only swap two number ( x , y ) (x, y) (x,y) such that l ≤ x + y ≤ r l \leq x + y \leq r lx+yr (arbitrary number of times, possibly 0 0 0).

† ^\dagger A permutation of length n n n is an array consisting of n n n distinct integers from 1 1 1 to n n n in arbitrary order. For example, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4] is a permutation, but [ 1 , 2 , 2 ] [1,2,2] [1,2,2] is not a permutation ( 2 2 2 appears twice in the array), and [ 1 , 3 , 4 ] [1,3,4] [1,3,4] is also not a permutation ( n = 3 n=3 n=3 but there is 4 4 4 in the array).

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 \le t \le 10^4 1t104). The description of the test cases follows.

Description of each test case consists of two lines. The first line contains one integer n n n ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1n105).

The second line contains n n n integers: the array p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,,pn ( 1 ≤ p i ≤ n 1 \le p_i \le n 1pin). It is guaranteed that this array is a permutation of length n n n.

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, print the number of pairs of integers ( l , r ) (l, r) (l,r) such that 1 ≤ l ≤ r ≤ 2 ⋅ n 1 \leq l \leq r \leq 2 \cdot n 1lr2n, and you can sort the array under the constraints.

Example

Example

input
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
output
6
11
23
29
55
46
58

Note

In the first example, we need to be able to swap 1 1 1 and 2 2 2, so we must be able to swap numbers with sum 3 3 3. There are exactly 6 6 6 pairs satisfying the condition: ( 1 , 3 ) , ( 2 , 3 ) , ( 3 , 3 ) , ( 1 , 4 ) , ( 2 , 4 ) (1, 3), (2, 3), (3, 3), (1, 4), (2, 4) (1,3),(2,3),(3,3),(1,4),(2,4) and ( 3 , 4 ) (3, 4) (3,4), so the answer is 6 6 6.

In the second example, the 11 11 11 pairs satisfying the condition are ( 1 , 4 ) , ( 1 , 5 ) , ( 1 , 6 ) , ( 2 , 4 ) , ( 2 , 5 ) , ( 2 , 6 ) , ( 3 , 4 ) , ( 3 , 5 ) , ( 3 , 6 ) , ( 4 , 5 ) (1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5) (1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6),(4,5) and ( 4 , 6 ) (4, 6) (4,6). For example, if we pick the pair ( 3 , 4 ) (3, 4) (3,4) we can first swap the numbers 1 1 1 and 2 2 2 and then the numbers 1 1 1 and 3 3 3, after this, the permutation is sorted.

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 L = n, R = 1;
	for (int i = 1; i <= n; i ++)
		if (a[i] != i) {
			L = i;
			break;
		}
	for (int i = n; i >= 1; i --)
		if (a[i] != i) {
			R = i;
			break;
		}
	if (L == n && R == 1) {
		cout << (2 * n + 1) * n << endl;
	} else {
		int tmp = -1, res = 0;
		for (int i = L; i <= R; i ++)
			if (a[i] != i) {
				if (tmp == -1) tmp = a[i] + a[a[i]];
				else {
					if (a[i] + a[a[i]] == tmp) continue;
					else {
						tmp = -1;
						break;
					}
				}
			}
		if (tmp != -1) res ++;
		L += n, R ++;
		for (int i = R; i <= 2 * n; i ++)
			res += min(L, i - 1);
		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;
}

视频讲解

Codeforces Round 945 (Div. 2)(A ~ E 题讲解)


最后祝大家早日在这里插入图片描述

  • 21
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值