| ||||||||||
| 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 | |||||||||
贴个ac代码,顺便指出容易导致WA的原因(我也WA了半天)//用long long 类型比较保险
//代码注释的地方要注意
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
struct priorqueue
{
LL arr[50000];
int n;
void init()
{
for(int i=0;i<50000;i++)
arr[i]=99999999999999999LL;
}
void shiftup()
{
int x=n;
while(x!=1)
{
if(arr[x]<arr[x>>1])
{
swap(arr[x],arr[x>>1]);
}
else break;
x>>=1;
}
}
void shiftdown()
{
int x=1;
while((x<<1)<=n)
{
if(arr[x<<1]<arr[x] && arr[x<<1]<=arr[(x<<1)+1])// 注意此处的比较,有一个是小于等于的,一直错在这
{
swap(arr[x],arr[x<<1]);
x<<=1;
}
else
if(arr[(x<<1)+1]<arr[x] && arr[(x<<1)+1]<=arr[(x<<1)])
{
swap(arr[x],arr[(x<<1)+1]);
x<<=1;
x++;
}
else break;
}
}
void push(LL x)
{
n++;
arr[n]=x;
shiftup();
}
LL pop()
{
LL tmp=arr[1];
arr[1]=arr[n];
arr[n--]=99999999999999999LL;
shiftdown();
return tmp;
}
}q;
void init()
{
int n;
scanf("%d",&n);
while(n--)
{
int x;
scanf("%d",&x);
q.push(x);
}
}
void work()
{
LL ans=0;
while(q.n>1)
{
LL a=q.pop();
a+=q.pop();
ans+=a;
q.push(a);
}
cout<<ans<<endl;
}
int main()
{
freopen("input.txt","r",stdin);
init();
work();
fclose(stdin);
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator