HHKB Programming Contest 2020【ABCE】
阿新 • • 發佈:2020-10-11
比賽連結:https://atcoder.jp/contests/hhkb2020/tasks
A - Keyboard
程式碼
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
char s, t;
cin >> s >> t;
cout << (s == 'Y' ? char(toupper(t)) : t) << "\n";
return 0;
}
B - Futon
題解
每個點只考慮右方和下方的點即可。
程式碼
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int h, w; cin >> h >> w; vector<string> MP(h); for (auto &x : MP) cin >> x; int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (MP[i][j] == '.' and i + 1 < h and MP[i + 1][j] == '.') ++ans; if (MP[i][j] == '.' and j + 1 < w and MP[i][j + 1] == '.') ++ans; } } cout << ans << "\n"; return 0; }
C - Neq Min
程式碼
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; set<int> st; for (int i = 0; i <= 200010; i++) st.insert(i); for (int i = 0; i < n; i++) { int x; cin >> x; st.erase(x); cout << *st.begin() << "\n"; } return 0; }
E - Lamps
題解
假設每盞燈在所有情況中都亮著,則亮著的燈的總數為 \(k \cdot 2^k\) 。
考慮每盞燈不亮的情況有多少種:一盞燈不亮的充要條件是上下左右連通的燈都不亮,設這些燈加上自身總個數為 \(tot\),那麼其餘的 \(k-tot\) 盞燈的亮滅情況是隨意的,即 \(2^{(k - tot)}\) 。
答案即為 $k \cdot 2^k - \sum \limits _{i = 1}^k 2^{(k - tot_i)} $ 。
上下左右連通的燈數用字首和計算一下即可。
程式碼
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 2020;
constexpr int MOD = 1e9 + 7;
char MP[N][N];
int up[N][N];
int dn[N][N];
int lf[N][N];
int rt[N][N];
int k;
int binpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1LL * res * a % MOD;
a = 1LL * a * a % MOD;
b >>= 1;
}
return res;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, w;
cin >> h >> w;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> MP[i][j];
if (MP[i][j] == '.') ++k;
}
}
for (int j = 1; j <= w; j++) {
for (int i = 1; i <= h; i++) {
if (MP[i][j] == '#') {
up[i][j] = 0;
} else {
up[i][j] = up[i - 1][j] + 1;
}
}
}
for (int j = 1; j <= w; j++) {
for (int i = h; i >= 1; i--) {
if (MP[i][j] == '#') {
dn[i][j] = 0;
} else {
dn[i][j] = dn[i + 1][j] + 1;
}
}
}
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (MP[i][j] == '#') {
lf[i][j] = 0;
} else {
lf[i][j] = lf[i][j - 1] + 1;
}
}
}
for (int i = 1; i <= h; i++) {
for (int j = w; j >= 1; j--) {
if (MP[i][j] == '#') {
rt[i][j] = 0;
} else {
rt[i][j] = rt[i][j + 1] + 1;
}
}
}
int ans = k * binpow(2, k);
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (MP[i][j] == '.') {
int tot = up[i][j] + dn[i][j] + lf[i][j] + rt[i][j] - 4 + 1;
ans -= binpow(2, k - tot);
(ans += MOD) %= MOD;
}
}
}
cout << ans << "\n";
return 0;
}