Codeforces Round 968 (Div. 2 ABCD1D2题) 视频讲解

news2024/11/16 7:49:05

A. Turtle and Good Strings

Problem Statement

Turtle thinks a string s s s is a good string if there exists a sequence of strings t 1 , t 2 , … , t k t_1, t_2, \ldots, t_k t1,t2,,tk ( k k k is an arbitrary integer) such that:

  • k ≥ 2 k \ge 2 k2.
  • s = t 1 + t 2 + … + t k s = t_1 + t_2 + \ldots + t_k s=t1+t2++tk, where + + + represents the concatenation operation. For example, abc = a + bc \texttt{abc} = \texttt{a} + \texttt{bc} abc=a+bc.
  • For all 1 ≤ i < j ≤ k 1 \le i < j \le k 1i<jk, the first character of t i t_i ti isn’t equal to the last character of t j t_j tj.

Turtle is given a string s s s consisting of lowercase Latin letters. Please tell him whether the string s s s is a good string!

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 a single integer n n n ( 2 ≤ n ≤ 100 2 \le n \le 100 2n100) — the length of the string.

The second line of each test case contains a string s s s of length n n n, consisting of lowercase Latin letters.

Output

For each test case, output “YES” if the string s s s is a good string, and “NO” 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
4
2
aa
3
aba
4
abcb
12
abcabcabcabc
output
No
nO
Yes
YES

Note

In the first test case, the sequence of strings a , a \texttt{a}, \texttt{a} a,a satisfies the condition s = t 1 + t 2 + … + t k s = t_1 + t_2 + \ldots + t_k s=t1+t2++tk, but the first character of t 1 t_1 t1 is equal to the last character of t 2 t_2 t2. It can be seen that there doesn’t exist any sequence of strings which satisfies all of the conditions, so the answer is “NO”.

In the third test case, the sequence of strings ab , cb \texttt{ab}, \texttt{cb} ab,cb satisfies all of the conditions.

In the fourth test case, the sequence of strings abca , bcab , cabc \texttt{abca}, \texttt{bcab}, \texttt{cabc} abca,bcab,cabc satisfies all of the conditions.

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 n;
	cin >> n;
	string s;
	cin >> s;

	if (s[0] != s.back()) 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;
}

B. Turtle and Piggy Are Playing a Game 2

Problem Statement

Turtle and Piggy are playing a game on a sequence. They are given a sequence a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an, and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.).

The game goes as follows:

  • Let the current length of the sequence be m m m. If m = 1 m = 1 m=1, the game ends.
  • If the game does not end and it’s Turtle’s turn, then Turtle must choose an integer i i i such that 1 ≤ i ≤ m − 1 1 \le i \le m - 1 1im1, set a i a_i ai to max ⁡ ( a i , a i + 1 ) \max(a_i, a_{i + 1}) max(ai,ai+1), and remove a i + 1 a_{i + 1} ai+1.
  • If the game does not end and it’s Piggy’s turn, then Piggy must choose an integer i i i such that 1 ≤ i ≤ m − 1 1 \le i \le m - 1 1im1, set a i a_i ai to min ⁡ ( a i , a i + 1 ) \min(a_i, a_{i + 1}) min(ai,ai+1), and remove a i + 1 a_{i + 1} ai+1.

Turtle wants to maximize the value of a 1 a_1 a1 in the end, while Piggy wants to minimize the value of a 1 a_1 a1 in the end. Find the value of a 1 a_1 a1 in the end if both players play optimally.

You can refer to notes for further clarification.

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.

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 2n105) — the length of the sequence.

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 5 1 \le a_i \le 10^5 1ai105) — the elements of the sequence 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 a single integer — the value of a 1 a_1 a1 in the end if both players play optimally.

Example

input
5
2
1 2
3
1 1 2
3
1 2 3
5
3 1 2 2 3
10
10 2 5 2 7 9 2 5 10 7
output
2
1
2
2
7

Note

In the first test case, initially a = [ 1 , 2 ] a = [1, 2] a=[1,2]. Turtle can only choose i = 1 i = 1 i=1. Then he will set a 1 a_1 a1 to max ⁡ ( a 1 , a 2 ) = 2 \max(a_1, a_2) = 2 max(a1,a2)=2 and remove a 2 a_2 a2. The sequence a a a becomes [ 2 ] [2] [2]. Then the length of the sequence becomes 1 1 1, and the game will end. The value of a 1 a_1 a1 is 2 2 2, so you should output 2 2 2.

