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

Re:怎么会WA呢?思路很简单啊

Posted by bobpkuhj at 2004-08-09 21:25:10 on Problem 1738
In Reply To:怎么会WA呢?思路很简单啊 Posted by:PC0400322032 at 2004-08-09 21:09:15
单纯的贪心肯定是有问题的,例如N=4,个数分别为:5,3,4,5。
                                             ——LouTianCheng

> #include <stdio.h>
> void main(void)
> {
> 	int n,m;
> 	unsigned int a[50005],b[50005],*p,*q,*r;
> 	int i,k;
> 	unsigned c,min,tm;
> 	while(1)
> 	{
> 		scanf("%d",&n);
> 		if(n==0)
> 			break;
> 		for(i=1;i<=n;i++)
> 			scanf("%d",&a[i]);
> 		m = 0;a[0]=b[0]=1000000005;
> 		p = a+1; q = b+1; c = 2;
> 		while(n>1)
> 		{
> 			min = p[n] = 1000000005;
> 			k = 0;
> 			for(i=0;i<n;i++)
> 			{
> 				tm = p[i]+p[i+1];
> 				if(tm<=c)
> 				{
> 					m += tm;
> 					q[k] = tm;
> 					i++;
> 				}
> 				else
> 					q[k] = p[i];
> 				if(q[k]+q[k-1]<min)
> 					min = q[k-1]+q[k];
> 				k++;
> 			}
> 			c = min;
> 			r = p;			p = q;			q = r;
> 			n = k;
> 		}
> 		printf("%u\n",m);
> 	}
> }

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