题目大意:
每天穿的衣服有数值val,从n天选m天使得val_max-val_min最小
Analysis:
尺取,维护一个m天的滑动窗口即可
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using i128 = __int128;
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int maxn = 1e6+10;
int n,m;
struct ty{
i64 val;
int id;
};
int cnt;
int vis[maxn];
int main(){
ios;
cin>>n>>m;
vector<ty>a;
for(int i = 1;i<=n;++i){
int k;cin>>k;
for(int j = 1;j<=k;++j){
int val;cin>>val;
a.push_back({val,i});
}
}
sort(a.begin(),a.end(),[](ty a,ty b)->bool{
return a.val<b.val;
});
i64 ans = 0x3f3f3f3f3f3f3f3f;
int sum = 0;
int l = 0;
for(int i = 0;i<=a.size()-1;++i){
sum+=(vis[a[i].id]==0);
vis[a[i].id]++;
while(sum==m&&l<=i){
ans = min(ans,a[i].val-a[l].val);
vis[a[l].id]--;
if(vis[a[l].id]==0) sum--;
l++;
}
}
cout<<ans<<"\n";
return 0;
}