In the second test case, one of the possible game processes is as follows:

  • Initially a = [ 1 , 1 , 2 ] a = [1, 1, 2] a=[1,1,2].
  • Turtle can choose i = 2 i = 2 i=2. Then he will set a 2 a_2 a2 to max ⁡ ( a 2 , a 3 ) = 2 \max(a_2, a_3) = 2 max(a2,a3)=2 and remove a 3 a_3 a3. The sequence a a a will become [ 1 , 2 ] [1, 2] [1,2].
  • Piggy can choose i = 1 i = 1 i=1. Then he will set a 1 a_1 a1 to min ⁡ ( a 1 , a 2 ) = 1 \min(a_1, a_2) = 1 min(a1,a2)=1 and remove a 2 a_2 a2. The sequence a a a will become [ 1 ] [1] [1].
  • The length of the sequence becomes 1 1 1, so the game will end. The value of a 1 a_1 a1 will be 1 1 1 in the end.

In the fourth test case, one of the possible game processes is as follows:

  • Initially a = [ 3 , 1 , 2 , 2 , 3 ] a = [3, 1, 2, 2, 3] a=[3,1,2,2,3].
  • Turtle can choose i = 4 i = 4 i=4. Then he will set a 4 a_4 a4 to max ⁡ ( a 4 , a 5 ) = 3 \max(a_4, a_5) = 3 max(a4,a5)=3 and remove a 5 a_5 a5. The sequence a a a will become [ 3 , 1 , 2 , 3 ] [3, 1, 2, 3] [3,1,2,3].
  • Piggy can choose i = 3 i = 3 i=3. Then he will set a 3 a_3 a3 to min ⁡ ( a 3 , a 4 ) = 2 \min(a_3, a_4) = 2 min(a3,a4)=2 and remove a 4 a_4 a4. The sequence a a a will become [ 3 , 1 , 2 ] [3, 1, 2] [3,1,2].
  • Turtle can choose i = 2 i = 2 i=2. Then he will set a 2 a_2 a2 to max ⁡ ( a 2 , a 3 ) = 2 \max(a_2, a_3) = 2 max(a2,a3)=2 and remove a 3 a_3 a3. The sequence a a a will become [ 3 , 2 ] [3, 2] [3,2].
  • Piggy can choose i = 1 i = 1 i=1. Then he will set a 1 a_1 a1 to min ⁡ ( a 1 , a 2 ) = 2 \min(a_1, a_2) = 2 min(a1,a2)=2 and remove a 2 a_2 a2. The sequence a a a will become [ 2 ] [2] [2].
  • The length of the sequence becomes 1 1 1, so the game will end. The value of a 1 a_1 a1 will be 2 2 2 in the end.

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 n;
	cin >> n;
	std::vector<int> a(n);
	for (int i = 0; i < n; i ++)
		cin >> a[i];
	sort(a.begin(), a.end());

	cout << a[n / 2] << endl;
}

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

	int dt;
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

C. Turtle and Good Pairs

Problem Statement

Turtle gives you a string s s s, consisting of lowercase Latin letters.

Turtle considers a pair of integers ( i , j ) (i, j) (i,j) ( 1 ≤ i < j ≤ n 1 \le i < j \le n 1i<jn) to be a pleasant pair if and only if there exists an integer k k k such that i ≤ k < j i \le k < j ik<j and both of the following two conditions hold:

  • s k ≠ s k + 1 s_k \ne s_{k + 1} sk=sk+1;
  • s k ≠ s i s_k \ne s_i sk=si or s k + 1 ≠ s j s_{k + 1} \ne s_j sk+1=sj.

Besides, Turtle considers a pair of integers ( i , j ) (i, j) (i,j) ( 1 ≤ i < j ≤ n 1 \le i < j \le n 1i<jn) to be a good pair if and only if s i = s j s_i = s_j si=sj or ( i , j ) (i, j) (i,j) is a pleasant pair.

Turtle wants to reorder the string s s s so that the number of good pairs is maximized. Please help him!

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.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105) — the length of the string.

The second line of each test case contains a string s s s of length n n n, consisting of lowercase Latin letters.

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 2105.

Output

For each test case, output the string s s s after reordering so that the number of good pairs is maximized. If there are multiple answers, print any of them.

Example

input
5
3
abc
5
edddf
6
turtle
8
pppppppp
10
codeforces
output
acb
ddedf
urtlet
pppppppp
codeforces

Note

