标签:
O(n)扫一遍就可以了,r是递增的
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(register int i=s;i<=t;i++)
#define dwn(i,s,t) for(register int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-‘0‘,c=getchar();
return x;
}
const int nmax=1e6+5;
const int inf=0x7f7f7f7f;
struct nd{
int a,b;
bool operator<(const nd&rhs)const {
return b<rhs.b;}
};
nd ns[nmax];
int t[71];
void mins(int &a,int b){
if(a>b) a=b;
}
int main(){
int n=read(),m=read(),u,cnt=0;
rep(i,1,m){
u=read();
rep(j,1,u) ns[++cnt].b=read(),ns[cnt].a=i;
}
sort(ns+1,ns+n+1);
//rep(i,1,n) printf("%d %d\n",ns[i].a,ns[i].b);
int r=0,ans=inf;cnt=0;
rep(i,1,n){
if(i!=1) if(!--t[ns[i-1].a]) cnt--;
while(cnt<m){
if(++r>n) break;
if(++t[ns[r].a]==1) cnt++;
}
if(cnt!=m) break;
mins(ans,ns[r].b-ns[i].b);
//printf("%d %d %d %d\n",i,r,ns[i].b,ns[r].b);
}
printf("%d\n",ans);
return 0;
}
有多种方案可选,其中比较短的是1~5和5~8。后者长度为3最短。
【数据规模】
对于50%的数据, N≤10000;
对于80%的数据, N≤800000;
对于100%的数据,1≤N≤1000000,1≤K≤60,0≤彩珠位置<2^31。
标签:
原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5858100.html