Codeforces Round #204 (Div. 1) A. Jeff and Rounding
2013-10-05 08:54
483 查看
A. Jeff and Rounding
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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.
Output
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
Sample test(s)
input
output
input
output
Note
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-i),所以,由这个性质,就可以得出线性算法了!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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.
Output
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
Sample test(s)
input
3 0.000 0.500 0.750 1.000 2.000 3.000
output
0.250
input
3 4469.000 6526.000 4864.000 9356.383 7490.000 995.896
output
0.279
Note
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-i),所以,由这个性质,就可以得出线性算法了!
#include <stdio.h> #include <iostream> #include <math.h> #include <string.h> using namespace std; int main (){ int n,s1,s2,i; while(scanf("%d",&n)!=EOF){ double sum,x; for(i=0,s1=0,s2=0,sum=0;i<n+n;i++){ scanf("%lf",&x); int temp=(int)x; x=x-(double)temp; if(x!=0) sum+=x,s1++; else s2++; } int k=min(s1,n); double ans=100000000.0; for(i=0;i<=k;i++){ if(i+s2>=n){ ans=min(ans,fabs(sum-i*1.0)); } } printf("%.3f\n",ans); } return 0; }
相关文章推荐
- Codeforces Round #204 (Div. 2) C. Jeff and Rounding——数学规律
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding
- Codeforces Round #204 (Div. 2) C. Jeff and Rounding
- Codeforces Round #204 (Div. 2)->C. Jeff and Rounding
- CodeForce 204 Div2. C Jeff And Rounding
- CodeForces 352C-- Jeff and Rounding
- cf 204 div2 D. Jeff and Furik 逆序对
- codeforces 204 Jeff and Rounding
- Codeforces Round #204 (Div. 1) E. Jeff and Permutation
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation
- codeforces_351B_Jeff and Rounding(数学)
- codeforces A. Jeff and Rounding (数学公式+贪心)
- CF 351A - Jeff and Rounding DP
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) B. Jeff and Furik
- Codeforces Round #204 (Div. 2)->A. Jeff and Digits
- Codeforces Round #204 (Div. 2) B - Jeff and Periods
- 【CodeForces 351A】Jeff and Rounding(思维)
- [CF 351A]Jeff and Rounding[greedy]
- Codeforces 352C Jeff and Rounding【dp】
- codeforces 352C Jeff and Rounding 很有趣的一道题