In the first test case, ( 1 , 3 ) (1, 3) (1,3) is a good pair in the reordered string. It can be seen that we can’t reorder the string so that the number of good pairs is greater than 1 1 1. bac and cab can also be the answer.

In the second test case, ( 1 , 2 ) (1, 2) (1,2), ( 1 , 4 ) (1, 4) (1,4), ( 1 , 5 ) (1, 5) (1,5), ( 2 , 4 ) (2, 4) (2,4), ( 2 , 5 ) (2, 5) (2,5), ( 3 , 5 ) (3, 5) (3,5) are good pairs in the reordered string. efddd can also be the 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;

void solve() {
	int n;
	string s;
	cin >> n >> s;

	int cnt[27] = {0};
	for (int i = 0; i < n; i ++) cnt[s[i] - 'a'] ++;
	for (int i = 0, j = 0; i < n; i ++, j = (j + 1) % 26) {
		while (!cnt[j]) j = (j + 1) % 26;
		cout << char(j + 'a'), cnt[j] --;
	}
	cout << endl;
}

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

	int dt;
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

D1. Turtle and a MEX Problem (Easy Version)

Problem Statement

The two versions are different problems. In this version of the problem, you can choose the same integer twice or more. You can make hacks only if both versions are solved.

One day, Turtle was playing with n n n sequences. Let the length of the i i i-th sequence be l i l_i li. Then the i i i-th sequence was a i , 1 , a i , 2 , … , a i , l i a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i} ai,1,ai,2,,ai,li.

Piggy gave Turtle a problem to solve when Turtle was playing. The statement of the problem was:

  • There was a non-negative integer x x x at first. Turtle would perform an arbitrary number (possibly zero) of operations on the integer.
  • In each operation, Turtle could choose an integer i i i such that 1 ≤ i ≤ n 1 \le i \le n 1in, and set x x x to mex † ( x , a i , 1 , a i , 2 , … , a i , l i ) \text{mex}^{\dagger}(x, a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}) mex(x,ai,1,ai,2,,ai,li).
  • Turtle was asked to find the answer, which was the maximum value of x x x after performing an arbitrary number of operations.

Turtle solved the above problem without difficulty. He defined f ( k ) f(k) f(k) as the answer to the above problem when the initial value of x x x was k k k.

Then Piggy gave Turtle a non-negative integer m m m and asked Turtle to find the value of ∑ i = 0 m f ( i ) \sum\limits_{i = 0}^m f(i) i=0mf(i) (i.e., the value of f ( 0 ) + f ( 1 ) + … + f ( m ) f(0) + f(1) + \ldots + f(m) f(0)+f(1)++f(m)). Unfortunately, he couldn’t solve this problem. Please help him!

† mex ( c 1 , c 2 , … , c k ) ^{\dagger}\text{mex}(c_1, c_2, \ldots, c_k) mex(c1,c2,,ck) is defined as the smallest non-negative integer x x x which does not occur in the sequence c c c. For example, mex ( 2 , 2 , 0 , 3 ) \text{mex}(2, 2, 0, 3) mex(2,2,0,3) is 1 1 1, mex ( 1 , 2 ) \text{mex}(1, 2) mex(1,2) is 0 0 0.

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.

The first line of each test case contains two integers n , m n, m n,m ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 0 ≤ m ≤ 1 0 9 1 \le n \le 2 \cdot 10^5, 0 \le m \le 10^9 1n2105,0m109).

Each of the following n n n lines contains several integers. The first integer l i l_i li ( 1 ≤ l i ≤ 2 ⋅ 1 0 5 1 \le l_i \le 2 \cdot 10^5 1li2105) represents the length of the i i i-th sequence, and the following l i l_i li integers a i , 1 , a i , 2 , … , a i , l i a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i} ai,1,ai,2,,ai,li ( 0 ≤ a i , j ≤ 1 0 9 0 \le a_{i, j} \le 10^9 0ai,j109) represent the elements of the i i i-th sequence.

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 2105, and the sum of ∑ l i \sum l_i li over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single integer — the value of ∑ i = 0 m f ( i ) \sum\limits_{i = 0}^m f(i) i=0mf(i).

Example

input
6
3 4
2 0 2
3 2 3 3
4 7 0 1 5
3 4
5 0 2 0 4 11
1 1
5 1 3 0 3 3
2 50
2 1 2
2 1 2
1 1
7 1 2 4 1 4 9 5
4 114514
2 2 2
5 7 3 6 0 3
3 0 1 1
5 0 9 2 1 5
5 1919810
1 2
2 324003 0
3 1416324 2 1460728
4 1312631 2 0 1415195
5 1223554 192248 2 1492515 725556
output
16
20
1281
6
6556785365
1842836177961

