50分求助
查看原帖
50分求助
591428
tangguochao楼主2022/2/21 17:10
#include "bits/stdc++.h"
using namespace std;
const int N = 1000005;
typedef pair<int , int> PII;
int idx,id[N], hh=1;
PII f[N], ans = {1, N};
int main()
{
    cin.tie(0);
    fill(id, id + N, -1);
    int n, k; cin >> n >> k;
    for(int i=1; i<=k; i++)
    {
        int t; cin >> t;
        for(int j=1; j<=t; j++)
        {
            cin >> f[++idx].first;
            f[idx].second = i;
        }
    }
    sort(f+1, f+1+n);
   int cnt = 0;
    for(int i=1; i<=n; i++)
    {
       if(id[f[i].second]==-1)cnt++;
       id[f[i].second] = f[i].first;
        while (hh<=i&&f[hh].first != id[f[hh].second])  hh++;
        if(cnt==k && f[i].first - f[hh].first < ans.second - ans.first)
            ans = {f[hh].first, f[i].first};
    }
    cout << ans.second - ans.first;
    return 0;
}
2022/2/21 17:10
加载中...