先要知道题目 如果能被做到那么时间则为 , 为在做 之前做了多少道题,某种做题方式可行需要满足 最晚被做到的题的时间 ,按照这个性质我们应该先尽量选最大的 。发现若按先后顺序做了题 那么有 ,设 的值域集合所构成的极长连续段集合 ,我们会从最后一个 的区间中一个题开始,然后可知我们每到达一个点就一定能把它所对应的连续段做完,而且这样做更优;做完当前连续段后跳到前一个段继续重复直到 数量用完即可。我们二分答案 表示我们做题的数量,判断用这种方法做的最晚结束时间是否 。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define rep(i,X,Y) for(int i=(X);i<=(Y);i++)
#define Rep(i,X,Y) for(int i=(X);i<(Y);i++)
#define per(i,X,Y) for(int i=(X);i>=(Y);i--)
#define ALL(X) X.begin(),X.end()
#define PII pair<int,int>
#define fr first
#define sc second
const int N=1e6+10;
int n,k,c[N],cnt[N],f[N];
vector<PII> v;
int m;
int calc(int x) {
int sum=0;
per(i,m,1) {
sum+=f[i];
if(sum>=x) {
return k-v[i-1].sc+2*(x-1)+1;
}
}
return 1e9;
}
int solve() {
rep(i,0,1e6) cnt[i]=f[i]=0; v.clear();
cin>>n>>k;
rep(i,1,n) {
cin>>c[i];
cnt[c[i]]++;
}
sort(c+1,c+n+1);
int lst=0,sum=0;
rep(i,1,N-1) {
if(!cnt[i]) {
if(i>lst+1) {
v.push_back({lst+1,i-1});
f[v.size()]=sum;
}
lst=i;
sum=0;
} sum+=cnt[i];
}
m=v.size();
for(;m>0&&v[m-1].fr>k;m--);
int l=0,r=n;
while(l<=r) {
int mid=(l+r)>>1;
if(calc(mid)>n) r=mid-1;
else l=mid+1;
}
cout<<r<<'\n';
return 0;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T; cin>>T;
while(T--) {solve();}
return 0;
}