Note

In the first test case, when x x x is initially 2 2 2, Turtle can choose i = 3 i = 3 i=3 and set x x x to mex ( x , a 3 , 1 , a 3 , 2 , a 3 , 3 , a 3 , 4 ) = mex ( 2 , 7 , 0 , 1 , 5 ) = 3 \text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}) = \text{mex}(2, 7, 0, 1, 5) = 3 mex(x,a3,1,a3,2,a3,3,a3,4)=mex(2,7,0,1,5)=3. It can be proved that Turtle can’t make the value of x x x greater than 3 3 3, so f ( 2 ) = 3 f(2) = 3 f(2)=3.

It can be seen that f ( 0 ) = 3 f(0) = 3 f(0)=3, f ( 1 ) = 3 f(1) = 3 f(1)=3, f ( 2 ) = 3 f(2) = 3 f(2)=3, f ( 3 ) = 3 f(3) = 3 f(3)=3, and f ( 4 ) = 4 f(4) = 4 f(4)=4. So f ( 0 ) + f ( 1 ) + f ( 2 ) + f ( 3 ) + f ( 4 ) = 3 + 3 + 3 + 3 + 4 = 16 f(0) + f(1) + f(2) + f(3) + f(4) = 3 + 3 + 3 + 3 + 4 = 16 f(0)+f(1)+f(2)+f(3)+f(4)=3+3+3+3+4=16.

In the second test case, when x x x is initially 1 1 1, Turtle can choose i = 3 i = 3 i=3 and set x x x to mex ( x , a 3 , 1 , a 3 , 2 , a 3 , 3 , a 3 , 4 , a 3 , 5 ) = mex ( 1 , 1 , 3 , 0 , 3 , 3 ) = 2 \text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}, a_{3, 5}) = \text{mex}(1, 1, 3, 0, 3, 3) = 2 mex(x,a3,1,a3,2,a3,3,a3,4,a3,5)=mex(1,1,3,0,3,3)=2, and choose i = 3 i = 3 i=3 and set x x x to mex ( x , a 3 , 1 , a 3 , 2 , a 3 , 3 , a 3 , 4 , a 3 , 5 ) = mex ( 2 , 1 , 3 , 0 , 3 , 3 ) = 4 \text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}, a_{3, 5}) = \text{mex}(2, 1, 3, 0, 3, 3) = 4 mex(x,a3,1,a3,2,a3,3,a3,4,a3,5)=mex(2,1,3,0,3,3)=4. It can be proved that Turtle can’t make the value of x x x greater than 4 4 4, so f ( 1 ) = 4 f(1) = 4 f(1)=4.

It can be seen that f ( 0 ) = 4 f(0) = 4 f(0)=4, f ( 1 ) = 4 f(1) = 4 f(1)=4, f ( 2 ) = 4 f(2) = 4 f(2)=4, f ( 3 ) = 4 f(3) = 4 f(3)=4, and f ( 4 ) = 4 f(4) = 4 f(4)=4. So f ( 0 ) + f ( 1 ) + f ( 2 ) + f ( 3 ) + f ( 4 ) = 4 + 4 + 4 + 4 + 4 = 20 f(0) + f(1) + f(2) + f(3) + f(4) = 4 + 4 + 4 + 4 + 4 = 20 f(0)+f(1)+f(2)+f(3)+f(4)=4+4+4+4+4=20.

In the fourth test case, it can be seen that f ( 0 ) = 3 f(0) = 3 f(0)=3 and f ( 1 ) = 3 f(1) = 3 f(1)=3. So f ( 0 ) + f ( 1 ) = 3 + 3 = 6 f(0) + f(1) = 3 + 3 = 6 f(0)+f(1)=3+3=6.

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 n, m;
	cin >> n >> m;
	std::vector<int> len(n), mx1(n), mx2(n);
	std::vector<vector<int>> a(n);
	int tot = 0;
	for (int i = 0; i < n; i ++) {
		cin >> len[i], a[i].resize(len[i]), tot += len[i];
		set<int> S;
		int flg = 0;
		for (int j = 0; j < len[i]; j ++)
			cin >> a[i][j], S.insert(a[i][j]);
		for (int j = 0; j <= len[i] + 5; j ++)
			if (!S.count(j)) {
				if (!flg) mx1[i] = j, flg = 1;
				else {
					mx2[i] = j;
					break;
				}
			}
	}
	std::vector<vector<int>> g(tot + 5);
	std::vector<int> dp(tot + 5, 0);
	for (int i = 0; i < n; i ++) g[mx1[i]].push_back(mx2[i]), dp[mx1[i]] = mx1[i];
	for (int i = tot + 4; i >= 0; i --)
		for (auto j : g[i]) dp[i] = max({dp[i], dp[j], j});

	int mx = 0, res = 0;
	for (int i = 0; i < n; i ++ ) mx = max(mx, dp[mx1[i]]);
	for (int i = 0; i <= min(tot + 4, m); i ++)
		res += max({mx, dp[i], i});
	cout << res + max(0ll, m - tot - 4) * (tot + 5 + m) / 2 << endl;
}

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

	int dt;
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

