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

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 k≥2.
  • 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 1≤i<j≤k, 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 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 ≤ 100 2 \le n \le 100 2≤n≤100) --- 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

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;

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 1≤i≤m−1, 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 1≤i≤m−1, 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 1≤t≤104). 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 2≤n≤105) --- 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 1≤ai≤105) --- 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

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;

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 1≤i<j≤n) 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 i≤k<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 1≤i<j≤n) 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 1≤t≤104). 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 2≤n≤2⋅105) --- 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 2⋅105.

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

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;

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 1≤i≤n, 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=0∑mf(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 1≤t≤104). 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 1≤n≤2⋅105,0≤m≤109).

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 1≤li≤2⋅105) 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 0≤ai,j≤109) 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 2⋅105, 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 2⋅105.

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=0∑mf(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

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;

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 1≤i≤n 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=0∑mf(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 1≤t≤104). 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 1≤n≤2⋅105,0≤m≤109).

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 1≤li≤2⋅105) 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 0≤ai,j≤109) 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 2⋅105 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 2⋅105.

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=0∑mf(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

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;

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 题讲解)


最后祝大家早日

相关推荐
daiyang123...12 分钟前
测试岗位应该学什么
数据结构
alphaTao16 分钟前
LeetCode 每日一题 2024/11/18-2024/11/24
算法·leetcode
我们的五年23 分钟前
【Linux课程学习】:进程程序替换,execl,execv,execlp,execvp,execve,execle,execvpe函数
linux·c++·学习
kitesxian25 分钟前
Leetcode448. 找到所有数组中消失的数字(HOT100)+Leetcode139. 单词拆分(HOT100)
数据结构·算法·leetcode
做人不要太理性1 小时前
【C++】深入哈希表核心:从改造到封装,解锁 unordered_set 与 unordered_map 的终极奥义!
c++·哈希算法·散列表·unordered_map·unordered_set
程序员-King.1 小时前
2、桥接模式
c++·桥接模式
chnming19871 小时前
STL关联式容器之map
开发语言·c++
VertexGeek1 小时前
Rust学习(八):异常处理和宏编程:
学习·算法·rust
石小石Orz1 小时前
Three.js + AI:AI 算法生成 3D 萤火虫飞舞效果~
javascript·人工智能·算法
程序伍六七1 小时前
day16
开发语言·c++