The meaning of problems: give you the n k colors, each point has two coordinates and color attributes, select a length of the interval as short as possible, so that the point of each color are present in the interval.
data range:
For 50% of the data, N≤10000;
For 80% of the data, N≤800000;
To 100% of the data, 1≤N≤1000000,1≤K≤60, 0 ≦ beads position <2 ^ {31} .
-------------------------------------------------- I'm dividing line ---------------------------------------------- ---------
Solution: monotonic foot emulated classic application. The coordinate point of first order, two variableslandrto enumerate interval,ifltorinterval does not satisfy the requirements,r++,ifltorsections meet, the answer records,l ++.
Sort time complexity O (NlogN), foot takes time complexity O (N), the total time complexity is O (NlogN).
#include<bits/stdc++.h>
#define ll long long
#define mp make_pair
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
using namespace std;
typedef pair<int, int> pii;
typedef double db;
const int N = 1e6 + 50;
struct node { int x, id; } a[N];
int n, k, h, vis[N], q[N], u, ans = 1e9, cnt;
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar();}
while(ch >='0' && ch <='9') { x = (x<<3)+(x<<1)+(ch^48); ch = getchar();}
return x*f;
}
bool mycmp(node a, node b){ return a.x < b.x; }
void insert(int x) { if(vis[x] == 0) cnt++; vis[x]++; }
void remove(int x) { if(vis[x] == 1) cnt--; vis[x]--; }
void init(){
n = read(); k = read();
rep(i, 1, k){
int t = read();
rep(j, 1, t) a[++h].x = read(), a[h].id = i;
}
sort(a+1, a+n+1, mycmp);
}
void work(){
int l = 1, r = 1;
while(r <= n){
insert(a[r].id);
while(true) {
remove(a[l].id);
if(cnt == k) l++;
else { insert(a[l].id); break;}
}
if(cnt == k) ans = min(ans, a[r].x - a[l].x);
r++;
}
printf("%d\n", ans);
}
int main(){
init();
work();
return 0;
}