Codeforces Round #571 (Div. 2)-D. Vus the Cossack and Numbers

D. Vus the Cossack and Numbers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vus the Cossack has ? real numbers ??. It is known that the sum of all numbers is equal to 0. He wants to choose a sequence ? the size of which is ? such that the sum of all numbers is 0 and each ?? is either ⌊??⌋ or ⌈??⌉. In other words, ?? equals ?? rounded up or down. It is not necessary to round to the nearest integer.

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

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

Help Vus the Cossack find such sequence!

Input
The first line contains one integer ? (1≤?≤105) — the number of numbers.

Each of the next ? lines contains one real number ?? (|??|<105). It is guaranteed that each ?? has exactly 5 digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to 0.

Output
In each of the next ? lines, print one integer ??. For each ?, |??−??|<1 must be met.

If there are multiple answers, print any.

Examples
inputCopy
4
4.58413
1.22491
-2.10517
-3.70387
outputCopy
4
2
-2
-4
inputCopy
5
-6.32509
3.30066
-0.93878
2.00000
1.96321
outputCopy
-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.

题目:https://codeforces.com/contest/1186/problem/D
题意:选择一个n个长度的序列,序列的元素为double类型,每个double数可往上或者往下近似,但必须保证序列之和为0.
先算出小数部分之和,根据小数部分的和来确定调整序列元素往上还是往下近似,从而达到相互平衡。
附上代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn =1e5;
int n;
double ans[maxn];
int ans1[maxn];
int main(){
    while(~scanf("%d",&n)){
        double s=0;
        for(int i=0;i<n;i++){
            scanf("%lf",&ans[i]);
            ans1[i]=ans[i];
            s+=(ans[i]-ans1[i]);
        }
        int key=round(s);
        if(key>0){
            for(int i=0;i<n;i++){
                if(key== 0)
                {
                    break;
                }
                if(ans[i]>ans1[i]){
                    ans1[i]++;
                    key --;
                }
            }
        }
        if(key<0){
            for(int i=0;i<n;i++){
                if(key==0)
                {
                    break;
                }
                if(ans[i]<ans1[i]){
                    ans1[i]--;
                    key++;
                }
            }
        }
        for(int i=0;i<n;i++)
            printf("%d\n",ans1[i]);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值