Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

发现STL比较耗内存 STL340多 数组230多

Posted by gongshaoqing at 2009-11-08 16:24:23 on Problem 3253
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator