bzoj 1293: [SCOI2009]生日礼物

思路:尺取一下

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define fi first
 4 #define se second
 5 #define mk make_pair
 6 #define pii pair<int,int>
 7 #define piii pair<int, pair<int,int>>
 8 
 9 using namespace std;
10 
11 const int N = 1e6 + 7;
12 const int M = 1e4 + 7;
13 const int inf = 0x3f3f3f3f;
14 const LL INF = 0x3f3f3f3f3f3f3f3f;
15 const int mod = 1e9 + 7;
16 
17 int n, k, hs[N], tot, cnt[66];
18 vector<int> vec[66];
19 vector<int> w[N];
20 LL a[N];
21 
22 int check() {
23     int l = 0, r = 0, ans = inf;
24     LL ret = 0;
25     while(r < tot) {
26         while(ret + 1 != 1ll << k && r < tot) {
27             for(int j = 0; j < w[r].size(); j++) {
28                 int u = w[r][j];
29                 if(!cnt[u]) {
30                     ret |= 1ll << u;
31                 }
32                 cnt[u]++;
33             }
34             r++;
35         }
36         if(ret + 1 == 1ll << k) ans = min(ans, hs[r - 1] - hs[l]);
37         else break;
38 
39         for(int j = 0; j < w[l].size(); j++) {
40             int u = w[l][j];
41             if(cnt[u] == 1) {
42                 ret ^= 1ll << u;
43             }
44             cnt[u]--;
45         }
46         l++;
47     }
48     return ans;
49 }
50 
51 int main() {
52     scanf("%d%d", &n, &k);
53     for(int i = 0; i < k; i++) {
54         int num; scanf("%d", &num);
55         while(num--) {
56             int x; scanf("%d", &x);
57             hs[tot++] = x;
58             vec[i].push_back(x);
59         }
60     }
61 
62     sort(hs, hs + tot);
63     tot = unique(hs, hs + tot) - hs;
64     for(int i = 0; i < k; i++) {
65         for(int j = 0; j < vec[i].size(); j++) {
66             int pos = lower_bound(hs, hs + tot, vec[i][j]) - hs;
67             w[pos].push_back(i);
68         }
69     }
70     int ans = check();
71     printf("%d\n", ans);
72     return 0;
73 }
74 
75 /*
76 */

 

posted @ 2018-05-20 16:37  NotNight  阅读(137)  评论(0编辑  收藏  举报