Editorial for Codeforces Round #748 (Div.3)
1593A - Elections
解法:模仿
**时间复杂度 O(1), 空间复杂度 O(1)
#include<bits/stdc++.h>using namespace std;#define endl '\n'const int N = 4E5 + 5;void solve() { int a, b, c; int mx = 0; cin >> a >> b >> c; mx = max(max(a, b), c); int f = (mx == a) + (mx == b) + (mx == c); if (f > 1) { mx += 1; cout << mx - a << " " << mx - b << " " << mx - c << endl; }else { if (mx == a) { cout << 0 << " "; }else cout << mx - a + 1 << " "; if (mx == b) { cout << 0 << " "; } else cout << mx - b + 1 << " "; if (mx == c) { cout << 0 << endl; }else cout << mx - c + 1 << endl; } }int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t --) { solve(); } return 0;}1593B - Make it Divisible by 25
解法:可以大概被25整除的数,其末端分为00,25,50,75四种环境。分别思量这四种环境,从后向前探求对应字符,减去探求过程中的无用字符即为答案,取min即可。
时间复杂度 O(N),空间复杂度 O(N)
#include<bits/stdc++.h>using namespace std;#define endl '\n'const int N = 4E5 + 5;void solve() { string s; cin >> s; int ret = 20; int d = 0; int n = s.size(); for (int i = s.size() - 1; i >= 0; -- i) { if (s != '0' && s != '5') continue; for (int j = i - 1; j >= 0; -- j) { if (s == '0') { if (s[j] == '0' || s[j] == '5') { ret = min(ret, i - j - 1 + n - i - 1); break; } } else { if (s[j] == '2' || s[j] == '7') { ret = min(ret, i - j - 1 + n - i - 1); break; } } } } cout << ret << endl; }int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t --) { solve(); } return 0;}1593C - Save More Mice
解法:模仿 & 贪婪,让离洞近来的老鼠先入洞
时间复杂度 O(N * log N),空间复杂度 O(N)
#include<bits/stdc++.h>using namespace std;#define endl '\n'const int N = 4E5 + 5;void solve() { int n, k; cin >> n >> k; vector<int> a; for (int i = 0; i < k; ++ i) { int t; cin >> t; a.push_back(t); } sort(a.begin(), a.end(), [](int& x, int& y) { return x > y; }); int ret = 0; int c = 0; for (int i = 0; i < a.size(); ++ i) { if (c >= a) break; if (a == n) ++ ret; else { c += n - a; ++ ret; } } cout << ret << endl; }int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t --) { solve(); } return 0;}1593D1 - All are Same
解法:
1.若存在一个数 K 能使全部数通过相减而相称,则 k 肯定是这个数列相邻两项差别的数的差值的因子,这里可以使用这个数列的 最大值 和 最小值 的差值 即 K | (max - min)。
2.存在数列 A1 A2 A3 ... An ,其差值为 A12 A23 ... A(n - 1)(n), 则 K 可表现为 K = gcd{A12, A23, ..., A(n - 1)(n)};
两种方法本质雷同
<strong>第一种:时间复杂度 O(N * |