codeforces A. Jeff and Rounding (数学公式+贪心)
题目链接:http://codeforces.com/contest/351/problem/A
算法思路:2n个整数,一半向上取整,一半向下。我们设2n个整数的小数部分和为sum.
ans = |A - B|;
sum = A +(n-b)-B;
所以ans = |sum - (n-b) |; 只有b未知,只需要枚举一下b就得到答案。
#include<cstdio> #include<iostream> #include<algorithm> #include<iostream> #include<cmath> using namespace std; const int maxn = 2005; const double eps = 1e-12; int dcmp(double x){ if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } int main() { //freopen("E:\\acm\\input.txt","r",stdin); double sum = 0; int n; int num = 0; cin>>n; for(int i=1;i<=2*n;i++) { double a; scanf("%lf",&a); if(dcmp(floor(a)-a) == 0) num++; sum += a - floor(a); } double ans = 1e10; for(int i=max(num-n,0);i<=min(n,num);i++) { ans = min(ans,fabs(sum-(n-i))); } printf("%.3lf\n",ans); }