跳到正文

算法·理论

代码源 Day2 总结

CuteGirlGC约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟

版本

  1. v1当前

抢到 D 题首A,嘻嘻。

B 题70pts变 0pts,不嘻嘻。

Final

A

挺简单的,答案是 1 的个数加联通块个数。

因为你需要跳到一个联通块的开头,然后在一个联通块里面跳联通块大小减一次,最后就需要跳回到目录,易证这是最小值。

C++
#include <iostream>

using namespace std;

const int kMaxN = 1005;

int n, a[kMaxN];

int main() {
  cin >> n;
  int ans = 0, cnt = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (cnt && a[i] == 0) {
      ans += 1 + cnt, cnt = 0;
    } else if (a[i] == 1) {
      cnt++;
    }
  }
  if (cnt) {
    ans += cnt;
  } else {
    ans--;
  }
  cout << (ans <= 0 ? 0 : ans) << '\n';
  return 0;
}

B

错因是算出来的字符串错了。

C++
char a = i + '0', b = j + '0';
c += m - n;
for (int i = 0; i < m; i++) {
  char tmp = (i & 1 ? b : a); // 这里
  if (tmp > r[i]) {
    c--;
    break;
  } else if (tmp < r[i]) {
    break;
  }
}
for (int i = 0; i < n; i++) {
  char tmp = (i & 1 ? b : a); // 这里
  if (tmp < l[i]) { // here
    // cout << i << ' ' << tmp << '\n';
    break;
  } else if (tmp > l[i]) { // 原本这两个地方的 l 打成 r 了
    c++;
    break;
  } else if (i == n - 1) {
    c++;
  }
}

其他的就如上面代码所示,下次需要考虑地更加详细。

C

其实我算 p:∃k∈[l,r],k & ans=ansp:\exists k \in[l, r],k\ \&\ ans=ans 这个命题的时候算错了:(。

下次不能直接就交,而且最后写出来的正解是 0pts,但是暴力有 70pts。

正确的思路是下哦那个高位到低位考虑,然后贪心地选择每一位,尽量选择二进制为较大的,因为 2k>∑i=0k−12i2^k > \sum\limits_{i=0}^{k-1}2^i。

最后写出来的代码:

C++
#include <iostream>

using namespace std;
using ll = long long;

const ll kMaxN = 2e5 + 1;

ll n, l[kMaxN], r[kMaxN], now[kMaxN];

int main() {
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> l[i] >> r[i];
  }
  ll ans = 0;
  for (ll i = 61; i >= 0; i--) {
    bool f = 1;
    for (ll j = 1; j <= n; j++) {
      ll L = now[j] + (1ll << i), R = now[j] + (2ll << i) - 1;
      if (L > r[j] || R < l[j]) {
        f = 0;
        break;
      }
    }
    if (f) {
      ans += 1ll << i;
      for (ll j = 1; j <= n; j++) {
        now[j] += 1ll << i;
      }
    } else {
      for (ll j = 1; j <= n; j++) {
        ll L = now[j] + (1ll << i), R = now[j] + (2ll << i) - 1;
        if (L <= r[j] && R >= l[j]) {
          if (L - 1 < l[j]) {
            now[j] += (1ll << i);
          }
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}

D-CF478D Red-Green Towers

首先算出最高高度是 s=⌊2(g+r)⌋s=\lfloor\sqrt{2(g+r)}\rfloor 这个数上下浮动一下,在较大的时候 ss 会偏大,需要减一。

然后把 1,2,⋯ ,s−1,s1, 2, \cdots, s -1, s 看作 ss 个物品做背包 DP。rr 为背包容量,设最后求出来的数组是 ff,然后求 ∑i=s(s+1)2−grfi\sum \limits_{i=\frac{s(s+1)}{2} - g}^{r}f_i。

C++
#include <assert.h>

#include <cmath>
#include <ctime>
#include <iostream>

using namespace std;
using ll = long long;

const ll kMaxN = 200005, kMod = 1e9 + 7;

ll r, g, f[kMaxN], a[kMaxN];

int main() {
  cin >> r >> g;
  ll s = sqrt((r + g) * 2);
  if (s * (s + 1) / 2 > r + g) {
    s--;
  }
  if (r > g) {
    swap(r, g);
  }
  f[0] = 1;
  for (int i = 1; i <= s; i++) {
    for (int j = r; j >= i; j--) {
      f[j] = (f[j] + f[j - i]) % kMod;
    }
  }
  ll ans = 0;
  for (int i = r; i + g >= s * (s + 1) / 2; i--) {
    ans = (ans + f[i]) % kMod;
  }
  cout << ans << '\n';
  return 0;
}
原文