D2. Turtle and a MEX Problem (Hard Version)

Problem Statement

The two versions are different problems. In this version of the problem, you can’t choose the same integer twice or more. You can make hacks only if both versions are solved.

One day, Turtle was playing with n n n sequences. Let the length of the i i i-th sequence be l i l_i li. Then the i i i-th sequence was a i , 1 , a i , 2 , … , a i , l i a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i} ai,1,ai,2,,ai,li.

Piggy gave Turtle a problem to solve when Turtle was playing. The statement of the problem was:

  • There was a non-negative integer x x x at first. Turtle would perform an arbitrary number (possibly zero) of operations on the integer.
  • In each operation, Turtle could choose an integer i i i such that 1 ≤ i ≤ n 1 \le i \le n 1in and i i i wasn’t chosen before, and set x x x to mex † ( x , a i , 1 , a i , 2 , … , a i , l i ) \text{mex}^{\dagger}(x, a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}) mex(x,ai,1,ai,2,,ai,li).
  • Turtle was asked to find the answer, which was the maximum value of x x x after performing an arbitrary number of operations.

Turtle solved the above problem without difficulty. He defined f ( k ) f(k) f(k) as the answer to the above problem when the initial value of x x x was k k k.

Then Piggy gave Turtle a non-negative integer m m m and asked Turtle to find the value of ∑ i = 0 m f ( i ) \sum\limits_{i = 0}^m f(i) i=0mf(i) (i.e., the value of f ( 0 ) + f ( 1 ) + … + f ( m ) f(0) + f(1) + \ldots + f(m) f(0)+f(1)++f(m)). Unfortunately, he couldn’t solve this problem. Please help him!

† mex ( c 1 , c 2 , … , c k ) ^{\dagger}\text{mex}(c_1, c_2, \ldots, c_k) mex(c1,c2,,ck) is defined as the smallest non-negative integer x x x which does not occur in the sequence c c c. For example, mex ( 2 , 2 , 0 , 3 ) \text{mex}(2, 2, 0, 3) mex(2,2,0,3) is 1 1 1, mex ( 1 , 2 ) \text{mex}(1, 2) mex(1,2) is 0 0 0.

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.

The first line of each test case contains two integers n , m n, m n,m ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 0 ≤ m ≤ 1 0 9 1 \le n \le 2 \cdot 10^5, 0 \le m \le 10^9 1n2105,0m109).

Each of the following n n n lines contains several integers. The first integer l i l_i li ( 1 ≤ l i ≤ 2 ⋅ 1 0 5 1 \le l_i \le 2 \cdot 10^5 1li2105) represents the length of the i i i-th sequence, and the following l i l_i li integers a i , 1 , a i , 2 , … , a i , l i a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i} ai,1,ai,2,,ai,li ( 0 ≤ a i , j ≤ 1 0 9 0 \le a_{i, j} \le 10^9 0ai,j109) represent the elements of the i i i-th sequence.

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 2105 and the sum of ∑ l i \sum l_i li over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single integer — the value of ∑ i = 0 m f ( i ) \sum\limits_{i = 0}^m f(i) i=0mf(i).

Example

input
6
3 4
2 0 2
3 2 3 3
4 7 0 1 5
3 4
5 0 2 0 4 11
1 1
5 1 3 0 3 3
2 50
2 1 2
2 1 2
1 1
7 1 2 4 1 4 9 5
4 114514
2 2 2
5 7 3 6 0 3
3 0 1 1
5 0 9 2 1 5
5 1919810
1 2
2 324003 0
3 1416324 2 1460728
4 1312631 2 0 1415195
5 1223554 192248 2 1492515 725556
output
16
18
1281
4
6556785365
1842836177961

Note

