题目链接:https://codeforces.com/contest/1186/problem/D
思路:正的全向下取整,负的全向上取整,然后如果值不相等在根据情况把负数变大或者吧正数变小即可。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];
vector <int> b , c;
double d[maxn];
int main() {
int n;
ios::sync_with_stdio(0);
cin >> n;
long long sumz = 0 , sumf = 0;
for(int i = 0 ; i < n; i++) {
cin >> d[i];
if(d[i] >= 0) {
a[i] = floor(d[i]);
if(a[i] != d[i])b.push_back(i);
sumz += a[i];
}
else {
a[i] = ceil(d[i]);
if(a[i] != d[i])c.push_back(i);
sumf += a[i];
}
}
if(sumf + sumz < 0) {
int t = sumf + sumz;
for(int i = 0 ; i < b.size() && i < - t ; i++) {
a[b[i]] += 1;
}
}
else if(sumf + sumz > 0) {
int t = sumf + sumz;
for(int i = 0; i < c.size() && i < t ; i++) {
a[c[i]] -= 1;
}
}
for(int i = 0 ; i < n ; i++) {
cout << a[i] << "\n";
}
return 0;
}