标签:long des break -- com 优先队列 优先 other mod
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19208 Accepted Submission(s):
6563
#include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<stack> #include<vector> #include<algorithm> //#include<cmath> using namespace std; const int INF = 9999999; #define LL long long inline int read(){ int x=0,f=1;char c=getchar(); for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1; for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘; return x*f; } int N,M; int a[100001]; int heap[100001]; int len; void insert(int x){ heap[++len]=x; for(int k=len;k!=1;k/=2){ if(heap[k]<heap[k/2]) swap(heap[k],heap[k/2]); else break; } return ; } void delet(){ if(len==0) return ; heap[1]=heap[len];len--; for(int k=1;k*2<=len;){ if((k*2<=len)&&(k*2+1>len)){ if(heap[k*2]<heap[k]) swap(heap[k*2],heap[k]),k*=2; else break; } else{ if(heap[k*2]<heap[k]||heap[k*2+1]<heap[k]){ if(heap[k*2]<heap[k*2+1]) swap(heap[k],heap[k*2]),k*=2; else swap(heap[k],heap[k*2+1]),k*=2,k++; } else break; } } return ; } int ans[100001]; int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); while(scanf("%d%d",&N,&M)!=EOF){ len=0; for(int i=1;i<=N;i++) a[i]=read(); for(int i=1;i<=N;i++) for(int j=i+1;j<=N;j++){ insert(a[i]+a[j]); if(len>M) delet(); } for(int i=1;i<=M;i++) ans[i]=heap[1],delet(); for(int i=M;i>1;i--) printf("%d ",ans[i]); printf("%d\n",ans[1]); } return 0; }
优先队列代码:
#include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<stack> #include<vector> #include<algorithm> //#include<cmath> using namespace std; const int INF = 9999999; #define LL long long inline int read(){ int x=0,f=1;char c=getchar(); for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1; for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘; return x*f; } int N,M; priority_queue<int ,vector<int> , greater<int> > Que; int a[100001]; int ans[100001]; int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); while(scanf("%d%d",&N,&M)!=EOF){ for(int i=1;i<=N;i++) a[i]=read(); for(int i=1;i<=N;i++) for(int j=i+1;j<=N;j++){ Que.push(a[i]+a[j]); if(Que.size()>M) Que.pop(); } for(int i=1;i<=M;i++) ans[i]=Que.top(),Que.pop(); for(int i=M;i>1;i--) printf("%d ",ans[i]); printf("%d\n",ans[1]); } return 0; }
标签:long des break -- com 优先队列 优先 other mod
原文地址:http://www.cnblogs.com/wxjor/p/7223054.html