题目链接
Groundhog finds that Apple seems to be less intimate than before.
He is very distressed for this.After pondering for a long time, Groundhog finds that he looks too dowdy.So, Groundhog decided to improve a little.
Because Groundhog is lazy, he only lists the clothes that can be worn for the next n {n} n days. On the i t h {i^{th}} ith day, the j t h {j^{th}} jth clothes have a dowdiness a i , j a_{i,j} ai,j.On each day,he will choose one of the clothes and wear it.
And Groundhog should choose m {m} m days from n {n} n days to go out with Apple.
Groundhog wants to know the minimum difference between the maximum dowdiness and the minimum dowdiness in m {m} m days when Groundhog’s choice is optimal.
Simplified: You should choose n clothes for each day and then choose m clothes from those n clothes, and the problem is to calculate the minimum difference between the maximum dowdiness and the minimum dowdiness.
The first line contains two integers n {n} n and m {m} m.
Then {n}n lines follow,each line contains a integer k i k_i ki,represents the number of the clothes that can be worn on i t h {i^{th}} ith day.Then k i k_i ki integers a i , j a_{i,j} ai,j follow.
Print in one line the minimum difference.
4 3
1 3
2 8 6
1 2
3 1 7 5
2
典型的尺取,存两个值,一个 d o w d i n e s s dowdiness dowdiness,还有一个下标 i ∈ [ 1 , n ] i\in[1,n] i∈[1,n],按 d o w d i n e s s dowdiness dowdiness 进行排序,然后尺取即可,每当种类达到 m m m 就更新答案即可,尺取的难点就在于左端点的更新,注意细节即可。吐槽一下,我看有通过代码有人直接按最小值排序混过的,很无语,证明数据还是太水了,AC代码如下:
#include
using namespace std;
const int N=2e6+5;
vector<pair<int,int>>good;
int mp[N]={0};
int main()
{
int n,m,k,x;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&k);
for(int j=0;j<k;j++){
scanf("%d",&x);
good.push_back({x,i});
}
}
sort(good.begin(),good.end());
int l=0,cnt=0,ans=1e9,len=good.size();
for(int i=0;i<len;i++){
if(mp[good[i].second]==0){
mp[good[i].second]++;
cnt++;
}else mp[good[i].second]++;
while(cnt>=m){
if(mp[good[l].second]>1) mp[good[l].second]--,l++;
else{
if(cnt-1>=m) mp[good[l].second]=0,l++,cnt--;
else break;
}
}
if(cnt==m) ans=min(ans,good[i].first-good[l].first);
}
printf("%d",ans);
return 0;
}