Codeforces Round #609 (Div. 2)
比赛链接:https://codeforces.com/contest/1269
A. Equation
题意
输出两个合数,使得二者之差为 n 。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << 9 * n << ' ' << 8 * n; }
B. Modulo Equality
题意
将数组 a 在模 m 的意义下加上 x,使得其与数组 b 相等,求 x 的最小值。
题解
逐值尝试范围太大,枚举 b[i] - a[0] 即可。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(b.begin(), b.end()); int ans = m; for (int i = 0; i < n; i++) { int x = (b[i] - a[0] + m) % m; vector<int> c(n); for (int j = 0; j < n; j++) c[j] = (a[j] + x) % m; sort(c.begin(), c.end()); if (c == b) ans = min(ans, x); } cout << ans; }
C. Long Beautiful Integer
题意
求不小于字符串 s 的最小的周期为 k 的字符串 t 。
题解
先用字符串 s 的前 k 个字符构造周期字符串,若小于 s,在前 k 个字符中从后向前找到第一个可以变大的位置,该位置字符加一,该位置之后的字符都置为最小,再用这 k 个字符构造周期字符串即可。
本题中的字符串由 '0' ~ '9' 构成,即从后向前找到第一个不为 '9' 的位置,该位置字符加一,该位置之后的字符都置为 '0' 。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string s; cin >> s; string t = s; for (int i = 0; i < n; i++) t[i] = s[i % k]; if (t < s) { for (int i = k - 1; i >= 0; i--) { if (s[i] != '9') { ++s[i]; for (int j = i + 1; j < k; j++) s[j] = '0'; break; } } for (int i = 0; i < n; i++) t[i] = s[i % k]; } cout << t.size() << "\n" << t; }
D. Domino for Young
题意
有一排高度不等的方格,最多可以放多少 1 x 2 和 2 x 1 的方块。
题解
偶数高度的方格放置竖置方块即可,如果两个奇数高度的方格间有偶数个偶数高度的方格,那么中间的这些偶数方格可以横置方块来给两边的奇数方格腾出最下面一排放置方块。
代码
#include <bits/stdc++.h> using namespace std; stack<int> stk; int main() { int n; cin >> n; long long ans = 0; for (int i = 0; i < n; i++) { int x; cin >> x; ans += x / 2; if (x & 1) { if (stk.empty() or stk.top() == i % 2) stk.push(i % 2); else stk.pop(), ++ans; } } cout << ans; }
参考博客:https://blog.csdn.net/tomjobs/article/details/103676175

浙公网安备 33010602011771号