Description
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 Input
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
Hint
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 个数字,然后 每次选择 第 i 个 和 第 j 个数字,第 i 个向下取整 第 j 个向上取整 ,然后问变化后的数列和 变化前的数列最小的 差是多少
思路:
假设我们有 n 个数字向下取整 其中 小数 部分的和为 A
假设我们有 n 个数字向上取整 其中 小数部分的和为 B 并且 整数的数目 (也即是 0 的数量)为 b 个。
那么 我们有
ans = A+B
sum(所有小数部分的和)=A+( n-b - B )
那么 ans = sum - (n-b) 其中 b 未知 所以我们去枚举 b 的值。
假设 0 的数量数 num 个
b 下界是 max(0,num-n) 上界是 min(n,num)
嗯 然后就得到了答案。
AC代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
double a;
int t;
double sum=0;
int num=0;
for(int i=0;i<2*n;i++)
{
cin>>a;
t=a;
sum+=(a-t);
if(a-t<0.00000001)
num++;
}
double ans=10000000;
for(int i=max(0,num-n);i<=min(n,num);i++)
{
ans=min(ans,abs(sum-(n-i)));
}
printf("%.3f\n",ans);
return 0;
}