标签:
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 118 Accepted Submission(s): 69
Special Judge
#include<bits/stdc++.h> using namespace std; const int maxn=1e5+20; const int INF=0x3f3f3f3f; struct Soda{ int l,r,len,ord; bool operator < (const Soda &b)const { return r>b.r; } }sodas[maxn]; priority_queue<Soda>PQ; int ans[maxn],vis[maxn],b[maxn]; //b数组是没有去的soda编号,ans数组是去了 bool cmp(Soda a,Soda b){ if(a.l!=b.l){ return a.l<b.l; }else{ return a.len<b.len; } } int main(){ int t,n; scanf("%d",&t); while(t--){ memset(vis,0,sizeof(vis)); scanf("%d",&n); int flag=0; for(int i=0;i<n;i++){ scanf("%d",&sodas[i].l); if(sodas[i].l==0){ flag=1; } } for(int i=0;i<n;i++){ scanf("%d",&sodas[i].r); sodas[i].ord=i+1; sodas[i].len=sodas[i].r-sodas[i].l+1; } if(!flag){ printf("0\n",n); printf("1"); for(int i=2;i<=n;i++){ printf(" %d",i); }printf("\n"); continue; } sort(sodas,sodas+n,cmp); int sum=0,kk=0,kt=0,nn=0; int fg=0;Soda tmp; for(int i=0;i<n;i++){ if(!fg){ if(sodas[i].l<=kt){ PQ.push(sodas[i]); }else{ i--; int mark=0; while(!PQ.empty()){ tmp=PQ.top(); PQ.pop(); if(tmp.r>=kt){ mark=1; kt++; ans[kk++]=tmp.ord; break; }else{ b[nn++]=tmp.ord; } } if(mark==0&&PQ.empty()) fg=1; } }else{ b[nn++]=sodas[i].ord; } } while(!PQ.empty()){ tmp=PQ.top(); PQ.pop(); if(tmp.r>=kt){ kt++; ans[kk++]=tmp.ord; }else{ b[nn++]=tmp.ord; } } printf("%d\n",kt); printf("%d",ans[0]); ans[0]=0; for(int i=1;i<kk;i++){ printf(" %d",ans[i]); ans[i]=0; } for(int i=0;i<nn;i++){ printf(" %d",b[i]); b[i]=0; }printf("\n"); } return 0; }
HDU 5360——Hiking——————【贪心+优先队列】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4708894.html