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

正确的代码,虽然有点烦

Posted by hahaxiao at 2005-08-31 19:13:39 on Problem 2593
#include <iostream>
using namespace std;
int main() {
	int n,ee;
	int i,j,p,big,little,t;
	int feifu;
	int a[100001];
	while(1) {
		cin>>n;
		if(n==0) break;
		big=-10000; little=-10000;
		for(i=1;i<=n;i++) {
			cin>>ee;			
			if(ee>big) {little=big; big=ee;}
			else if(ee>little) little=ee;
			if(ee>=0) break;
		}
		if(i>n) { cout<<big+little<<endl; continue;}
		if(i==n) {cout<<ee+little<<endl; continue;}
		p=1; a[p]=ee; feifu=1;t=0;
		for(;i<n;i++) {
			cin>>ee;
			if(ee>big) {little=big; big=ee;}
			else if(ee>little) little=ee;
			if(ee>=0) {
				++feifu;
				if(a[p]>=0) a[p]+=ee;
				else {a[++p]=ee;++t;}
			}
			else {
				if(a[p]<0) a[p]+=ee;
				else {a[++p]=ee; ++t;}
			}
		}
		if(feifu==1){cout<<a[1]+little<<endl; continue;}	
		if(t<=1 && feifu>1) {cout<<a[1]<<endl; continue;}
		t=t/2+1;
		while(t>2) {
			for(i=1;i+2<=p;i=i+2) 
				if(a[i]+a[i+1]+a[i+2]>=a[i] && a[i]+a[i+1]+a[i+2]>=a[i+2]){
				a[i]=a[i]+a[i+1]+a[i+2];
				for(j=i+3;j<=p;j++)
					a[j-2]=a[j];
				p=p-2; --t;
				break;
				}
			if(i+2>p) break;
		}
		big=a[1];little=a[3];
		if(big<little) {t=big;big=little;little=t;}
		for(i=5;i<=p;i=i+2) {
			if(a[i]>big) {little=big;big=a[i];}
			else if(a[i]>little)little=a[i];
		}
		cout<<big+little<<endl;
	}
	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