习题-[SCOI2009]生日礼物
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
bool vis[1000010];
unordered_map<int, int> mp;
signed main(){
int n, k; cin >> n >> k;
vector<PII> p;
for(int i = 1; i <= k; ++i){
int t; cin >> t;
for(int j = 1; j <= t; ++j){
int x; cin >> x;
p.push_back({x, i});
}
}
sort(p.begin(), p.end());
long long res = 1e9;
long long l = 0, r = 0;
int cnt = 0;
while(r < n){
if(!mp[p[r].second]) ++cnt;
mp[p[r].second]++;
while(cnt == k && mp[p[l].second] > 1){
mp[p[l].second]--;
if(!mp[p[l].second]) --cnt;
++l;
res = min(res, p[r].first - p[l].first - 0ll);
}
++r;
}
cout << res << endl;
return 0;
}