标签:names 一半 coder Beginner 有关 contest $0 pre pac
比赛链接:https://atcoder.jp/contests/abc173/tasks
计算只用 $1000$ 元支付某个价格 $n$ 的找零是多少。
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << (1000 - n % 1000) % 1000 << "\n"; }
给出 $n$ 个不外乎为 "AC", "WA", "TLE", "RE" 的字符串,统计四个字符串的个数。
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; map<string, int> mp; for (int i = 0; i < n; i++) { string s; cin >> s; ++mp[s]; } for (auto s : {"AC", "WA", "TLE", "RE"}) cout << s << " x " << mp[s] << "\n"; }
给出一个 $h \times w$ 的黑白方阵,可以选择将一些行和列涂红,问使得方阵中刚好有 $k$ 个黑方块的方案个数。
$dfs$ 枚举所有情况。
#include <bits/stdc++.h> using namespace std; const int N = 10; int h, w, k, ans; char C[N][N]; bool choose_row[N]; bool choose_col[N]; void dfs(int row, int col) { if (row <= h) { choose_row[row] = true; dfs(row + 1, col); choose_row[row] = false; dfs(row + 1, col); } else if (col <= w) { choose_col[col] = true; dfs(row, col + 1); choose_col[col] = false; dfs(row, col + 1); } else { char MP[N][N] = {}; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) MP[i][j] = C[i][j]; for (int i = 1; i <= h; i++) if (choose_row[i]) for (int j = 1; j <= w; j++) MP[i][j] = ‘.‘; for (int i = 1; i <= w; i++) if (choose_col[i]) for (int j = 1; j <= h; j++) MP[j][i] = ‘.‘; int cnt = 0; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) if (MP[i][j] == ‘#‘) ++cnt; if (cnt == k) ++ans; } } int main() { cin >> h >> w >> k; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> C[i][j]; dfs(1, 1); cout << ans << "\n"; }
将每行或列的选取状态压缩为二进制的 $0$ 或 $1$,枚举行和列的所有状态,每次只考虑未涂的行和列的方块。
#include <bits/stdc++.h> using namespace std; int main() { int h, w, k; cin >> h >> w >> k; char MP[h][w] = {}; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> MP[i][j]; int ans = 0; for (int maskR = 0; maskR < (1 << h); maskR++) { for (int maskC = 0; maskC < (1 << w); maskC++) { int cnt = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (!((maskR >> i) & 1) and !((maskC >> j) & 1) and MP[i][j] == ‘#‘) ++cnt; if (cnt == k) ++ans; } } cout << ans << "\n"; }
可以将 $n$ 个数按照任意顺序插进一个空圈中,每次插入一个数的收益为相邻数的最小值,问最大收益为多少。
先用较大的一半数构成一个圈,然后将剩下较小的一半数插入两个较大的数之间,注意最后插入的较小数的收益与 $n$ 的奇偶性有关。
#include <bits/stdc++.h> using ll = long long; using namespace std; int main() { int n; cin >> n; int a[n] = {}; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n, greater<int>()); ll ans = 0; int half = (n + 1) / 2; ans += accumulate(a, a + half - 1, 0LL); ans += accumulate(a + 1, a + 1 + n - half, 0LL); if (n % 2 == 0) ans -= a[half], ans += a[half - 1]; cout << ans << "\n"; }
标签:names 一半 coder Beginner 有关 contest $0 pre pac
原文地址:https://www.cnblogs.com/Kanoon/p/13252243.html