• CodeForces 352C. Jeff and Rounding(贪心)


    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
    ", abs(res));
        return 0;
    }
    

      

  • 相关阅读:
    从xib初始化的UIView如何继承?
    no implicit conversion of nil into String
    @synchronized(self) 加锁引起的Crash
    iOS手工Crash解析
    iOS线程While-True死循环会发生什么
    2019年新年总结
    218. The Skyline Problem-Hard
    ReplayKit 启动录制按钮 RPSystemBroadcastPickerView 的使用
    S212-搜索+字典树-212. Word Search II-(Hard)
    mybatis批量生成
  • 原文地址:https://www.cnblogs.com/wenruo/p/4749843.html
Copyright © 2020-2023  润新知