CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding

Posted 日拱一卒 功不唐捐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding相关的知识,希望对你有一定的参考价值。

http://codeforces.com/problemset/problem/351/A

 

题意:

2*n个数,选n个数上取整,n个数下取整

最小化 abs(取整之后数的和-原来数的和)

 

先使所有的数都下取整,累积更改的sum

那么选1个小数上取整,就会使sum-1

整数上下取整不会产生影响

所以有1个整数就可以使上取整的小数少1个

所以ans=min(abs(i-sum)) i∈[n-整数个数,n]

 

#include<cmath>
#include<cstdio>
#include<algorithm>

using namespace std;

const double eps=1e-7;

int main()
{
    int n;
    scanf("%d",&n);
    int m=n<<1; 
    int cnt=0; 
    double x;
    double sum=0; 
    for(int i=1;i<=m;++i) 
    {
        scanf("%lf",&x);
        sum+=x-floor(x);
        if(x-floor(x)<eps) cnt++;
    }
    double ans=1e9;
    for(int i=n;i>=n-cnt;--i) ans=min(ans,abs(i-sum));
    printf("%.3lf",ans); 
}

 

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.

Examples
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.

以上是关于CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding的主要内容,如果未能解决你的问题,请参考以下文章

CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds

CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake

CF&&CC百套计划2 CodeChef December Challenge 2017 Penalty Shoot-out

CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

(每日一题)CF1139D Steps to One && 2021年天梯赛 L3-3 可怜的简单题(期望,莫比乌斯反演,杜教筛)

完美商业计划书全攻略