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 |
发现STL比较耗内存 STL340多 数组230多#include <iostream> #include <queue> #include <algorithm> #include <stdio.h> using namespace std; void HeapUp(int heap[],int p) { int q=p>>1,a=heap[p]; while(q) { if(a<heap[q])heap[p]=heap[q]; else break; p=q; q=p>>1; } heap[p]=a; } void AddToHeap(int heap[],int a,int &heapSize) { heap[++heapSize]=a; HeapUp(heap,heapSize); } void HeapDown(int heap[],int p,int heapSize) { int q=p<<1,a=heap[p]; while(q<=heapSize) { if(q<heapSize&&heap[q+1]<heap[q])q++; if(a>heap[q])heap[p]=heap[q]; else break; p=q; q=p<<1; } heap[p]=a; } int GetTopFromHeap(int heap[],int &heapSize) { int TopElement=heap[1]; heap[1]=heap[heapSize--]; HeapDown(heap,1,heapSize); return TopElement; } int minheap[20005]; int minsize; int main() { int m; int i,x; while(scanf("%d",&m)!=EOF) { minsize=0; for(i=0;i<m;i++) { scanf("%d",&x); AddToHeap(minheap,x,minsize); } int t1,t2; __int64 ans=0; while(minsize!=1) { t1=GetTopFromHeap(minheap,minsize); t2=GetTopFromHeap(minheap,minsize); ans+=(t1+t2); AddToHeap(minheap,t1+t2,minsize); } printf("%I64d\n",ans); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator