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

看 我的第一次 greedy haha AC

Posted by qqgyp at 2006-05-27 22:56:49 on Problem 1700
In Reply To:我是这样过的,供参考 Posted by:t9thkid at 2006-05-26 21:16:00
#include<stdio.h>
#include<stdlib.h>

int cmp ( const void *a , const void *b ) 
{ 
     return *(int *)a - *(int *)b; 
} 

void main()
{
	int n, num, a[1002], i;
	
	scanf("%d",&n);
	
	while(n--)
	{
		scanf("%d",&num);
		
		__int64 total = 0;

		for(i = 0;i < num;i++)
			scanf("%d",&a[i]);
		
		qsort(a, num, sizeof(a[0]), cmp);
		
		for(i = num - 1;i >= 3;i -= 2)
		{
		
           if(2 * a[1] >= a[0] + a[i - 1]) 
                 total += 2 * a[0] + a[i - 1] + a[i];                
                           
           else   
                 total += 2 * a[1] + a[0] + a[i];  
		}

		if(i == 2)total += a[0] + a[1] + a[2];
		if(i == 1)total += a[1];
		if(i == 0)total += a[0];

		printf("%ld\n",total);
	} 
}



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