Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
烦躁,发纯C代码。优先队列16MS,刷不进0MS#include<stdio.h> __int64 sum; int b[20005]; int h,len; pt(int i) { return i/2; } lt(int i) { return 2*i; } rt(int i) { return 2*i+1; } mh(int i) { int l,r,lg,t; l=lt(i); r=rt(i); if(l<=h&&b[l]<b[i]) lg=l; else lg=i; if(r<=h&&b[r]<b[lg]) lg=r; if(lg!=i) { t=b[i]; b[i]=b[lg]; b[lg]=t; mh(lg); } } bmh() { int i; h=len; for(i=len/2;i>0;i--) mh(i); } int hem() { int min=b[1]; b[1]=b[h]; h--; mh(1); return min; } hink(int i,int key) { int t; b[i]=key; mh(1); } int main() { int n,i,j,t; while(scanf("%d",&n)!=EOF) { sum=0; for(i=1;i<=n;i++) scanf("%d",&b[i]); len=n; bmh(); while(h>1) { t=hem(); t+=b[1]; sum+=t; hink(1,t); } printf("%I64d\n",sum); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator