A - Difference Max

区间左端减去区间右端

  1. int main() {
  2. ios_base::sync_with_stdio(false), cin.tie(0);
  3. int a, b, c, d;
  4. cin >> a >> b >> c >> d;
  5. cout << b - c << endl;
  6. return 0;
  7. }

B - Round Down

一开始想错了,应该String读入,然后find(‘.’)的位置即可,如果没有说明原来就是整数

  1. int main() {
  2. ios_base::sync_with_stdio(false), cin.tie(0);
  3. string s;
  4. cin >> s;
  5. int idx = s.find('.');
  6. if (idx == -1) cout << s;
  7. else
  8. for (int i = 0; i < idx; ++i) cout << s[i];
  9. return 0;
  10. }

C - Doubled

AtCoder Beginner Contest 196 个人题解 - 图1 过大,所以不可能直接通过整数解决。

利用 字符串 的连接性质转化为 long long 进行判断即可,尽管稍微仍比较耗时但能够 AC

  1. using ll = long long;
  2. int main() {
  3. ios_base::sync_with_stdio(false), cin.tie(0);
  4. ll n, x = 0;
  5. cin >> n;
  6. while (stoll(to_string(x) + to_string(x)) <= n) x++;
  7. cout << x - 1 << "\n"; // 把 0 算进去了,所以要减一
  8. return 0;
  9. }

D - Hanjo

题意:在 HW 的面积里放置 A个 AtCoder Beginner Contest 196 个人题解 - 图2 长方块榻榻米,B个 AtCoder Beginner Contest 196 个人题解 - 图3 方形榻榻米。请问有多少种放置方法。

根据官方的题解,对于这个 AtCoder Beginner Contest 196 个人题解 - 图4 是约束很小的情况,所以我们可以使用 DFS 进行完全爆搜。

从左上角的格子开始,尝试放置一个方形榻榻米垫,个长方形榻榻米垫,在左侧或下方突出;然后我们可以穷尽地搜索所有的覆盖方式。
现在让我们估计一下时间复杂度。
我们有左上角单元格和榻榻米方向的选择,所以我们有答案的上界 AtCoder Beginner Contest 196 个人题解 - 图5#card=math&code=2%5EA%28%5Cbegin%7Bsmallmatrix%7D%20HW%5C%5CA%20%5Cend%7Bsmallmatrix%7D%29)。
由于 AtCoder Beginner Contest 196 个人题解 - 图6#card=math&code=2%5EA%28%5Cbegin%7Bsmallmatrix%7D%20HW%5C%5CA%20%5Cend%7Bsmallmatrix%7D%29) 的最大值为 AtCoder Beginner Contest 196 个人题解 - 图7,所以速度足够快。

事实上,最大答案是3388,所以它的工作速度比估计的要快得多。

  1. int H, W, A, B, ans = 0;
  2. void dfs(int i, int bit, int A, int B) {
  3. if (i == H * W) return (void)ans++;
  4. if (bit & 1 << i) return dfs(i + 1, bit, A, B);
  5. if (B) dfs(i + 1, bit | 1 << i, A, B - 1);
  6. if (A) {
  7. if (i % W != W - 1 && ~bit & 1 << (i + 1))
  8. dfs(i + 1, bit | 1 << i | 1 << (i + 1), A - 1, B);
  9. if (i + W < H * W) dfs(i + 1, bit | 1 << i | 1 << (i + W), A - 1, B);
  10. }
  11. }
  12. int main() {
  13. ios_base::sync_with_stdio(false), cin.tie(0);
  14. cin >> H >> W >> A >> B;
  15. dfs(0, 0, A, B);
  16. cout << ans << "\n";
  17. return 0;
  18. }

E - Filters

https://atcoder.jp/contests/abc196/editorial/953

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <numeric>
  4. using namespace std;
  5. using ll = long long;
  6. const ll INF = numeric_limits<ll>::max() / 4;
  7. void chmin(ll& a, ll b) { return (void)(a > b ? a = b : a = a); }
  8. void chmax(ll& a, ll b) { return (void)(a < b ? a = b : a = a); }
  9. int main() {
  10. ios_base::sync_with_stdio(false), cin.tie(0);
  11. ll N;
  12. cin >> N;
  13. ll low = -INF, high = INF, add = 0;
  14. for (ll i = 0; i < N; ++i) {
  15. ll A, T;
  16. cin >> A >> T;
  17. if (T == 1) low += A, high += A, add += A;
  18. else if (T == 2)
  19. chmax(low, A), chmax(high, A);
  20. else
  21. chmin(low, A), chmin(high, A);
  22. }
  23. ll Q, x;
  24. cin >> Q;
  25. while (Q--) {
  26. cin >> x;
  27. // clamp 用来判断一个值是否“夹”在另外一对值之间,如果在区间内则返回
  28. // x,如果大于左区间则返回左区间,不然返回右区间
  29. // cout << clamp(x + add, low, high) << '\n';
  30. cout << min(high, max(low, x + add)) << "\n";
  31. }
  32. return 0;
  33. }