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]);
}
}