跳到正文

题解

题解:P12690 [KOI 2022 Round 1] ABBC

wqc2011约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年10月20日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 3 分钟
题目
P12690

版本

  1. v2当前
  2. v1

传送门

hack

我的代码就是这样挂的。

C++
BABC
C++
2

思路

貌似是一道贪心。

我们从后向前遍历 ss,需要记下如下信息:

  1. cntBcntB:表示到第 ii 位,我们当前可用的 BB 的数量;
  2. cntCcntC:表示到第 ii 位,我们当前可用的 CC 的数量;
  3. cntABcntAB:表示到第 ii 位,我们已经组好的 ABAB 数量;
  4. cntBCcntBC:表示到第 ii 位,我们已经组好的 BCBC 数量;

如何转移?我们可以分类讨论:

  1. si=As_i = A
{cntAB+1,cntB−1(cntB≠0)cntAB+1,cntBC−1,cntC+1(cntB=0且cntBC≠0)不操作(cntB=0且cntBC=0)\begin{cases} cntAB + 1,cntB - 1 & (cntB \ne 0) \\ cntAB + 1,cntBC - 1,cntC + 1 & (cntB = 0 \text{且} cntBC \ne 0) \\ \text{不操作} & (cntB = 0 \text{且} cntBC = 0) \end{cases}

容易证明这样更优,因为答案总数不变,我们还会多出一个 CC。

  1. si=Bs_i = B
{cntBC+1,cntC−1(cntC≠0)cntB+1(cntC=0)\begin{cases} cntBC + 1,cntC - 1 & (cntC \ne 0) \\ cntB + 1 & (cntC = 0) \end{cases}
  1. si=Cs_i = C
{cntC+1\begin{cases} cntC + 1\end{cases}

答案即为 cntAB+cntBCcntAB + cntBC。

代码

C++
#include<bits/stdc++.h>
#define int long long
#pragma GCC optimize(3)
using namespace std;
const int maxn = 100010;
const int inf = 1e9;
const int hs = 131;// 或 1331
//unsigned long long
//cout << fixed << setprecision(3)
//cout << setw(5) <<
//continue
signed main(){
    // freopen("aabc.in", "r", stdin);
    // freopen("aabc.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    // cout << s.length();
    int n = s.length(), ansab = 0, ansbc = 0, b = 0, c = 0;
    for(int i = n - 1;i >= 0;i--){
    	if(s[i] == 'A'){
    		if(b > 0) ansab++, b--;
    		else if(ansbc > 0) ansab++, ansbc--, c++;
    	}else if(s[i] == 'B'){
    		if(c > 0) ansbc++, c--;
    		else b++;
    	}else c++;
    }
    cout << ansab + ansbc;
    return 0;
}

感谢阅读!

原文
存档评论2 条抓取于