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

发布于:2024-05-02 ⋅ 阅读:(28) ⋅ 点赞:(0)

A. Card Exchange

Problem Statement

You have a hand of n n n cards, where each card has a number written on it, and a fixed integer k k k. You can perform the following operation any number of times:

  • Choose any k k k cards from your hand that all have the same number.
  • Exchange these cards for k − 1 k-1 k1 cards, each of which can have any number you choose (including the number written on the cards you just exchanged).

Here is one possible sequence of operations for the first example case, which has k = 3 k=3 k=3:

What is the minimum number of cards you can have in your hand at the end of this process?

Input

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 500 1 \le t \le 500 1t500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and k k k ( 1 ≤ n ≤ 100 1 \le n \le 100 1n100, 2 ≤ k ≤ 100 2 \le k \le 100 2k100) — the number of cards you have, and the number of cards you exchange during each operation, respectively.

The next line of each test case contains n n n integers c 1 , c 2 , … c n c_1, c_2, \ldots c_n c1,c2,cn ( 1 ≤ c i ≤ 100 1 \le c_i \le 100 1ci100) — the numbers written on your cards.

Output

For each test case, output a single integer — the minimum number of cards you can have left in your hand after any number of operations.

Example

Example

input
7
5 3
4 1 1 4 4
1 10
7
7 2
4 2 1 100 5 2 3
10 4
1 1 1 1 1 1 1 1 1 1
5 2
3 8 1 48 7
6 2
10 20 30 10 20 40
6 3
10 20 30 10 20 40
output
2
1
1
3
5
1
6

Note

The first example case corresponds to the picture above. The sequence of operations displayed there is optimal, so the answer is 2 2 2.

In the second example case, no operations can be performed, so the answer is 1 1 1.

In the fourth example case, you can repeatedly select 4 4 4 cards numbered with 1 1 1 and replace them with 3 3 3 cards numbered with 1 1 1, until there are 3 3 3 cards left.

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 = 1e2 + 10;

int cnt[N];

void solve() {
	memset(cnt, 0, sizeof cnt);
	int n, k;
	cin >> n >> k;

	std::vector<int> a(n + 1);
	int mx = 0;
	for (int i = 1; i <= n; i ++)
		cin >> a[i], cnt[a[i]] ++, mx = max(mx, cnt[a[i]]);

	if (mx < k) {
		cout << n << endl;
	} else {
		cout << k - 1 << endl;
	}
}

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

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

B. Rectangle Filling

Problem Statement

There is an n × m n \times m n×m grid of white and black squares. In one operation, you can select any two squares of the same color, and color all squares in the subrectangle between them that color.

Formally, if you select positions ( x 1 , y 1 ) (x_1, y_1) (x1,y1) and ( x 2 , y 2 ) (x_2, y_2) (x2,y2), both of which are currently the same color c c c, set the color of all ( x , y ) (x, y) (x,y) where min ⁡ ( x 1 , x 2 ) ≤ x ≤ max ⁡ ( x 1 , x 2 ) \min(x_1, x_2) \le x \le \max(x_1, x_2) min(x1,x2)xmax(x1,x2) and min ⁡ ( y 1 , y 2 ) ≤ y ≤ max ⁡ ( y 1 , y 2 ) \min(y_1, y_2) \le y \le \max(y_1, y_2) min(y1,y2)ymax(y1,y2) to c c c.

This diagram shows a sequence of two possible operations on a grid:

Is it possible for all squares in the grid to be the same color, after performing any number of operations (possibly zero)?

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and m m m ( 1 ≤ n , m ≤ 500 1 \le n, m \le 500 1n,m500) — the number of rows and columns in the grid, respectively.

Each of the next n n n lines contains m m m characters ‘W’ and ‘B’ — the initial colors of the squares of the grid.

It is guaranteed that the sum of n ⋅ m n\cdot m nm over all test cases does not exceed 3 ⋅ 1 0 5 3\cdot 10^5 3105.

Output

For each test case, print “YES” if it is possible to make all squares in the grid the same color, 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

Example

input
8
2 1
W
B
6 6
WWWWBW
WBWWWW
BBBWWW
BWWWBB
WWBWBB
BBBWBW
1 1
W
2 2
BB
BB
3 4
BWBW
WBWB
BWBW
4 2
BB
BB
WW
WW
4 4
WWBW
BBWB
WWBB
BBBB
1 5
WBBWB
output
NO
YES
YES
YES
YES
NO
YES
NO

