Jeff and Rounding
来源:互联网 发布:1064 mysql 编辑:程序博客网 时间:2024/06/05 23:45
Jeff got 2n real numbers a1, a2, ..., a2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows:
- choose indexes i and j (i ≠ j) that haven't been chosen yet;
- round element ai to the nearest integer that isn't more thanai (assign toai:⌊ ai ⌋);
- round element aj to the nearest integer that isn't less thanaj (assign toaj:⌈ aj ⌉).
Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbersa1, a2, ...,a2n(0 ≤ ai ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
30.000 0.500 0.750 1.000 2.000 3.000
0.250
34469.000 6526.000 4864.000 9356.383 7490.000 995.896
0.279
In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3),(i = 5, j = 6). In this case, the difference will equal|(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25.
对于任何a1 ,a2,a3,a4...an,如果,把所有的数都向下取整,也就是小数部分加得到的和为sum,那么,如果,我们取a1,a2,..an,中的任何k个数向上取整,结果一定是fabs(sum-k),所以,由这个性质,就可以得出线性算法了!
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#define maxn 5000
using namespace std;
double a[maxn];
int main()
{
int n;
scanf("%d",&n);
int cnt1=0,cnt2=0;
double sum=0;
for(int i=0;i<2*n;i++){
cin>>a[i];
double t=a[i]-floor(a[i]);
sum+=t;
if(t!=0) cnt1++; /统计小数部分不为零
else cnt2++; /统计小数部分为零
}
cnt1=min(cnt1,n);
double ans=100000000.000;
for(int i=0;i<=cnt1;i++){
if(i+cnt2>=n){ /保证存在n个数向上取整(i个向上取整,再从cnt2中取n-i个向上取整)
ans=min(ans,abs(sum-(double)i)); /找出最大值
}
}
printf("%.3lf\n",ans);
return 0;
}
- Jeff and Rounding
- codeforces 204 Jeff and Rounding
- 351A Jeff and Rounding
- [CF 351A]Jeff and Rounding[greedy]
- CodeForce 204 Div2. C Jeff And Rounding
- CodeForces 351A Jeff and Rounding
- Codeforces 352C Jeff and Rounding【dp】
- A. Jeff and Rounding----思维题
- Codeforces Round #204 (Div. 1) A. Jeff and Rounding
- Codeforces Round #204 (Div. 2) C. Jeff and Rounding
- cf 204 div2 C Jeff and Rounding 模拟
- Codeforces Round #204 (Div. 2) C Jeff and Rounding
- codeforces 351A Jeff and Rounding(区间枚举)
- codeforces 351A A. Jeff and Rounding(智商题+枚举)
- codeforces 351A A. Jeff and Rounding(★)
- 【CodeForces 351A】Jeff and Rounding(思维)
- codeforces 352C Jeff and Rounding 很有趣的一道题
- codeforces_351B_Jeff and Rounding(数学)
- [codechef MARCH17]SUMDIS
- ESP8266--Linux-TCPServer-MySQL
- leetcode [Binary Tree Level Order Traversal II]
- 各大排序算法自己总结
- window10安装docker时报错 hyper-v feature is not enabled
- Jeff and Rounding
- struts2的数据校验&常用OGNL表达式&ActionSupport
- nginx配置文件nginx.conf实例
- 设计模式(16)-状态模式
- 2017浙江省选一试第一天游记
- 类模板模拟实现STL中Vector
- nginx配置文件nginx.conf中文注释
- 设计模式(六)适配器模式
- hdu 2102