In the first test case, when x x x is initially 2 2 2, Turtle can choose i = 3 i = 3 i=3 and set x x x to mex ( x , a 3 , 1 , a 3 , 2 , a 3 , 3 , a 3 , 4 ) = mex ( 2 , 7 , 0 , 1 , 5 ) = 3 \text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}) = \text{mex}(2, 7, 0, 1, 5) = 3 mex(x,a3,1,a3,2,a3,3,a3,4)=mex(2,7,0,1,5)=3. It can be proved that Turtle can’t make the value of x x x greater than 3 3 3, so f ( 2 ) = 3 f(2) = 3 f(2)=3.

It can be seen that f ( 0 ) = 3 f(0) = 3 f(0)=3, f ( 1 ) = 3 f(1) = 3 f(1)=3, f ( 2 ) = 3 f(2) = 3 f(2)=3, f ( 3 ) = 3 f(3) = 3 f(3)=3, and f ( 4 ) = 4 f(4) = 4 f(4)=4. So f ( 0 ) + f ( 1 ) + f ( 2 ) + f ( 3 ) + f ( 4 ) = 3 + 3 + 3 + 3 + 4 = 16 f(0) + f(1) + f(2) + f(3) + f(4) = 3 + 3 + 3 + 3 + 4 = 16 f(0)+f(1)+f(2)+f(3)+f(4)=3+3+3+3+4=16.

In the second test case, when x x x is initially 1 1 1, Turtle can choose i = 1 i = 1 i=1 and set x x x to mex ( x , a 1 , 1 , a 1 , 2 , a 1 , 3 , a 1 , 4 , a 1 , 5 ) = mex ( 1 , 0 , 2 , 0 , 4 , 11 ) = 3 \text{mex}(x, a_{1, 1}, a_{1, 2}, a_{1, 3}, a_{1, 4}, a_{1, 5}) = \text{mex}(1, 0, 2, 0, 4, 11) = 3 mex(x,a1,1,a1,2,a1,3,a1,4,a1,5)=mex(1,0,2,0,4,11)=3. It can be proved that Turtle can’t make the value of x x x greater than 3 3 3, so f ( 1 ) = 3 f(1) = 3 f(1)=3.

It can be seen that f ( 0 ) = 4 f(0) = 4 f(0)=4, f ( 1 ) = 3 f(1) = 3 f(1)=3, f ( 2 ) = 4 f(2) = 4 f(2)=4, f ( 3 ) = 3 f(3) = 3 f(3)=3, and f ( 4 ) = 4 f(4) = 4 f(4)=4. So f ( 0 ) + f ( 1 ) + f ( 2 ) + f ( 3 ) + f ( 4 ) = 4 + 3 + 4 + 3 + 4 = 18 f(0) + f(1) + f(2) + f(3) + f(4) = 4 + 3 + 4 + 3 + 4 = 18 f(0)+f(1)+f(2)+f(3)+f(4)=4+3+4+3+4=18.

In the fourth test case, it can be seen that f ( 0 ) = 3 f(0) = 3 f(0)=3 and f ( 1 ) = 1 f(1) = 1 f(1)=1. So f ( 0 ) + f ( 1 ) = 3 + 1 = 4 f(0) + f(1) = 3 + 1 = 4 f(0)+f(1)=3+1=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;

