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 k−1 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 1≤t≤500) — 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 1≤n≤100, 2 ≤ k ≤ 100 2 \le k \le 100 2≤k≤100) — 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 1≤ci≤100) — 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)≤x≤