题目描述:

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 (1≤n≤1051≤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, |ai−bi|<1|ai−bi|<1 must be met.

If there are multiple answers, print any.

Examples

input

4
    4.58413
    1.22491
    -2.10517
    -3.70387

output

4
    2
    -2
    -4

input

5
    -6.32509
    3.30066
    -0.93878
    2.00000
    1.96321

output

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

思路:

要保证取整后和为零,就要看是怎么去上整和下整。

举个例子:1.99 2.01 -4取整应该是2 2 -4,那么我们怎么知道该取多少个上整或下整呢?就先把正数小数部分加起来记为posit,负数的小数部分的绝对值加起来记为negt,。用posit-negt得到的数记为gap,即表示正数的小数部分和负数的小数部分相差是什么。如果是零,那么好了,直接把数组的数取整就可以输出了。如果gap>0,说明这个时候正数的整数部分和是小于负数的正数部分和的绝对值的。因为正数还需要小数部分的补充才能和负数打个平手,同理,gap<0,说明负数整数和小于正数整数和。其实按理说小数部分加起来后应该是个整数的,要不然和不可能是零,但是,狗血的来了,这个时候gap的类型是double,后面再计数时要转换成int,如果gap=2,说明他在计算机表示上接近2,但不能看成2,直接转换成整数竟然可以等于一!佛了。这时候就要用round函数了(见参考文章1)。

然后根据gap的正负,把相应的数进行加减操作,比如gap>0,那就要把正的而且原来不是整数的数加1,<0就把负的数而且原来不是整数的数减一,最后输出。

怎么判断原来a数组的数是不是整数呢?我一开始是按要求来fabs(a[i]-b[i])<1(b[i]为取整后的数,整数下取整,负数上取整)来判断的,事实证明,这不行,我怎么这么天真无邪善良可爱(傻),精度还是有问题。就在刚刚写到这里,我突然意识到了什么,不四fabs(a[i]-b[i])<1,而四fabs(a[i]-b[i]-1)<1和fabs(a[i]-b[i]+1)<1.怎么会这样子滋滋滋滋滋~。好,这样是可以的,然后就可以修改了。最后输出✿✿ヽ(°▽°)ノ✿(真好)。

再提一点,在过程中我竟然发现了-0这样的输出,还是负的,用了ceil返回值是double,应该直接截取了整数部分。联想到浮点数的表示,嗯,这是个负的零,只能接近零,但不是零。这就要看浮点数在计算机中的表示了。如果指数是 0 并且尾数的小数部分是 0,这个数是 ±0(和符号位有关)(具体参见参考文章2)

代码:

 #include <iostream>
#include <cmath>
#define max_n 100005
using namespace std;
int n;
double a[max_n];
int b[max_n];
double posit = ;
double negt = ;
double eps = 10e-;
int main()
{
//printf("%d\n",-2);
cin >> n;
for(int i = ;i<n;i++)
{
cin >> a[i];
if(a[i]>)
{
posit += a[i]-floor(a[i]);
b[i] = floor(a[i]);
}
else if(a[i]<)
{
negt += ceil(a[i])-a[i];
b[i] = ceil(a[i]);
}
}
int gap = round(posit-negt);
//cout << gap << endl;
if(gap>)
{
for(int i = ; i<n&&gap; i++)
{
if(a[i]>&&fabs(a[i]-b[i]-)<)
{
b[i] = b[i]+;
gap--;
}
}
}
else
{
for(int i = ;i<n&&gap;i++)
{
if(a[i]<&&fabs(a[i]-b[i]+)<)
{
b[i] = b[i]-;
gap++;
}
}
}
for(int i = ;i<n;i++)
{
cout << b[i] << endl;
}
return ; }

参考文章:

dangzhangjing97,C语言(C++)中:详解floor函数、ceil函数和round函数,https://blog.csdn.net/dangzhangjing97/article/details/81279862

TwinkleStar0121,浮点数在计算机中的表示,https://blog.csdn.net/jvandc/article/details/81176294

需要清醒,清醒

Codeforces F. Vus the Cossack and Numbers(贪心)的更多相关文章

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

  2. codeforces#571Div2 D---Vus the Cossack and Numbers【贪心】

    题目:http://codeforces.com/contest/1186/problem/D 题意:给定一个大小为$n$的浮点序列,这$n$个数的和为0. 现在对这个序列中的每个数,进行向上取整或向 ...

  3. codeforces 1186C Vus the Cossack and Strings

    题目链接:https://codeforc.es/contest/1186/problem/C 题目大意:xxxxx(自认为讲不清.for instance) 例如:a="01100010& ...

  4. Codeforces F. Maxim and Array(构造贪心)

    题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. Codeforces 1186F - Vus the Cossack and a Graph 模拟乱搞/欧拉回路

    题意:给你一张无向图,要求对这张图进行删边操作,要求删边之后的图的总边数 >= ceil((n + m) / 2), 每个点的度数 >= ceil(deg[i] / 2).(deg[i]是 ...

  6. @codeforces - 1186F@ Vus the Cossack and a Graph

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 点 m 边的图(n, m<=10^6),记第 ...

  7. CodeForces - 1186 C. Vus the Cossack and Strings (异或)

    Vus the Cossack has two binary strings, that is, strings that consist only of "0" and &quo ...

  8. Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)

    C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...

  9. E. Vus the Cossack and a Field (求一有规律矩形区域值) (有一结论待证)

    E. Vus the Cossack and a Field (求一有规律矩形区域值) 题意:给出一个原01矩阵,它按照以下规则拓展:向右和下拓展一个相同大小的 0 1 分别和原矩阵对应位置相反的矩阵 ...

随机推荐

  1. Photoshop 7.0 安装及注册方法

    参照:https://jingyan.baidu.com/article/e75057f2e51ac9ebc91a8989.html

  2. CentOS 7.7安装Erlang和Elixir

    安装之前,先看一下它们的简要说明 Erlang Erlang是一种开源编程语言,用于构建对高可用性有要求的大规模可扩展的软实时系统.它通常用于电信,银行,电子商务,计算机电话和即时消息中.Erlang ...

  3. 翻书shader

    //把下面的shader挂载到plane上,调节_Angle Shader "Unlit/PageTurning"{ Properties { _Color ("Colo ...

  4. C++中STL中简单的Vector的实现

    该vector只能容纳标准库中string类, 直接上代码了,StrVec.h文件内容为: #ifndef STRVEC_H #define STRVEC_H #include<iostream ...

  5. Html设置问题(设置浏览器上面的图标,移动设备上面页面保存为图标)

    最近开发了一个新的项目,项目完成之后:要求把页面在移动设备上面保存为图标,通过图标直接进入系统入口(这样看着就想APP一样):刚开始通过百度直接设置了,发现有两个问题,第一.图标直接是页面的截图:第二 ...

  6. Mysql】Mysql中CURRENT_TIMESTAMP,CURRENT_DATE,CURRENT_TIME,now(),sysdate()各项值的区别

    CURRENT_TIMESTAMP,CURRENT_DATE,CURRENT_TIME,now(),sysdate()各项值的区别,我们可以通过在终端下,查看结果就能知道: SELECT CURREN ...

  7. centos 6.5安装zabbix 4.4

    一.安装环境 本环境,使用单机部署. 操作系统:centos 7.5 x64zabbix-server,Mysql,php,nginx都在同一台服务器.都是使用Yum安装的! 官方安装文档: http ...

  8. Golang ---基准测试

    什么是基准测试 基准测试,是一种测试代码性能的方法,比如你有多种不同的方案,都可以解决问题,那么到底是那种方案性能更好呢?这时候基准测试就派上用场了. 基准测试主要是通过测试CPU和内存的效率问题,来 ...

  9. 从损失函数优化角度:讨论“线性回归(linear regression)”与”线性分类(linear classification)“的联系与区别

    1. 主要观点 线性模型是线性回归和线性分类的基础 线性回归和线性分类模型的差异主要在于损失函数形式上,我们可以将其看做是线性模型在多维空间中“不同方向”和“不同位置”的两种表现形式 损失函数是一种优 ...

  10. 在Centos中安装.net core SDK

    在Linux中运行.net core 项目必须要有.net core SDK 环境.之前配置过几次,但由于没有做总结.过了几天又配置的时候 感觉特别陌生,今天就记录一次.net core SDK 的安 ...