【每日一题】生日礼物
[SCOI2009]生日礼物
https://ac.nowcoder.com/acm/problem/20565
题意:
思路:
#include <cstdio> #include <vector> #include <algorithm> #include <iostream> using namespace std; const int N = 1e6 + 10; const int inf = 0x3f3f3f3f; vector<int> v; struct Pos{ int pos,val; }a[N]; bool cmp(Pos a,Pos b){ return a.pos < b.pos; } int n,k,num[65],cnt; void add(int i){ num[a[i].val]++; if(num[a[i].val] == 1) cnt++; } void del(int i){ num[a[i].val]--; if(num[a[i].val] == 0) cnt--; } int main(){ scanf("%d%d",&n,&k); int idx = 1; for(int i = 1,x;i <= k;i++){ scanf("%d",&x); for(int j = 1;j <= x;j++){ scanf("%d",&a[idx].pos); a[idx].val = i; idx++; } } sort(a + 1,a + 1 + n,cmp); int l = 1,r = 0; int ans = inf; while(l <= n){ while(r < n && cnt < k) add(++r); if(cnt == k){ ans = min(ans,a[r].pos - a[l].pos); } del(l++); } printf("%d\n",ans); return 0; }
每日一题 文章被收录于专栏
每题一题题目