题目链接:
题目大意:
给出2*n个数,n个向上取整,n个向下取整,求新的数的和与原始的数的和的最小差距。
题目分析:
- 机智的人会发现,先对所有的数的小数部分取和,然后如果出现一个向上取整的,那么sum的变化一定是1,所以只和向上取整的数的个数有关系,而向上取整和向下取整的个数已经确定,只有存在小数部分是0的情况的时候,值会不同,因为它转换为向上取整和向下取整的值是不变的。
- 所以做法是,求得所有数小数部分的和,然后我们枚举在向上取整的n个数中,0占i个,然后当前这种情况的答案就是sum-(n-i)。
- 我们枚举所有情况取最小即可。
AC代码:
#include <iostream>
#include <cstdio>
#include <algoritth>
#include <cstring>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#define MAX 5007
#define eps 1e-6
using namespace std;
typedef long long LL;
int n;
double a[MAX];
bool check ( double x )
{
if ( fabs(x) < eps ) return true;
else return false;
}
int main ( )
{
while ( ~scanf ( "%d" , &n ) )
{
for ( int i = 1; i <= n ; i++ )
{
scanf ( "%lf" , &a[2*i-1] );
scanf ( "%lf" , &a[2*i] );
}
int num = 0;
double sum = 0;
for ( int i = 1 ; i <= 2*n ; i++ )
{
a[i] -= floor ( a[i] );
if ( check ( a[i] ) ) num++;
sum += a[i];
}
double ans = 1e11;
for ( int i = max(0,num-n) ; i <= min ( num , n ) ; i++ )
ans = min ( ans , fabs ( sum-(n-i) ) );
cout <<setprecision(3) <<fixed<< ans << endl;
}
}
本文详细介绍了如何通过数学思维和枚举技巧解决Codeforces平台上的351A题目,主要关注于计算在给定一组向上取整和向下取整的数之后,新数集合与原始数集合之间的最小差距。文章首先阐述了题目背景和关键概念,随后深入分析了解题思路,最后提供了一段AC代码以展示具体实现过程。
1442

被折叠的 条评论
为什么被折叠?



