题目链接: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 = ;
const double eps = 1e-;
int dcmp(double x){
if(fabs(x) < eps) return ;
else return x < ? - : ;
}
int main()
{
//freopen("E:\\acm\\input.txt","r",stdin); double sum = ;
int n;
int num = ;
cin>>n;
for(int i=;i<=*n;i++)
{
double a;
scanf("%lf",&a);
if(dcmp(floor(a)-a) == ) num++;
sum += a - floor(a);
}
double ans = 1e10;
for(int i=max(num-n,);i<=min(n,num);i++)
{
ans = min(ans,fabs(sum-(n-i)));
}
printf("%.3lf\n",ans);
}