题意:
输入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,反之亦然,证毕。
输入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;
}