跳到正文

个人记录

The Final Duel(R17-10.30)

Bc2_Ch1ckenPr1nce约 4 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

最后一场……

重组字母(anagram)

期望得分 100100,实际得分 100100。

唐氏送分题。

直接对 cch 的字符串的每个字符 sis_i 分类讨论:

  • si=as_i = \texttt{a}:cwj 只能填 b\texttt{b} 和 c\texttt{c}。显然填 b\texttt{b} 最优,如果没有 b\texttt{b} 了就填 c\texttt{c},但是需要判断用完这个 b\texttt{b} 之后后面的 c\texttt{c} 是否有足够的 a\texttt{a} 和 b\texttt{b} 来填。
  • si=bs_i = \texttt{b}:cwj 只能填 a\texttt{a} 和 c\texttt{c}。显然填 a\texttt{a} 最优,如果没有 a\texttt{a} 了就填 c\texttt{c},但是需要判断用完这个 a\texttt{a} 之后后面的 c\texttt{c} 是否有足够的 a\texttt{a} 和 b\texttt{b} 来填。
  • si=cs_i = \texttt{c}:cwj 只能填 a\texttt{a} 和 b\texttt{b}。显然填 a\texttt{a} 最优,如果没有 a\texttt{a} 了就填 b\texttt{b},但是需要判断用完这个 a\texttt{a} 之后后面的 b\texttt{b} 是否有足够的 a\texttt{a} 和 c\texttt{c} 来填。

对于额外条件,由于 n≤5000n \le 5000,可以用暴力或者后缀和来处理,时间复杂度分别为 O(n2)\mathcal{O}(n^2) 和 O(n)\mathcal{O}(n)。

C++
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ull = unsigned long long;

const int kMaxN = 5050;

int n, c[4], suf[kMaxN][4];
string cwj, cch;

int main() {
  // freopen("anagram.in", "r", stdin);
  // freopen("anagram.out", "w", stdout);
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> cwj >> cch;
  for (int i = 0; i < n; ++ i) {
    ++ c[cwj[i] - 'a' + 1];
  }
  for (int i = n - 1; i >= 0; -- i) {
    for (int j = 1; j <= 3; ++ j) {
      suf[i][j] = suf[i + 1][j];
    }
    if (cch[i] == 'a') {
      ++ suf[i][1];
    } else if (cch[i] == 'b') {
      ++ suf[i][2];
    } else {
      ++ suf[i][3];
    }
  }
  for (int i = 0; i < n; ++ i) {
    if (cch[i] == 'b') {
      if (c[1] && c[1] + c[2] - 1 >= suf[i + 1][3]) {
        cout << 'a';
        -- c[1];
      } else {
        cout << 'c';
        -- c[3];
      }
    } else if (cch[i] == 'a') {
      if (c[2] && c[1] + c[2] - 1 >= suf[i + 1][3]) {
        cout << 'b';
        -- c[2];
      } else {
        cout << 'c';
        -- c[3];
      }
    } else {
      if (c[1] && c[1] + c[3] - 1 >= suf[i + 1][2]) {
        cout << 'a';
        -- c[1];
      } else {
        cout << 'b';
        -- c[2];
      }
    }
  }
  cout << '\n';
  return 0;
}

区间整除(intdiv)

期望得分 100100,实际得分 100100。

数学都会吧?

原文