| ||||||||||
| 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 | |||||||||
誰來給個給力的測試資料,不明白哪裡WA...如題...以下是我的code
麻煩各位路過的大神們了!
#include<stdio.h>
int heap[20000], r;
void swap(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int min(int a, int b)
{
return a>b?b:a;
}
void push(int n)
{
r++;
heap[r] = n;
int s = r, f = (s-1)/2;
while(heap[s] < heap[f]) {
swap(&heap[s], &heap[f]);
if(!f) break;
s = f;
f = (s-1)/2;
}
}
int pop()
{
int t = 0, n = heap[0], s = 2*t, w;
heap[0] = heap[r];
while(r-t>1) {
if(heap[t] > min(heap[s+1], heap[s+2])) {
if(heap[s+1] < heap[s+2]) {
swap(&heap[t], &heap[s+1]);
w = 1;
}else {
swap(&heap[t], &heap[s+2]);
w = 2;
}
t = s+w;
s = 2*t;
} else break;
}
r--;
return n;
}
int main()
{
int n, l;
scanf("%d", &n);
r = -1;
while(n--) {
scanf("%d", &l);
push(l);
}
int sum = 0;
for(;;) {
int x = pop() + pop();
sum += x;
push(x);
if(!r) break;
}
printf("%d\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