[ICPC 2024 Hangzhou R] Kind of Bingo题解
time limit per test
1 second
memory limit per test
1024 megabytes
There is a grid with n rows and m columns. The cells in the grid are numbered from 1 to n×m, where the cell on the i-th row and the j-th column is numbered as ((i−1)×m+j).
Given a permutation p1,p2,⋯,pn×m of n×m, we're going to perform n×m operations according to the permutation. For the i-th operation, we'll mark cell pi. If after the b-th operation, there is at least one row such that all the cells in that row are marked, and b is as small as possible, then we say b is the "bingo integer" of the permutation.
You're given the chance to modify the permutation at most k times (including zero times). Each time you can swap a pair of elements in the permutation. Calculate the smallest possible bingo integer after the modifications.
Recall that a sequence p1,p2,⋯,pn×m of length n×m is a permutation of n×m if and only if each integer from 1 to n×m (both inclusive) appears exactly once in the sequence.
有道 翻译
有一个包含 n 行和 m 列的网格。网格中的单元格编号从 1 到 n×m ,其中第 i 行和第 j 列上的单元格编号为 ((i−1)×m+j) 。
给定 n×m 的一个排列 p1,p2,⋯,pn×m ,我们将根据该排列执行 n×m 操作。对于第 i }次操作,我们将标记单元格 pi 。如果在 b \第一次操作之后,至少有一行使得该行中的所有单元格都被标记,并且 b 尽可能小,那么我们说 b 是该排列的“宾果整数”。
您最多有机会修改 k 次排列(包括0次)。每次你都可以交换排列中的一对元素。计算修改后可能的最小宾果整数。
回想一下,长度为 n×m 的序列 p1,p2,⋯,pn×m 是 n×m 的排列,当且仅当从 1 到 n×m (包括两个)的每个整数在序列中恰好出现一次。
Input
There are multiple test cases. The first line of the input contains an integer T (1≤T≤104) indicating the number of test cases. For each test case:
The first line contains three integers n, m and k (1≤n,m≤105, 1≤n×m≤105, 0≤k≤109), indicating the number of rows and columns of the grid and the number of modifications you can perform.
The second line contains n×m distinct integers p1,p2,⋯,pn×m (1≤pi≤n×m).
It's guaranteed that the sum of n×m of all test cases will not exceed 105.
有道 翻译
输入** **
有多个测试用例。输入的第一行包含一个整数 T ( 1≤T≤104 ),表示测试用例的数量。对于每个测试用例:
第一行包含三个整数 n , m 和 k ( 1≤n,m≤105 , 1≤n×m≤105 , 0≤k≤109 ),表示网格的行数和列数以及可以执行的修改次数。
第二行包含 n×m 不同的整数 p1,p2,⋯,pn×m ( 1≤pi≤n×m )。
保证所有测试用例的 n×m 之和不超过 105 。
Output
For each test case output one line containing one integer indicating the smallest possible bingo integer after the modifications.
有道 翻译
** **输出
对于每个测试用例输出一行,其中包含一个整数,表示修改后可能的最小bingo整数。
Example
Input
Copy
3
3 5 2
1 4 13 6 8 11 14 2 7 10 3 15 9 5 12
2 3 0
1 6 4 3 5 2
2 3 1000000000
1 2 3 4 5 6
Output
Copy
7 5 3
Note
For the first sample test case, we can first swap 1 and 15, then swap 6 and 12 to get the sequence [15,4,13,12,8,11,14,2,7,10,3,1,9,5,6]. It's easy to see that after the 7-th operation, all cells in the 3-rd row will be marked.
For the second sample test case, it's easy to see that after the 5-th operation, all cells in the 2-nd row will be marked.
For the third sample test case, we don't need to make any modifications. It's easy to see that after the 3-rd operation, all cells in the 1-st row will be marked.
有道 翻译
注意
对于第一个示例测试用例,我们可以首先交换 1 和 15 ,然后交换 6 和 12 ,以获得序列 [15,4,13,12,8,11,14,2,7,10,3,1,9,5,6] 。很容易看到,在 7 \第二次操作之后, 3 -rd行的所有单元格都将被标记。
对于第二个示例测试用例,很容易看到,在 5 第th次操作之后, 2 -nd行的所有单元格都将被标记。
对于第三个样例测试用例,我们不需要做任何修改。很容易看出,在 3 -rd操作之后, 1 -st行的所有单元格都将被标记。
思路
贪心即可。
代码见下
#include<bits/stdc++.h>
using namespace std;
long long t,n,m,k,p[100005],lk=1e18+7;
vector<long long> v[100005];
int main(){
cin>>t;
while(t--){
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
v[i].clear();
}
for(int i=1;i<=n*m;i++){
cin>>p[i];
if(p[i]%m!=0){
v[p[i]/m+1].push_back(i);
}
else{
v[p[i]/m].push_back(i);
}
}
lk=1e18+7;
for(int i=1;i<=n;i++){
lk=min(lk,v[i][max(m-1-k,0ll)]);
}
lk=max(lk,m);
cout<<lk<<endl;
}
return 0;
}

浙公网安备 33010602011771号