AtCoder Beginner Contest 174
第一次 ak ABC,纪念一下。
比赛链接:https://atcoder.jp/contests/abc174
A - Air Conditioner
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << (n >= 30 ? "Yes" : "No"); }
B - Distance
#include <bits/stdc++.h> using namespace std; int main() { int n, d; cin >> n >> d; int cnt = 0; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; if (1ll * x * x + 1ll * y * y <= 1ll * d * d) ++cnt; } cout << cnt << "\n"; }
C - Repsept
#include <bits/stdc++.h> using namespace std; int main() { int k; cin >> k; int ans = 1; for (int mod = 7; mod % k; mod = (mod * 10 + 7) % k) if (++ans > k) { ans = -1; break; } cout << ans << "\n"; }
D - Alter Altar
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; int ans = 0; int l = 0, r = n - 1; while (l < r) { if (s[l] == 'W') { while (r > l and s[r] != 'R') --r; if (r > l) { ++ans; ++l; --r; } } else ++l; } cout << ans << "\n"; }
E - Logs
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100; int n, k; int a[N]; bool ok(int mid) { int need = 0; for (int i = 0; i < n; i++) { need += (a[i] - 1) / mid; } return need <= k; } int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } int l = 1, r = 1e9; while (r - l > 1) { int mid = (l + r) / 2; if (ok(mid)) r = mid; else l = mid; } cout << (ok(l) ? l : r) << "\n"; }
F - Range Set Query
原题P1972。
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 100; int a[N], bit[N], vis[N], ans[N]; struct P{ int l, r, pos; }p[N]; void add(int pos, int val) { for (int i = pos; i <= N; i += i & (-i)) { bit[i] += val; } } int sum(int pos) { int ans = 0; for (int i = pos; i != 0; i -= i & (-i)) { ans += bit[i]; } return ans; } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= m; i++) { cin >> p[i].l >> p[i].r; p[i].pos = i; } sort(p + 1, p + 1 + m, [&] (P x, P y) { return x.r < y.r; }); int r = 1; for (int i = 1; i <= m; i++) { for (int j = r; j <= p[i].r; j++) { if (vis[a[j]]) add(vis[a[j]], -1); add(j, 1); vis[a[j]] = j; } r = p[i].r + 1; ans[p[i].pos] = sum(p[i].r) - sum(p[i].l - 1); } for (int i = 1; i <= m; i++) { cout << ans[i] << "\n"; } }