AtCoder Beginner Contest 417 赛后反思

· · 算法·理论

第 1 题:A - A Substring

思路:

按照题意,进行字符串的模拟即可。

AC CODE

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl '\n'
#define reg register
#define pii pair<int, int>
#define cmax(a, b) a > b ? a : b
#define cmin(a, b) a < b ? a : b
#define rep(i, a, b) for (reg int i = a; i <= b; ++i)
#define reb(i, a, b) for (reg int i = a; i >= b; --i)
inline int read() {
  reg int x = 0, f = 1;
  reg char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
inline void write(reg int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
int n, l, r;
string s;
int main() {
  n = read(), l = read(), r = read();
  cin >> s;
  for (int i = l; i < n - r; i++) cout << s[i];
  return 0;
}

第 2 题: B - Search and Delete

思路:

暴力枚举 O(n^2) 可过,但注重对于二分模版的理解,建议写一遍二分最优解。

AC CODE(暴力版本O(n^2))

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl '\n'
#define reg register
#define pii pair<int, int>
#define cmax(a, b) a > b ? a : b
#define cmin(a, b) a < b ? a : b
#define rep(i, a, b) for (reg int i = a; i <= b; ++i)
#define reb(i, a, b) for (reg int i = a; i >= b; --i)
inline int read() {
  reg int x = 0, f = 1;
  reg char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
inline void write(reg int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
int n, m, a[105], x;
int main() {
  n = read(), m = read();
  rep(i, 1, n) a[i] = read();
  rep(i, 1, m) {
    x = read();
    rep(j, 1, n) {
      if (a[j] == x) {
        a[j] = 2e9;
        break;
      }
    }
  }
  rep(i, 1, n) if (a[i] != 2e9) write(a[i]), putchar(' ');
  return 0;
}

AC CODE(二分算法O(nlogn))

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl '\n'
#define reg register
#define pii pair<int, int>
#define cmax(a, b) a > b ? a : b
#define cmin(a, b) a < b ? a : b
#define rep(i, a, b) for (reg int i = a; i <= b; ++i)
#define reb(i, a, b) for (reg int i = a; i >= b; --i)
inline int read() {
  reg int x = 0, f = 1;
  reg char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
inline void write(reg int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const int N = 105;
int n, m, a[N], x;
inline int find(reg int num) {
    reg int l = 1, r = n, mid;
    while (l <= r) {
        mid = (l + r) >> 1;
        if (a[mid] == num) return mid;
        else if (a[mid] < num) l = mid + 1;
        else r = mid - 1;
    }
    return 0;
}
int main() {
  n = read(), m = read();
  rep (i ,1, n) a[i] = read();
  rep (i, 1, m) {
    sort (a + 1, a + n + 1);
    x = read();
    int idx = find(x);
    a[idx] = 2e9;
  }
  rep (i ,1, n) if (a[i] != 2e9) write(a[i]), putchar(' ');
  return 0;
}

第 3 题:C - Distance Indicators

思路:

可以将式子 j - i = a_i + a_j 转换为 i + a_i = j - a_j,用哈希表 O(n) 记录前者,再以 O(n) 的复杂度检查后者,简单计数即可。

AC CODE

#include <bits/stdc++.h>
#define int long long
using namespace std;
using ll = long long;
#define endl '\n'
#define reg register
#define pii pair<int, int>
#define cmax(a, b) a > b ? a : b
#define cmin(a, b) a < b ? a : b
#define rep(i, a, b) for (reg int i = a; i <= b; ++i)
#define reb(i, a, b) for (reg int i = a; i >= b; --i)
inline int read() {
  reg int x = 0, f = 1;
  reg char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
inline void write(reg int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
int n, a[200005];
unordered_map<int, int> mp;
signed main() {
  n = read();
  rep(i, 1, n) a[i] = read(), mp[a[i] + i]++;
  ll ans = 0;
  rep(i, 1, n) ans += mp[i - a[i]];
  write(ans);
  return 0;
}

第 4 题:D - Takahashi's Expectation

思路:(官方题解)

这个问题可以通过逆序扫描礼物来解决。

定义一个 DP(动态规划)表如下:

dp[i][j] := \text{如果在收到第 }i\text{ 个礼物后(或当 }i=0\text{ 时,初始心情为 }j\text{)的最终心情值}

则可以按如下方式递推:

dp[i][j] = \begin{cases} dp[i+1][j + A_i] & (j \leq P_i) \\ dp[i+1][\max\{0, j - B_i\}] & \text{(否则)} \end{cases}
但是,如果对所有 (0 \leq j \leq 10^9) 都求解 DP 表,是难以在时间限制内完成的。注意到:如果高桥的心情值 (j) 远大于某个礼物的可能值 (P_i),那么心情的转移几乎是显然的;这有助于我们缩小 DP 表的范围。
如果我们只在合适的范围内计算 DP 表,当给定的查询超出计算范围时,答案可以通过计算他收到礼物直到心情值进入范围为止得到,因为此时心情会一直递减,直到进入该范围。

关于 DP 表尺寸有两种策略:

  1. 只在 (0 \leq j \leq \max_i P_i) 内计算。

    这种方法内存占用小,但在预处理时需要访问 DP 表外的值,使得转移变复杂。
  2. 只在 (0 \leq j \leq \max_i {A_i + P_i}) 内计算。

    这种方法预处理不需要访问 DP 表外的值,但内存占用较大。
下方是样例代码,在本样例中采用的是第二种 DP 表范围策略。
#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
#include <ranges>
#include <vector>

int main() {
  using namespace std;
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  unsigned N;
  cin >> N;
  vector<array<unsigned, 3>> P(N);
  for (auto&& [p, a, b] : P) cin >> p >> a >> b;

  // Manage mood up to {max (P + A)}
  const auto M{ranges::max(
      P | views::transform([](const auto& p) { return p[0] + p[1]; }))};

  // dp[i][j] := the final mood value if the mood value right after receiving
  // present i is j.
  vector dp(N + 1, vector<unsigned>(M + 1));

  // If the mood after receiving all presents is j, then the final mood is j
  iota(begin(dp.back()), end(dp.back()), 0);

  for (unsigned i = N; i--;) {
    const auto& [p, a, b] = P[i];
    for (unsigned j = 0; j <= M; ++j)
      // +A if the current mood value is not greater than the present's value,
      // and -B otherwise
      dp[i][j] = j <= p ? dp[i + 1][j + a] : dp[i + 1][j - min(j, b)];
  }

  // Find the cumulative sums to determine when the mood falls into the range
  // when a large mood value is found.
  vector<unsigned> sum_B(N);
  ranges::copy(P | views::elements<2>, begin(sum_B));
  inclusive_scan(begin(sum_B), end(sum_B), begin(sum_B), plus{});

  // The final mood value when the initial mood is `x` (including offset)
  const auto access{[&dp, &sum_B, N, M](unsigned x) -> unsigned {
    // If it is within the range initially, then directly return the dp value
    if (x <= M) return dp[0][x];

    // If larger, keep descending for a while
    const auto need_down{ranges::lower_bound(sum_B, x - M)};
    // If descending N times is not sufficient, just descend N times
    if (need_down == end(sum_B)) return x - sum_B.back();
    // Once reaching the range, return the DP value
    return dp[need_down - begin(sum_B) + 1][x - min(x, *need_down)];
  }};

  unsigned Q;
  cin >> Q;
  for (unsigned i = 0; i < Q; ++i) {
    unsigned x;
    cin >> x;
    cout << access(x) << '\n';
  }
  cout << flush;
  return 0;
}

第 5 题:E - A Path in A Dictionary

思路:

贪心 bfs,开两个标记桶 vis1 和 vis2,分别记录 走到了哪些点 和 还能走哪些点,每次 x 取最优的邻居点,最终输出即可。

AC CODE

#include <bits/stdc++.h>
using namespace std;
#define N 1005
int t, n, m, x, y, u, v, v1[N], v2[N], tmp;
vector<int> g[N], ans;
queue<int> q;
main() {
  cin >> t;
  while (t--) {
    cin >> n >> m >> x >> y, ans.clear();
    for (int i = 1; i <= n; i++) g[i].clear(), v1[i] = 0;
    while (m--) cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
    for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end());
    v1[x] = 1, ans.push_back(x);
    while (x != y) {
      memset(v2, 0, sizeof v2);
      while (!q.empty()) q.pop();
      q.push(y), v2[y] = 1, tmp = 0;
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int v : g[u])
          if (!v1[v] && !v2[v]) v2[v] = 1, q.push(v);
      }
      for (int v : g[x]) {
        if (!v1[v] && v2[v]) {
          tmp = v;
          break;
        }
      }
      v1[tmp] = 1, ans.push_back(tmp), x = tmp;
    }
    for (int i : ans) cout << i << ' ';
    cout << '\n';
  }
}