专栏文章
题解:P12690 [KOI 2022 Round 1] ABBC
P12690题解参与者 3已保存评论 2
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @mink22g5
- 此快照首次捕获于
- 2025/12/02 03:41 3 个月前
- 此快照最后确认于
- 2025/12/02 03:41 3 个月前
hack
BABC
CPP2
思路
貌似是一道贪心。
我们从后向前遍历 ,需要记下如下信息:
- :表示到第 位,我们当前可用的 的数量;
- :表示到第 位,我们当前可用的 的数量;
- :表示到第 位,我们已经组好的 数量;
- :表示到第 位,我们已经组好的 数量;
如何转移?我们可以分类讨论:
-
容易证明这样更优,因为答案总数不变,我们还会多出一个 。
-
-
答案即为 。
代码
CPP#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 条评论,欢迎与作者交流。
正在加载评论...