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 qiqilrq at 2007-07-12 16:23:25 on Problem 2576
In Reply To:先排序,然后把每次最小的和最大的分到一组,然后随机交换 Posted by:JiangLY at 2005-08-16 09:43:04
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
using namespace std;

int A[100], B[50], C[50];
int bn, cn, bs, cs, n;
int i, j;

void pt(){
	puts("");
	for(i=0; i<n; i++)
		cout<<A[i]<<' ';
	puts("");
	for(i=0; i<bn; i++)
		cout<<B[i]<<' ';
	puts("");
	for(i=0; i<cn; i++)
		cout<<C[i]<<' ';
	
}

void main()
{
	cin>>n;
	for(i=n-1; i>=0; i--)
		scanf("%d", A+i);
	sort(&A[0], &A[n]);
	bn = n /2;
	cn = n - bn;
	int ft=0, bk=n-1;
	for(bs=0, i=bn-1; i>=0; i--)
	{
		if(i&1) 
			B[i]=A[bk--];
		else 
			B[i]=A[ft++];
		bs += B[i];
	}

//	printf("%d %d\n", cn, bk-ft+1);

	for(i=cn-1; i>=0; i--){
		C[i]=A[ft+i];
		cs += C[i];
	}

	for(int t=0;t<1000000;++t)
	{
		i = rand()%bn;
		j = rand()%cn;
		int d1 = abs(bs-cs);
		int d2 = abs(bs-cs-((B[i]-C[j])<<1));
		if(d1 >= d2)
		{
			bs -= B[i] - C[j];
			cs -= C[j] - B[i];
			swap(B[i], C[j]);
		}
	}
	if(bs>cs) swap(bs, cs);
	cout<<bs<<" "<<cs;
	
	pt();
}

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