Note

In the first example, it is impossible to ever change the color of any square with an operation, so we output NO.

The second example is the case pictured above. As shown in that diagram, it is possible for all squares to be white after two operations, so we output YES.

In the third and fourth examples, all squares are already the same color, so we output YES.

In the fifth example we can do everything in two operations. First, select positions ( 2 , 1 ) (2, 1) (2,1) and ( 1 , 4 ) (1, 4) (1,4) and color all squares with 1 ≤ x ≤ 2 1 \le x \le 2 1x2 and 1 ≤ y ≤ 4 1 \le y \le 4 1y4 to white. Then, select positions ( 2 , 1 ) (2, 1) (2,1) and ( 3 , 4 ) (3, 4) (3,4) and color all squares with 2 ≤ x ≤ 3 2 \le x \le 3 2x3 and 1 ≤ y ≤ 4 1 \le y \le 4 1y4 to white. After these two operations all squares are white.

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 = 5e2 + 10;

int n, m;
int g[N][N], s[N][N];

int sum(int x1, int y1, int x2, int y2) {
	return s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
}

void solve() {
	cin >> n >> m;

	char x;
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= m; j ++) {
			cin >> x;
			g[i][j] = (x == 'B');
		}
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= m; j ++)
			s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + g[i][j];

	int flg = 1;
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= m; j ++)
			if (!g[i][j] && ((sum(1, 1, i, j) && sum(i, j, n, m)) || (sum(i, 1, n, j) && sum(1, j, i, m))))
				continue;
			else if (!g[i][j]) {
				flg = 0;
				break;
			}
	if (flg) {
		cout << "YES" << endl;
		return;
	}
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= m; j ++)
			s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (!g[i][j]);

	flg = 1;
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= m; j ++)
			if (g[i][j] && ((sum(1, 1, i, j) && sum(i, j, n, m)) || (sum(i, 1, n, j) && sum(1, j, i, m))))
				continue;
			else if (g[i][j]) {
				flg = 0;
				break;
			}
	if (flg) {
		cout << "YES" << endl;
		return;
	}
	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;
}

C. Everything Nim

Problem Statement

Alice and Bob are playing a game on n n n piles of stones. On each player’s turn, they select a positive integer k k k that is at most the size of the smallest nonempty pile and remove k k k stones from each nonempty pile at once. The first player who is unable to make a move (because all piles are empty) loses.

Given that Alice goes first, who will win the game if both players play optimally?

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. The description of the test cases follows.

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

The next 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 9 1 \le a_i \le 10^9 1ai109), where a i a_i ai is the initial number of stones in the i i i-th pile.

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, print a single line with the name of the winner, assuming both players play optimally. If Alice wins, print “Alice”, otherwise print “Bob” (without quotes).

Example

input
7
5
3 3 3 3 3
2
1 7
7
1 3 9 7 4 2 100
3
1 2 3
6
2 1 3 4 2 4
8
5 7 2 9 6 3 3 2
1
1000000000
output
Alice
Bob
Alice
Alice
Bob
Alice
Alice

Note

In the first test case, Alice can win by choosing k = 3 k=3 k=3 on her first turn, which will empty all of the piles at once.

In the second test case, Alice must choose k = 1 k=1 k=1 on her first turn since there is a pile of size 1 1 1, so Bob can win on the next turn by choosing k = 6 k=6 k=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;

const int N = 2e5 + 10;

int n;
int a[N];

void solve() {
	cin >> n;

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

	sort(a + 1, a + 1 + n);
	n = unique(a + 1, a + 1 + n) - a - 1;

	int cnt = 0;
	for (int i = 1; i <= n; i ++)
		if (a[i] == a[i - 1] + 1)
			cnt ++;
		else
			break;

	if (n - cnt == 0) {
		if (n & 1) cout << "Alice" << endl;
		else cout << "Bob" << endl;
	}
	else if (cnt & 1) {
		cout << "Bob" << endl;
	} else {
		cout << "Alice" << endl;
	}
}

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

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

D. Missing Subsequence Sum

Problem Statement

You are given two integers n n n and k k k. Find a sequence a a a of non-negative integers of size at most 25 25 25 such that the following conditions hold.

  • There is no subsequence of a a a with a sum of k k k.
  • For all 1 ≤ v ≤ n 1 \le v \le n 1vn where v ≠ k v \ne k v=k, there is a subsequence of a a a with a sum of v v v.

