sgu165:Basketball

题意:
输入n个在[1.95,2.05]范围内的数。
      保证他们的平均数为2.00。
      现在要求把这些数调整出一个顺序,
      使得任意长度为K的子段和与2.00*K的差值的绝对值不超过0.1(K=1,2...,n)
分析:
把每一项减去2,那么我们的目的就是使前缀和|Sj-Si|<=0.1。
考虑使用构造法:若当前S>=0,选一个负数填第i项;若S<0,选一个非负数填第i项。(S为1到i-1项的前缀和)。
由于[1.95,2.05],所以减2后[-0.05,0.05],所以由以上方法得出的|Sj-Si|<=0.05,满足题意。
接下来证明一定有解:因为平均数为2,减2后为0,若当前S<0,我们总能不断加上非负数使S>=0,反之亦然,证毕。
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 6010;
const double eps = 1e-10;
int n;
double S = 0;
int ans[MAXN], top;
struct man 
{
	double h;
	int node;
}team[MAXN];

bool cmp(const man &A, const man &B)
{
	return A.h < B.h;
}

int dcmp(const double &A)
{
	if(fabs(A) <= eps) return 0;
	else return A>0?1:-1;	
}

int main()
{
	cin >> n;
	for(int i = 1; i <= n; ++i)
	{
		cin >> team[i].h;
		team[i].h -= 2;
		team[i].node = i;
	}
	sort(team+1, team+n+1, cmp);
	for(int i = 1, j = n; i <= j;)
	{
		if(dcmp(S) >= 0) 
		{
			S += team[i].h;
			ans[++top] = team[i++].node; 
		}
		else 
		{
			S += team[j].h;
			ans[++top] = team[j--].node;	
		}
	}
	cout << "yes" << endl;
	for(int i = 1; i <= top; ++i)
		cout << ans[i] << ' ';
	return 0;	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值