zoukankan      html  css  js  c++  java
  • Codeforces Round #204 (Div. 2) C. Jeff and Rounding

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

    /*
    题意:给你2×n个浮点型数据,现在可以选取两个数,然后一个向下取整,一个向上取整,直到没法取,得到一个新数列,问你怎么样取,才能使得
        新数列和与旧数列和差的绝对值最小
    
    初步思路:取到最后肯定是n个向上取整,n个向下取整,假设n*2个数的小数部分小数和为sum,最后的要求的结果为res
        那么n个向下取整的数中:
        有小数部分和A,有a个没有小数部分的数
    
        那么n个向上取整的数中:
        有小数部分向上取整加的和为B,有b个没有小数部分的数
    
          得    res=|A-B|;
               sum=|A+(n-b-B)|
        于是推出
            res=|sum-(n-b)|
        里面只有b是未知数,这样只需要枚举一下b就行了
    */
    #include <bits/stdc++.h>
    #define INF 1e10
    #define exp 1e-12
    using namespace std;
    int main(){
        // freopen("in.txt","r",stdin);
        double sum=0;
        int b=0;
        int n;
        double a;
        scanf("%d",&n);
        for(int i=1;i<=n*2;i++){
            scanf("%lf",&a);
            if(a-floor(a)<exp) b++;
            sum+=a-(int)a;
        }
        double res=INF;
        for(int i=max(b-n,0);i<=min(n,b);i++){
            
            res=min(res,fabs(sum-(n-i)));
        }
        // cout<<n<<endl;
        printf("%.3lf
    ",res);
        return 0;
    }
  • 相关阅读:
    webpack4笔录
    在Salesforce中进行Report和Dashboard的配置
    在Salesforce中以PDF的格式显示对应的页面
    在Salesforce中处理Email的发送
    在Visualforce page中用自带的控件实现Ajax回调后台方法(并且可以用js去动态给parameters赋值)
    javascript settimeout and setinterval
    Convert XML to Object using LINQ
    Convert Object to XML using LINQ
    JS对Array进行自定制排序
    在Salesforce中通过 Debug Log 方式 跟踪逻辑流程
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6624403.html
Copyright © 2011-2022 走看看