A sequence b b b is a subsequence of a a a if b b b can be obtained from a a a by the deletion of several (possibly, zero or all) elements, without changing the order of the remaining elements. For example, [ 5 , 2 , 3 ] [5, 2, 3] [5,2,3] is a subsequence of [ 1 , 5 , 7 , 8 , 2 , 4 , 3 ] [1, 5, 7, 8, 2, 4, 3] [1,5,7,8,2,4,3].

It can be shown that under the given constraints, a solution always exists.

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of test cases. The description of the test cases follows.

Each test case consists of a single line containing two integers n n n and k k k ( 2 ≤ n ≤ 1 0 6 2 \le n \le 10^6 2n106, 1 ≤ k ≤ n 1 \le k \le n 1kn) — the parameters described above.

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 7 10^7 107.

Output

The first line of output for each test case should contain a single integer m m m ( 1 ≤ m ≤ 25 1 \le m \le 25 1m25) — the size of your chosen sequence.

The second line of output for each test case should contain m m m integers a i a_i ai ( 0 ≤ a i ≤ 1 0 9 0 \le a_i \le 10^9 0ai109) — the elements of your chosen sequence.

If there are multiple solutions, print any.

Example

Example

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

Note

In the first example, we just need a subsequence that adds up to 1 1 1, but not one that adds up to 2 2 2. So the array a = [ 1 ] a=[1] a=[1] suffices.

In the second example, all elements are greater than k = 1 k=1 k=1, so no subsequence adds up to 1 1 1. Every other integer between 1 1 1 and n n n is present in the array, so there is a subsequence of size 1 1 1 adding up to each of those numbers.

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 = 1e6 + 10;

int n, k;
int f[N];

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

	f[0] = 1;
	std::vector<int> res;
	int i;
	for (i = 1; i * 2 - 1 < k; i *= 2) {
		res.emplace_back(i);
		for (int j = n; j >= i; j --)
			f[j] |= f[j - i];
	}
	if (k - i > 0) {
		res.emplace_back(k - i);
		for (int j = n; j >= k - i; j --)
			f[j] |= f[j - (k - i)];
	}

	for (i = k + 1; i <= n; i ++)
		if (!f[i]) {
			res.emplace_back(i);
			for (int j = n; j >= i; j --)
				f[j] |= f[j - i];
		}

	cout << res.size() << endl;
	for (auto v : res)
		cout << v << " ";
	cout << endl;
	for (int i = 0; i <= n; i ++)
		f[i] = 0;
}

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

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

E. Folding Strip

Problem Statement

You have a strip of paper with a binary string s s s of length n n n. You can fold the paper in between any pair of adjacent digits.

A set of folds is considered valid if after the folds, all characters that are on top of or below each other match. Note that all folds are made at the same time, so the characters don’t have to match in between folds.

For example, these are valid foldings of s = 110110110011 s = \mathtt{110110110011} s=110110110011 and s = 01110 s = \mathtt{01110} s=01110:

The length of the folded strip is the length seen from above after all folds are made. So for the two above examples, after the folds shown above, the lengths would be 7 7 7 and 3 3 3, respectively.

Notice that for the above folding of s = 01110 s = \mathtt{01110} s=01110, if we made either of the two folds on their own, that would not be a valid folding. However, because we don’t check for validity until all folds are made, this folding is valid.

After performing a set of valid folds, what is the minimum length strip you can form?

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. The description of the test cases follows.

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

The second line of each test case contains a string s s s of n n n characters ‘0’ and ‘1’ — a description of the digits on the strip.

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 a single integer — the minimum possible length of the strip after a valid folding.

Example

input
6
6
101101
1
0
12
110110110011
5
01110
4
1111
2
01
output
3
1
3
3
1
2

Note

For the first example case, one optimal folding is to fold the strip in the middle, which produces a strip of length 3.

The third and fourth example cases correspond to the images above. Note that the folding shown above for s = 110110110011 s = \mathtt{110110110011} s=110110110011 is not of minimal length.

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, s = ' ' + s;

	int mn = 0, mx = 0, cur = 0;
	for (int i = 1; i <= n; i ++) {
		if ((cur & 1) == (s[i] == '1'))
			cur ++;
		else
			cur --;
		mn = min(mn, cur), mx = max(mx, cur);
	}

	cout << mx - mn << 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 941 (Div. 2)(A ~ E 题讲解)


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