zoukankan      html  css  js  c++  java
  • Codeforces Round #571 (Div. 2)-D. Vus the Cossack and Numbers

    Vus the Cossack has nn real numbers aiai. It is known that the sum of all numbers is equal to 00. He wants to choose a sequence bb the size of which is nn such that the sum of all numbers is 00 and each bibi is either ai⌊ai⌋ or ai⌈ai⌉. In other words, bibi equals aiai rounded up or down. It is not necessary to round to the nearest integer.

    For example, if a=[4.58413,1.22491,2.10517,3.70387]a=[4.58413,1.22491,−2.10517,−3.70387], then bb can be equal, for example, to [4,2,2,4][4,2,−2,−4].

    Note that if aiai is an integer, then there is no difference between ai⌊ai⌋ and ai⌈ai⌉, bibi will always be equal to aiai.

    Help Vus the Cossack find such sequence!

    Input

    The first line contains one integer nn (1n1051≤n≤105) — the number of numbers.

    Each of the next nn lines contains one real number aiai (|ai|<105|ai|<105). It is guaranteed that each aiai has exactly 55 digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to 00.

    Output

    In each of the next nn lines, print one integer bibi. For each ii, |aibi|<1|ai−bi|<1 must be met.

    If there are multiple answers, print any.

    Examples
    input
    Copy
    4
    4.58413
    1.22491
    -2.10517
    -3.70387
    
    output
    Copy
    4
    2
    -2
    -4
    
    input
    Copy
    5
    -6.32509
    3.30066
    -0.93878
    2.00000
    1.96321
    
    output
    Copy
    -6
    3
    -1
    2
    2
    
    Note

    The first example is explained in the legend.

    In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.

    题意:原来的浮点型数组之和为0,现在让你进行向上或者向下取整,使得变化后的数组之和也是0

    思路:把浮点型数组进行一种取整方式,然后把小数部分保留一下,看最后的小数部分的进行四舍五入后的结果,然后根据最后小数部分的和进行不断地进行改变取整方式就可以了

    代码:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<vector>
    #include<cmath>
     
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    double a[maxn];
    int b[maxn];
     
    int  main()
    {
        int n;
        cin>>n;
        double s=0;
        for(int t=0;t<n;t++)
        {
            scanf("%lf",&a[t]);
            b[t]=a[t];
            s+=(a[t]-b[t]);
        }
        int ss=round(s);
        if(ss>0)
        {
     
        for(int t=0;t<n;t++)
        {
            if(ss==0)
            {
                break;
            }
            if(a[t]>b[t])
            {
                b[t]++;
                ss--;
            }
        }
        }
        if(ss<0)
        {
        for(int t=0;t<n;t++)
        {
            if(ss==0)
            {
                break;
            }
            if(a[t]<b[t])
            {
                b[t]--;
                ss++;
            }
        }    
        }
        for(int t=0;t<n;t++)
        {
            cout<<b[t]<<endl;
        }
        
        
        return 0;    
    } 
  • 相关阅读:
    四层架构设计实践
    看看node.js chat程序如何实现Ajax longpolling长链接刷新模式
    模仿igoogle【定制化、拖动排序,最大化、分屏】
    安装和配置Apache
    好书推荐《Pro ASP.NET MVC 3 Framework 3rd Edition》
    GAC和VS引用的程序集不一致?
    不要在 ASP.NET 4.5 Beta 的 Page 类事件上直接使用 async 与 await
    使用事务自动回滚来实现单元测试
    C# 如何异步查询数据库
    Linq + Jquery + Ajax 实现异步分页,批量删除,单个删除,全选,反选 ……
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11109248.html
Copyright © 2011-2022 走看看