当前位置: 首页 > news >正文

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


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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【计算机组成原理】汇总三、存储系统
  • 机械学习—零基础学习日志(如何理解概率论4)
  • 京准同步:北斗授时设备(北斗校时服务器)操作指南
  • MVVM分层思想
  • 图形学论文笔记
  • Qt WebSocket
  • 10款主流图纸加密软件大盘点(为图纸穿上隐形衣)
  • 幅频特性曲线分析及使用WPF绘制
  • 动手学深度学习7.7. 稠密连接网络(DenseNet)-笔记练习(PyTorch)
  • 供应链系统源码的关键技术是什么?
  • RTC碰到LXTAL低频晶振停振怎么办?
  • docker的前端部署1
  • 构建基于I2C与UART通信的智能嵌入式机械臂抓取系统,结合OpenCV技术进行高效物体识别与动作控制的综合解决方案(代码示例)
  • BaseCTF-web-Week1
  • shell脚本-采集容器内自定义端口tcp连接数并通过http接口推送到Prometheus
  • [LeetCode] Wiggle Sort
  • JavaScript服务器推送技术之 WebSocket
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • MySQL数据库运维之数据恢复
  • nodejs实现webservice问题总结
  • PhantomJS 安装
  • React Transition Group -- Transition 组件
  • Redis 懒删除(lazy free)简史
  • Redis 中的布隆过滤器
  • SpringBoot 实战 (三) | 配置文件详解
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • vue自定义指令实现v-tap插件
  • 解析带emoji和链接的聊天系统消息
  • 前端面试之闭包
  • 如何用vue打造一个移动端音乐播放器
  • 温故知新之javascript面向对象
  • 移动端唤起键盘时取消position:fixed定位
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • Java性能优化之JVM GC(垃圾回收机制)
  • 浅谈sql中的in与not in,exists与not exists的区别
  • 树莓派用上kodexplorer也能玩成私有网盘
  • 组复制官方翻译九、Group Replication Technical Details
  • #【QT 5 调试软件后,发布相关:软件生成exe文件 + 文件打包】
  • #HarmonyOS:Web组件的使用
  • #WEB前端(HTML属性)
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (c语言)strcpy函数用法
  • (java)关于Thread的挂起和恢复
  • (ZT)出版业改革:该死的死,该生的生
  • (二)换源+apt-get基础配置+搜狗拼音
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (函数)颠倒字符串顺序(C语言)
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (十六)视图变换 正交投影 透视投影
  • (五)c52学习之旅-静态数码管
  • (一)、python程序--模拟电脑鼠走迷宫