一、题目
1、题目描述
2、输入输出
2.1输入
2.2输出
3、原题链接
二、解题报告
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;
}