Codeforces Round #598 (Div. 3)
比赛链接:https://codeforces.com/contest/1256
A - Payment Without Change
题意
有 a 枚价值 n 和 b 枚价值 1 的硬币,问能否凑出价值 s 。
题解
先尽可能多得用价值 n 的硬币,然后再用价值 1 的硬币。
代码
#include <bits/stdc++.h> using namespace std; void solve() { int a, b, n, s; cin >> a >> b >> n >> s; cout << (min(a, s / n) * n + b >= s ? "YES" : "NO") << "\n"; } int main() { int t; cin >> t; while (t--) solve(); }
B - Minimize the Permutation
题意
n 的一个排列数组中,每个 a[i] 可以与 a[i+1] 交换一次,问能够生成的最小排列。
题解
贪心,先把 1 排在最前面,然后是 2 、3 ...
代码
#include <bits/stdc++.h> using namespace std; const int M = 105; int a[M], pos[M]; void solve() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; } int pre = 1; for (int i = 1; i <= n; i++) { if (pos[i] < pre) continue; for (int j = pos[i] - 1; j >= pre; j--) { if (a[j] > a[j + 1]) { swap(a[j], a[j + 1]); } } pre = pos[i]; } for (int i = 1; i <= n; i++) { cout << a[i] << " \n"[i == n]; } } int main() { int t; cin >> t; while (t--) solve(); }
C - Platforms Jumping
题意
开始时在点 0 处,有一条长 n 的河,每次能跳的最远距离为 d,可以放 m 个长为 ci 的木板,木板不能重叠,问能否从 0 跳至 n + 1,如果可以,输出木板的放置方案。
题解
先计算能跳到的最远位置,如果小于 n + 1 则无解,否则考虑木板外的河水长度,贪心地把这些长度插在 m 个木板两侧。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n, m, d; cin >> n >> m >> d; int c[m] = {}; int far = 0; for (int i = 0; i < m; i++) { cin >> c[i]; far += d + c[i] - 1; } far += d;//最后一块木板再跳一次 if (far < n + 1) { cout << "NO"; return 0; } cout << "YES" << "\n"; int zero = n - accumulate(c, c + m, 0); for (int i = 0; i < m; i++) { for (int j = 0; j < min(zero, d - 1); j++) { cout << "0 "; } zero -= d - 1; for (int j = 0; j < c[i]; j++) { cout << i + 1 << ' '; } } for (int j = 0; j < min(zero, d - 1); j++) { cout << "0 "; } }
D - Binary String Minimizing
题意
有一长 n 的 01 串,可以移动字符 k 次,问能得到的字典序最小的 01 串。
题解
从前往后依次记录所有 0 的位置,逐个尽可能地放在最前面。
代码
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k; cin >> n >> k; string s; cin >> s; vector<int> pos; for (int i = 0; i <= s.size(); i++) { if (s[i] == '0') { pos.push_back(i); } } string ans(n, '1'); for (int i = 0; i < pos.size(); i++) { if (k >= pos[i] - i) { k -= pos[i] - i; ans[i] = '0'; } else { ans[pos[i] - k] = '0'; for (int j = i + 1; j < pos.size(); j++) ans[pos[j]] = '0'; break; } } cout << ans << "\n"; } int main() { int t; cin >> t; while (t--) solve(); }

浙公网安备 33010602011771号