贪心+枚举,CF 351A - Jeff and Rounding

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

351A - Jeff and Rounding


二、解题报告

1、思路分析

我们只关注后三位

将后三位映射到[0, 999]

考虑上取整就相当于变1000

我们先计算初始和tot

然后枚举上取整的数目 i 

假如有 i 个上取整,那么差就是abs(tot - i * 1000)

2、复杂度

时间复杂度: O(N)空间复杂度:O(1)

3、代码详解

 ​
#include <bits/stdc++.h>
#define sc scanf
using i64 = long long;
using PII = std::pair<int, int>;
constexpr int inf32 = 1e9 + 7;
constexpr i64 inf64 = 1e18 + 7;
constexpr int P = 998244353;
constexpr double eps = 1e-6;

// #define DEBUG

void solve()
{
    int n;
    std::cin >> n;
    int tot = 0, c0 = 0;
    for (int i = 0; i < n * 2; ++ i) {
        std::string s;
        std::cin >> s;
        int a = 0;
        for (int i = s.size() - 3; i < s.size(); ++ i)
            a = a * 10 + (s[i] ^ 48);
        tot += a;
        if (!a) 
            ++ c0;
    }
    int res = 1000 * 2 * n;
    for (int i = std::max(n - c0, 0); i <= std::min(n, 2 * n - c0); ++ i)
        res = std::min(res, abs(tot - i * 1000));
    
    std::cout << res / 1000 << '.';
    res %= 1000;
    std::cout << res / 100;
    res %= 100;
    std::cout << res / 10 << res % 10;
}

int main()
{
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
    int _ = 1;
    // std::cin >> _;
    while (_--)
        solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EQUINOX1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值