sgu165:Basketball
来源:互联网 发布:测试淘宝标题 编辑:程序博客网 时间:2024/06/05 17:04
题意:
输入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;}
0 0
- sgu165:Basketball
- basketball vocabulary
- Basketball English
- Play Basketball
- Basketball Gym
- Basketball Gym
- Backyard Basketball 2007
- I love basketball~!!!
- [模拟][YandexAlgorithm2013T1]Ancient Basketball
- C. Vasya and Basketball
- codeforces Vasya and Basketball
- Vasya and Basketball
- Weekly Roundup: Basketball
- NBA Palace for Basketball Players
- NBA Palace for Basketball Players
- codeforces 107B Basketball Team
- sgu 165 Basketball 贪心构造
- Vasya and Basketball 题解报告
- nginx+apache+mysql+php+memcache+squid搭建集群web环境
- Android里面的TextView跑马灯效果,重写了TextView控件
- Redis基础和集群
- Unity3D—增,查,删,改方法
- mac示例代码-filecards by大家学分享站
- sgu165:Basketball
- 两字符串的编辑距离 (Eidt Distance)
- Rational rose 7.0 安装说明
- Android横向ListView可以滚动
- sql调优
- CListBox 总是显示最新添加的一行 自动滚动 MFC
- android studio for Mac 安装提示 Java not found 解决办法
- Sum Root to Leaf Numbers
- 查看电脑型号