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.

    Sample test(s)
    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.

    算法比较简单~~

    因为一定会有n个数向上取整,n个数向下取整,所以最后差值d:

     d = 求和 (1-di) - 求和(di)  di表示第i个数向下取整丢掉的小数  。。。。。(i从1到n)

    当ai含非零小数位时,

    d = n - 求和(di)   (i从1到2n).

     那么可以求出小数部分的所有和,即上式右边的第二项,数出有多少个含非零小数的数;

     然后为了使|d|最小,即等式右边第一项最接近第二项,从n中选数向上取整即可(直接贪心)。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    
    using namespace std;
    
    #define LL long long
    #define ULL unsigned long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    
    #define MAXN 4444
    #define EPS 1e-9
    #define INF 1e9
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int i, n;
        while(scanf(" %d", &n)==1){
            double ans=INF, a, t=0;
            int cnt=0;
            for(i=0; i<n<<1; i++){
                scanf(" %lf", &a);
                double del = a - (int)a;
                t = t + del;
                if(del>EPS) cnt++;
            }
            int minn = (cnt>=n ? cnt-n : 0),
                maxx = (cnt>=n ? n : cnt);
            for(i=minn; i<=maxx; i++)
                ans=MIN(fabs(i-t), ans);
            printf("%.3f
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    回调函数: 一定要在函数名前加上 CALLBACK,否则有可能引起内存崩溃!
    win32-api: 让 static 控件中的水平横行,垂直居中。
    Win32-API: 终于能正常的捕获焦点事件: WM_COMMAND、BN_SETFOCUS、EN_SETFOCUS
    FindExecutable:查找与一个指定文件关联在一起的程序的文件名
    ImageMagick: win7 | win8 & uac (用户帐户控制) 注册表的一些事
    ImageMagick: 6.8.3 升级到 6.8.9 遇到的问题
    ImageMagick: DrawImage(Image*,DrawInfo*) 绘制填充图片时卡住的原因分析
    真的无语, 今天遇到一个奇葩的事情: http 会话劫持
    高DPI下界面错乱的解决方法和原理
    关于 HDC 的释放
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3352866.html
Copyright © 2011-2022 走看看