Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
158120 WZH_William 货币系统 C++ 解答错误 0 1 MS 256 KB 1334 2024-07-21 21:16:54

Tests(0/20):


#include <iostream> #include <vector> #include <numeric> #include <algorithm> bool canRepresentAll(int x, const std::vector<int>& coins) { std::vector<bool> dp(x + 1, false); dp[0] = true; for (const int coin : coins) { for (int i = coin; i <= x; ++i) { if (dp[i - coin]) { dp[i] = true; } } } return dp[x]; } int findMinimalEquivalentSystem(int n, const std::vector<int>& a) { int maxValue = *std::max_element(a.begin(), a.end()) + 1; int minM = n; for (int m = 1; m <= n; ++m) { std::vector<int> b(m); std::iota(b.begin(), b.end(), 1); bool valid = true; for (int value = 1; value < maxValue; ++value) { if (canRepresentAll(value, a) != canRepresentAll(value, b)) { valid = false; break; } } if (valid) { minM = m; break; } } return minM; } int main() { int T; std::cin >> T; while (T--) { int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; ++i) { std::cin >> a[i]; } std::cout << findMinimalEquivalentSystem(n, a) << std::endl; } return 0; }


测评信息: