跳到正文

题解

题解:P12690 [KOI 2022 Round 1] ABBC

ZBH2015约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年11月20日
收录
2026年11月29日
更新
2026年11月29日
篇幅
约 7 分钟
题目
P12690

版本

  1. v1当前

一道贪心的题目。

贪心策略

  • 删除一个 AA 及其后的某个 BB。

  • 删除一个 BB 及其后的某个 CC。

根据题意,可将操作等价地转换为以下形式。

  • 删除一个 BB 及其前面的某个 AA。

  • 删除一个 BB 及其后面的某个 CC。

我们从后往前倒序遍历,找到了 CC 统计一下,找到一个 BB 就看能不能和 CC 配对,如果能,BCBC 配对的答案加 11,否则统计一下 BB 的个数,当找到了 AA 的时候,如果还有未匹配的 BB,就将 ABAB 配对的答案加 11,否则,BB 的数量不够,需要将 BCBC 配对的答案减 11,拆出来一个 BB,一个 CC,然后将 ABAB 配对。

实现过程

按照贪心策略倒序遍历模拟即可。

倒序遍历的原因:

  • 每往左走一步,可以匹配的 CC 只会越来越多。
  • BB 的选择会更简单:看到 BB 直接看后面还有 CC 吗,有就匹配,没有就保留等待前面的 AA。

代码

C++
//#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define F(i,j,k,l) for(int i=(j);i<=(k);i+=(l))
#define f(i,j,k,l) for(int i=(j);i>=(k);i-=(l))
// #ifdef __unix__
// #define gc getchar_unlocked
// #define pc putchar_unlocked
// #else
// #define gc _getchar_nolock
// #define pc _putchar_nolock
// #endif
#define W(i) while(i)
#define A auto
#define ot float
#define FO(x,a) for(int x:a)
#define Fo(x,a) for(A x:a)
#define el "\n"
#define ull unsigned long long
#define ll long long
#define in __int128
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define dle double
#define ld long double
#define imax INT_MAX
#define imin INT_MIN
#define llmax LONG_LONG_MAX
#define llmin LONG_LONG_MIN
#define debug(x) cerr<<#x<<" "<<x<<"\n";
const int N=1e6+10,M=1<<20;
char buf[M],*p1=buf,*p2=buf,out[M],*p3=out;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,M,stdin)),*p1++)
#define gc() (p1==p2?(p2=(p1=buf)+fread(buf,1,M,stdin),p1==p2?EOF:*p1++):*p1++)
//#define flush() do{fwrite(out,1,p3-out,stdout);p3=out;}while(0)
//#define pc(c) do{if(p3==(out+M)) flush();*p3++=(c);}while(0)
#define flush() (fwrite(out,1,p3-out,stdout),p3=out)
#define pc(c) (p3==(out+M)?(flush(),*p3++=(c)):*p3++=(c))
inline long long read(){
    long long x=0,f=1;
    char ch=gc();
    W(!isdigit(ch)){
        if(ch=='-') f=-1;
        ch=gc();
    }
    W(isdigit(ch)){
        x=x*10+(ch-'0');
        ch=gc();
    }
    return x*f;
}
inline string rsline(){
    string s;
    char ch=gc();
    W(ch!='\n'&&ch!='\r'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline string rs(){
    string s;
    char ch=gc();
    W(ch==' '||ch=='\n'||ch=='\r'||ch=='\t') ch=gc();
    W(ch!=' '&&ch!='\n'&&ch!='\r'&&ch!='\t'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline void write(long long x){
    if(x<0){
        pc('-');
        x=-x;
    }
    if(x==0){
        pc('0');
        return;
    }
    char s[25];
    int len=0;
    W(x){
        s[len++]=char('0'+x%10);
        x/=10;
    }
    W(len) pc(s[--len]);
    return;
}
inline void ws(string &s){
    for(auto x:s) pc(x);
    return;
}
string s;
inline void pre(){
    
}
inline void input(){
    s=rs();
    int siz=s.size();
    int cntc=0,cntb=0,ansab=0,ansbc=0;
    f(i,siz-1,0,1){
        if(s[i]=='C') cntc++;
        else if(s[i]=='B'){
            if(cntc>0) cntc--,ansbc++;
            else cntb++;
        }else{
            if(cntb>0) cntb--,ansab++;
            else if(ansbc>0) ansbc--,cntc++,ansab++;
        }
    }
    write(ansab+ansbc);
}
inline void output(){
    
}
inline void solve(){
    pre();
    input();
    output();
    return;
}
int main(){
    //freopen("","r",stdin);
    //freopen("","w",stdout);
    //ios::sync_with_stdio(0);
    //cin.tie(0);cin.tie(0);
    int _=1;//_=read();
    while(_--) solve();
    flush();
    return 0;
}
原文