标签:des style blog http color os io for 数据
有多种方案可选,其中比较短的是1~5和5~8。后者长度为3最短。
【数据规模】
对于50%的数据, N≤10000;
对于80%的数据, N≤800000;
对于100%的数据,1≤N≤1000000,1≤K≤60,0≤彩珠位置<2^31。
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #define inf 1<<30 12 #define maxn 500+100 13 #define maxm 500+100 14 #define eps 1e-10 15 #define ll long long 16 #define pa pair<int,int> 17 using namespace std; 18 inline int read() 19 { 20 int x=0,f=1;char ch=getchar(); 21 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 22 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 23 return x*f; 24 } 25 vector<int>a[61]; 26 priority_queue<pa,vector<pa>,greater<pa> >q; 27 int n,k,mi,mx,ans; 28 int main() 29 { 30 freopen("input.txt","r",stdin); 31 freopen("output.txt","w",stdout); 32 n=read();k=read(); 33 for(int i=1;i<=k;i++) 34 { 35 int x=read(),y; 36 for(int j=1;j<=x;j++)y=read(),a[i].push_back(y); 37 } 38 mi=inf;mx=0; 39 for(int i=1;i<=k;i++) 40 { 41 q.push(make_pair(a[i][0],(i-1)*n+1)); 42 if(a[i][0]>mx)mx=a[i][0];if(a[i][0]<mi)mi=a[i][0]; 43 } 44 ans=mx-mi;//cout<<mx<<‘ ‘<<mi<<endl; 45 while(1) 46 { 47 pa now=q.top();q.pop(); 48 int x=(now.second-1)/n+1,y=(now.second-1)%n+1; 49 //cout<<now.first<<‘ ‘<<now.second<<‘ ‘<<x<<‘ ‘<<y<<‘ ‘<<a[x].size()<<endl; 50 if(y==a[x].size())break; 51 q.push(make_pair(a[x][y],(x-1)*n+y+1)); 52 mi=q.top().first; 53 if(a[x][y]>mx)mx=a[x][y]; 54 ans=min(ans,mx-mi);//cout<<mx<<‘ ‘<<mi<<endl; 55 } 56 printf("%d\n",ans); 57 return 0; 58 }
标签:des style blog http color os io for 数据
原文地址:http://www.cnblogs.com/zyfzyf/p/3930654.html