【CodeForces 351A】Jeff and Rounding(思维)

19 篇文章 0 订阅

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

题目大意:2n个数,n个上取整,n个下取整,求如何选择使变换后数的和与原数和的差最小

思路:开始想着将小数部分排序,前n个下取整,后n个上取整,显然不对啊。后来又想使相邻的两个一个上取,一个下取,显然更不对。其实写出数学表达式就能看出来了,设小数部分为x,则下取整 sum+x, 上取整 sum - (1-x),合起来就是 x-(1-x)=x+x-1,也就是先把小数部分都加起来,然后讨论1的个数。起始时1有n个,但如果有一个数的小数部分为0,那么就能少一个1,用小数部分为0 的数的个数来松弛,找到最小值

#include <bits/stdc++.h>
#define manx 4002
#define INF 0x3f3f3f3f
using namespace std;
int n;
struct node
{
    double v,k;
}a[manx];
int main()
{
    double sum=0;
    int cot=0;
    scanf("%d",&n);
    for (int i=0; i<2*n; i++){
        scanf("%lf",&a[i].v);
        a[i].k = a[i].v - (int)a[i].v;
        if(!a[i].k) cot++;
        sum+=a[i].k;
    }
    double ans=INF;
    if(sum >= n) ans=sum-n;
    else{
        for(int i=0; i<=cot && i<=n; i++){ //0 num
            ans=min(fabs(ans),fabs(sum-(n-i)));
        }
    }
    printf("%.3f\n",fabs(ans));
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值