思路:
先排个序,然后类似一个框框住,然后直接找最小值和最大值
c o d e code code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN = 1e6 + 10, MAXN_ = 1e5 + 10;
int n, l[MAXN_], tot, c[MAXN_];
struct node {
int x, id;
}a[MAXN];
struct abc {
int maxx, minn;
}tr1[MAXN_ << 2], tr2[MAXN_ << 2];
queue<int> q[MAXN_];
bool v[MAXN_];
bool cmp(node x, node y) {
return x.x < y.x;
}
void change1(int k, int l, int r, int x, int y) {
tr1[k].minn = 1e9;
if(l > r) return ;
if(l == r && l == x) {
tr1[k].minn = y;
return ;
}
int mid = l + r >> 1;
if(x <= mid) change1(k << 1, l, mid, x, y);
if(x > mid) change1(k << 1 | 1, mid + 1, r, x, y);
tr1[k].minn = min(tr1[k << 1].minn, tr1[k << 1 | 1].minn);
}
void change2(int k, int l, int r, int x, int y) {
tr2[k].maxx = -1e9;
if(l > r) return ;
if(l == r && l == x) {
tr2[k].maxx = y;
return ;
}
int mid = l + r >> 1;
if(x <= mid) change2(k << 1, l, mid, x, y);
if(x > mid) change2(k << 1 | 1, mid + 1, r, x, y);
tr2[k].maxx = max(tr2[k << 1].maxx, tr2[k << 1 | 1].maxx);
}
int main() {
freopen("military.in", "r", stdin);
freopen("military.out", "w", stdout);
scanf("%d", &n);
for(int i = 1; i <= n; i ++) {
scanf("%d%d", &l[i], &c[i]);
for(int j = 1; j <= l[i]; j ++) scanf("%d", &a[++ tot].x), a[tot].id = i;
}
sort(a + 1, a + 1 + tot, cmp);
int ans = 1e9, tmp = 0;
for(int i = 1; i <= tot; i ++) {
int id = a[i].id, x = a[i].x;
if(q[id].size() < c[id]) {
q[id].push(x);
if(q[id].size() == c[id]) change1(1, 1, n, id, q[id].front()), change2(1, 1, n, id, x);
if(q[id].size() == c[id] && v[id] == 0) tmp++, v[id] = 1;
}
else {
if(v[id] == 0) tmp ++, v[id] = 1;
q[id].pop(), q[id].push(x);
change1(1, 1, n, id, q[id].front());
change2(1, 1, n, id, x);
}
if(tmp == n) ans = min(ans, tr2[1].maxx - tr1[1].minn);
}
printf("%d", ans);
return 0;
}