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

誰來給個給力的測試資料,不明白哪裡WA...

Posted by yuusha at 2015-01-13 17:21:50 on Problem 3253
如題...以下是我的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:
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