您的位置:首页 > 其它

Jeff and Rounding

2017-03-20 22:44 218 查看
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.

Example

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-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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  filter