Codeforces Round #204 (Div. 二) C. Jeff and Rounding
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 than ai (assign to ai: ⌊ ai ⌋);
- round element aj to the nearest integer that isn't less than aj (assign to aj: ⌈ 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 numbers a1, 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.
3 0.000 0.500 0.750 1.000 2.000 3.000
0.250
3 4469.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.
思路:
2n个数(设小数部分都看做x),n个向下取整,相对原来的数-x,n个向下取整,相对原来的数+(1-x),从两个式子中可以看出x的前面都是负号,所以可以统一处理,那么剩下来的就是统计1的个数cnt了。如果不出现a.000的情况的话,那么cnt=n,因为a.000如果向上取整的话是不需要+1的,所以统计出现a.000的次数num,那么+1的个数就在(max(0,n-num),n)之间了,取最佳ans就够了。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> //#pragma comment (linker,"/STACK:102400000,102400000") #define maxn 100005 #define MAXN 100005 #define mod 1000000007 #define INF 0x3f3f3f3f using namespace std; typedef long long ll; int n,m,cnt; double ans,sum; int main() { int i,j,mi; ll t; double s,tmp; while(~scanf("%d",&n)) { m=2*n; cnt=0; sum=0; for(i=1;i<=m;i++) { scanf("%lf",&s); t=ll(s); if(t==s) cnt++; else { tmp=s-t; sum+=tmp; } } mi=min(n,cnt); ans=INF; for(i=0;i<=mi;i++) { ans=min(ans,fabs(n-i-sum)); } printf("%.3f\n",ans); } return 0; }
相关推荐
- Codeforces Round #332 (Div. 二) C. Day at the Beach 线段树
- C. Watto and Mechanism 字典树 Codeforces Round #291 (Div. 二)
- Codeforces Round #291 (Div. 二) - C. Watto and Mechanism
- Codeforces Round #370 (Div. 二) C. Memory and De-Evolution 水题
- Codeforces Round #315 (Div. 二) C. Primes or Palindromes? 素数表 回文数
- Codeforces Round #315 (Div. 二)——C. Primes or Palindromes
- C. Amr and Chemistry(Codeforces Round #312 (Div. 二) 二进制+暴力)
- Codeforces Round #316 (Div. 二) C. Replacement 找规律 或 线段树
- Codeforces Round #316 (Div. 二) C. Replacement
- C. Arthur and Table(Codeforces Round #311 (Div. 二) 贪心)
- Codeforces Round #204 (Div. 一) C Jeff and Brackets dp+矩阵加速
- hdu4453 舒张树基本题