void solve() {
	int n, m;
	cin >> n >> m;
	std::vector<int> len(n), mx1(n), mx2(n);
	std::vector<vector<int>> a(n);
	int tot = 0;
	for (int i = 0; i < n; i ++) {
		cin >> len[i], a[i].resize(len[i]), tot += len[i];
		set<int> S;
		int flg = 0;
		for (int j = 0; j < len[i]; j ++)
			cin >> a[i][j], S.insert(a[i][j]);
		for (int j = 0; j <= len[i] + 5; j ++)
			if (!S.count(j)) {
				if (!flg) mx1[i] = j, flg = 1;
				else {
					mx2[i] = j;
					break;
				}
			}
	}
	std::vector<vector<int>> g(tot + 5);
	std::vector<int> dp(tot + 5, 0), f(n, 0), st(tot + 5, 0);
	vector<multiset<PII>> S(tot + 5);
	for (int i = 0; i < n; i ++) g[mx1[i]].push_back(mx2[i]);
	for (int i = tot + 4; i >= 0; i --)
		for (auto j : g[i]) dp[i] = max({dp[i], dp[j], j}), S[i].insert({max(dp[j], j), j});
	int mx = 0, cnt = 1;
	for (int i = 0; i < n; i ++) {
		S[mx1[i]].erase(S[mx1[i]].lower_bound(make_pair(max(dp[mx2[i]], mx2[i]), mx2[i])));
		mx = max({mx, mx1[i]});
		if (S[mx1[i]].size()) mx = max(mx, (*S[mx1[i]].rbegin()).fi);
		S[mx1[i]].insert({max(dp[mx2[i]], mx2[i]), mx2[i]});
	}

	int res = 0;
	for (int i = 0; i <= min(tot + 4, m); i ++)
		res += max({mx, i, dp[i]});
	cout << res + max(0ll, m - tot - 4) * (tot + 5 + m) / 2 << 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 968 (Div. 2)(A ~ D2 题讲解)


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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2076438.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

接口自动化测试利器,使用Rest Assured进行REST API测试

我们在做接口测试时&#xff0c;一般在代码中会使用HttpClient&#xff0c;但是HttpClient相对来讲还是比较麻烦的&#xff0c;代码量也相对较多&#xff0c;对于新手而言上手会比较难一点&#xff0c;今天我们来看下另一个接口测试工具包REST Assured REST Assured是一个流行…

Blazor官方文档学习记录

Blazor官方文档学习记录 1 官方文档2 Blazor教程-生成首个应用3 项目结构4 基础知识4.1 生态4.2 Razor组件指令顺序4.3 Razor组件的初始化方法 5 注意 1 官方文档 https://dotnet.microsoft.com/zh-cn/apps/aspnet/web-apps/blazor2 Blazor教程-生成首个应用 https://dotnet.…

Python | Linux | 解析Himawari-8/9 | Standard Data

写作前面 之前一个相关的工作需要解析Himawari-8/9 Standard Data文件&#xff0c;因为他是二进制的&#xff0c;之前没有处理过&#xff0c;导致完全摸不着头脑。在网上找了中英文搜索找了好久&#xff0c;虽然也找到了公开的解析代码&#xff0c;但是放在自己的数据这感觉总是…

趣味算法------猴子吃桃(循环,递归双重解法)

题目描述 猴子第一天摘下若干个桃子&#xff0c;当天吃了一半&#xff0c;后面又多吃一个。第二天早上又将剩下的桃子吃掉一半&#xff0c;又多吃了一个。后面每天猴子都吃了前一天剩下的一半零一个。到第十天想再吃时&#xff0c;只剩下一个桃子。求第一天共摘了多少桃子。 …

鸿蒙(API 12 Beta3版)【获取音视频元数据】音频播放与录制

使用AVMetadataExtractor可以实现从原始媒体资源中获取元数据&#xff0c;本开发指导将以获取一个音频资源的元数据作为示例&#xff0c;向开发者讲解AVMetadataExtractor元数据相关功能。视频资源的元数据获取流程与音频类似&#xff0c;由于视频没有专辑封面&#xff0c;所以…

【中仕公考怎么样】公务员备考小建议

2025年国考在即&#xff0c;掌握正确的备考方法很重要&#xff01;中仕为大家简单分享4点小技巧。 1. 在提升行测分数时&#xff0c;可以采用大量的练习题、整理题以及关注往年核心考点的方式。无论处于准备过程的哪一阶段&#xff0c;对各类题型进行深入分析并掌握相应的解题…

C++ | Leetcode C++题解之第373题查找和最小的K对数字

题目&#xff1a; 题解&#xff1a; class Solution { public:vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {int m nums1.size();int n nums2.size();auto count [&](int target){long long …

怎么用AI生成PPT演讲稿?5个方法教你快速生成

想象一下&#xff0c;你正在准备一场关于“墨西哥是如何走到今天这一步的”演讲&#xff0c;而你却苦于如何将复杂的历史背景、经济变迁以及文化特色等内容有机地整合进一份PPT中。 这时候&#xff0c;一款好的AI自动生成PPT的工具就能派上用场了。它不仅能够帮助你快速构建起…

C# 使用 WinForm MDI 模式管理多个子窗体程序的详细步骤

前言 嗨&#xff0c;各位码农们&#xff01;今天我们要来聊聊如何在 C# 的 WinForms 应用程序中用 MDI&#xff08;Multiple Document Interface&#xff09;模式来优雅地管理多个子窗体。 如果你曾经对着一堆乱七八糟的窗体不知所措&#xff0c;或者想要让你的应用程序看起来…

基于SpringBoot的线上教学平台系统

你好呀&#xff0c;我是计算机学姐码农小野&#xff01;如果有相关需求&#xff0c;可以私信联系我。 开发语言 Java 数据库 MySQL 技术 SpringBoot框架&#xff0c;Java语言 工具 IDEA/Eclipse、Navicat、Maven 系统展示 首页 管理员功能模块 学员功能模块 前台首页…

16行为型设计模式——策略模式

一、策略模式简介 策略模式&#xff08;Strategy Pattern&#xff09;是一种行为型设计模式&#xff0c;它定义了一系列的算法&#xff0c;将每一个算法封装起来&#xff0c;并使它们可以相互替换。具体的算法选择交由客户端决定&#xff0c;即不同的算法可以在运行时动态地&a…

后端微服务架构:构建分布式博客系统

后端微服务架构&#xff1a;构建分布式博客系统 在当今的软件开发领域&#xff0c;微服务架构已经成为构建可扩展、灵活且易于维护的应用程序的主流选择。本文将探讨如何利用微服务架构来设计和实现一个分布式的博客系统。 1. 微服务架构简介 微服务架构是一种将应用程序分解…

练习题 期望dp

题目 分析&#xff1a; 首先注意到期望有线性性&#xff1a; E ( a b ) E ( a ) E ( b ) E(ab)E(a)E(b) E(ab)E(a)E(b)&#xff0c;其中 a a a、 b b b不要求相互独立。 因为网上很多地方的证明不严谨&#xff0c;所以这里证明一下&#xff1a; E ( a b ) ∑ i i ⋅ P …

C语言基础(十八)

1、共用体&#xff08;Union&#xff09;是一种特殊的数据类型&#xff0c;也被称为联合体&#xff0c;它允许在相同的内存位置存储不同的数据类型&#xff0c;每次只能存储其中一种类型的值。共用体是一种数据结构&#xff0c;多个不同类型的变量能够共享同一段内存空间。在C语…

OpenAI推出新功能:GPT-4o正式上线微调功能,限时免费!

GPT-4o正式上线微调功能&#xff0c;限时免费&#xff01; 每个组织每天可以免费获得多达100万个训练token&#xff0c;活动将持续到9月23日。 这意味着开发者们现在可以利用自定义数据集对GPT-4o进行微调&#xff0c;从而以较低的成本构建自己的应用程序。 据悉&#xff0c;G…

推荐3款在Windows系统上运行流畅、音质出众的音乐播放器

foobar2000 Foobar2000是一款由原Winamp开发公司的Peter Pawlowski开发的免费多功能音频播放器&#xff0c;具有高度定制化和丰富的功能。它支持多种音频格式&#xff0c;包括MP3、AAC、WMA、FLAC、WAV等&#xff0c;并且可以进行音频转码和格式转换。此外&#xff0c;Foobar20…

C盘满了,如何清理C盘

目录 磁盘清理删除休眠文件查看系统盘的存储占比卸载掉安装在系统盘的软件更改临时文件、文档等的存储位置 磁盘清理 选择自己的系统盘&#xff0c;我的是G盘。清理系统文件删除休眠文件 删除休眠文件 管理员打开cmd powercfg -h off 直接减少几个g的C盘占用 查看系统盘的存储…

世媒讯海外发稿:全球知名的中文媒体平台

新闻媒体网站专注于媒体行业中各类新闻的网站&#xff0c;也是网友最爱访问的网站类型之一。以下是一些知名的中文媒体平台&#xff0c;它们不仅提供华文内容&#xff0c;还具有较高的访问量和影响力。以下是十大可发布中文新闻稿的知名媒体&#xff0c;包括其月访问量、地区排…

【连续4届EI稳定检索】第五届计算机工程与智能控制学术会议(ICCEIC 2024,10月11-13)

第五届计算机工程与智能控制学术会议&#xff08;ICCEIC 2024&#xff09;将于2024年10月11日至13日在广州举办&#xff0c;聚焦计算机工程与智能控制前沿&#xff0c;涵盖网络安全、硬件系统、软件工程、嵌入式创新等多个核心议题及交叉学科研究。 ICCEIC 2024将计算机工程和智…

QT 程序直接崩溃The program has unexpectedly finished.

QT报错比较难处理&#xff0c;因为给出的控制台输出太宽泛甚至没有信息。 遇到当遇到这种直接崩溃的&#xff0c;试着把一些代码注释掉&#xff0c;慢慢试出出错的位置 。 这里有一种可能的原因是因为有些变量没有初始化&#xff0c;就直接使用。 如下图&#xff1a; 当我运…