C. Jeff and Rounding

time limit per test:  1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Jeff got 2n real numbers a1, a2, ..., a2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows:

  • choose indexes i and j (i ≠ j) that haven't been chosen yet;
  • round element ai to the nearest integer that isn't more than ai (assign to ai: ⌊ ai ⌋);
  • round element aj to the nearest integer that isn't less than aj (assign to aj: ⌈ aj ⌉).

Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.

Input

The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a1, a2, ..., a2n (0 ≤ ai ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.

Output

In a single line print a single real number — the required difference with exactly three digits after the decimal point.

Sample test(s)
input
3
0.000 0.500 0.750 1.000 2.000 3.000
output
0.250
input
3
4469.000 6526.000 4864.000 9356.383 7490.000 995.896
output
0.279
Note
In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25.

题意:给2n个实数,对其中n个数做向上取整操作,另外n个数向下取整操作。求操作后的2n个数的和与原来2n个数的和差的绝对值的最小值。

做法:全部向上取整操作和记作res,再选取n个数做向下取整,只要减去向上取整与向下取整的差。一个数向上取整与向下取整的差只能是0或者1,那么如果res大于0.5,就让res尽量减1,否则减0。

第一次感到自己想出解题方法的感觉真好。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; double a[4005];
double ceil_a[4005];
double floor_a[4005];
double cha[4005]; int main()
{
//freopen("in.txt", "r", stdin); int n;
scanf("%d", &n); double res = 0;
for (int i = 0; i < 2 * n; ++i) {
scanf("%lf", &a[i]);
ceil_a[i] = ceil(a[i]);
floor_a[i] = floor(a[i]);
cha[i] = ceil_a[i] - floor_a[i];
res += ceil_a[i] - a[i];
}
sort(cha, cha + 2 * n);
int l = 0, r = 2 * n - 1;
for (int i = 0; i < n; ++i) {
if (res > 0.5) res -= cha[r--];
else res -= cha[l++];
} printf("%.3f\n", abs(res));
return 0;
}

  

CodeForces 352C. Jeff and Rounding(贪心)的更多相关文章

  1. CodeForces 352C Jeff and Rounding

    题意 有一个含有\(2n(n \leqslant2000)\)个实数的数列,取出\(n\)个向上取整,另\(n\)个向下取整.问取整后数列的和与原数列的和的差的绝对值. 就是说,令\(a\)为原数列, ...

  2. codeforces A. Jeff and Rounding (数学公式+贪心)

    题目链接:http://codeforces.com/contest/351/problem/A 算法思路:2n个整数,一半向上取整,一半向下.我们设2n个整数的小数部分和为sum. ans = |A ...

  3. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding

    http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的 ...

  4. Codeforces Round #204 (Div. 2)->C. Jeff and Rounding

    C. Jeff and Rounding time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  6. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  7. Codeforces Round #204 (Div. 2) C. Jeff and Rounding——数学规律

    给予N*2个数字,改变其中的N个向上进位,N个向下进位,使最后得到得数与原来数的差的绝对值最小 考虑小数点后面的数字,如果这些数都非零,则就是  abs(原数小数部分相加-1*n), 多一个0 则 m ...

  8. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  9. Codeforces Gym 100269E Energy Tycoon 贪心

    题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...

随机推荐

  1. Bootstrap 分页功能

    function bootstrappage() { var options = { currentPage: currentPage, totalPages: totalPages, size: ' ...

  2. java随机生成字符串并排序

    package com.Imooc; import java.util.ArrayList; import java.util.Collections; import java.util.List; ...

  3. SQL中Case的使用方法(上篇)(转)

    http://www.cnblogs.com/fxgachiever/archive/2010/09/09/1822106.html Case具有两种格式.简单Case函数和Case搜索函数. --简 ...

  4. nginx Location配置总结(转)

    本文部分转自:http://cssor.com/nginx-location-configuration.html 一. 开头 语法规则: location [=|~|~*|^~] /uri/ { … ...

  5. http://blog.csdn.net/carolzhang8406/article/details/7196011

    http://blog.csdn.net/carolzhang8406/article/details/7196011

  6. 对TCP/IP网络协议的深入浅出归纳(转)

    前段时间做了一个开发,涉及到网络编程,开发过程比较顺利,但任务完成后始终觉得有一些疑惑.主要是因为对网络协议不太熟悉,对一些概念也没弄清楚.后来 我花了一些时间去了解这些网络协议,现在对TCP/IP网 ...

  7. 带你走进EJB--MDB实现发送邮件(3)

    接上篇,在业务逻辑中已经发送JMS消息,而接下来的消息驱动Bean作为JMS消息监听器,主要是负责监听指定的JMS消息,此时已经接受到JMS的消息,那么MDB的onMessage()方法会被触发.调用 ...

  8. 在运行时切换 WinForm 程序的界面语言 ---------多语言设置基础

    System.ComponentModel.ComponentResourceManager .ApplyResources 时间:2015-06-17 14:59:06      阅读:473    ...

  9. ubunt下的MinimalCD

    ubuntu有MinimalCD,水平高的衍生版制作者基于MinimalCD安装并编译,定制出独特风格的ubuntu衍生版,如 crunchbang.水平不高的个人用户可以从Alternate(文字安 ...

  10. PHP数组排列

    一.先看最简单的情况.有两个数组: $arr1 = array(1,9,5);$arr2 = array(6,2,4); array_multisort($arr1,$arr2); print_r($ ...