CF1913B Swap and Delete 题解
1.CF1909C Heavy Intervals 题解2.CF1913C Game with Multiset 题解3.P9750 [CSP-J 2023] 一元二次方程 题解4.CF1891B Deja Vu 题解5.CF1907B YetnotherrokenKeoard 题解
6.CF1913B Swap and Delete 题解
7.CF1121A Technogoblet of Fire 题解8.CF1162B Double Matrix 题解9.CF1184B1 The Doctor Meets Vader (Easy) 题解10.CF133B Unary 题解11.P8661 [蓝桥杯 2018 省 B] 日志统计 题解12.P3891 [GDOI2014] 采集资源 题解13.CF1804C Pull Your Luck 题解14.CF1815A Ian and Array Sorting 题解15.CF1681C Double Sort 题解16.P8791 [蓝桥杯 2022 国 AC] 内存空间 题解17.CF1250A Berstagram 题解18.CF358B Dima and Text Messages 题解19.CF1737C Ela and Crickets 题解20.CF1817A Almost Increasing Subsequence 题解21.CF1670B Dorms War 题解22.CF121A Lucky Sum 题解23.CF875B Sorting the Coins 题解24.CF158C Cd and pwd commands 题解25.CF1040B Shashlik Cooking 题解26.CF958F1 Lightsabers (easy) 题解翻译
给定一个字符串
- 删除一个字符。(花费一枚金币)
- 交换某两个字符的位置。(不花费金币)
假设经过若干次操作后得到的字符串为
自然,空串一定是好的。
问最小花费。
多测。
思路
记录每个 0
和 1
的数量,第二次遍历时直接遍历 1
则填 0
,为 0
则填 1
。当无法继续填时直接输出剩余的 0
和 1
总字符数。因为此时后面的不管怎么填一定会有相同,只能全删。
代码
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#define ll long long
#define fr(i , a , b) for(ll i = a ; i <= b ; ++i)
#define fo(i , a , b) for(ll i = a ; i >= b ; --i)
using namespace std;
ll T;
char s[200005];
signed main()
{
// freopen("in.in" , "r" , stdin);
// freopen("out.out" , "w" , stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> T;
while(T--)
{
cin >> s + 1;
ll len = strlen(s + 1) , num0 = 0 , num1 = 0;
fr(i , 1 , len)
{
if(s[i] == '0')
{
num0++;
}
else
{
num1++;
}
}
fr(i , 1 , len)
{
if(s[i] == '0')
{
if(num1 == 0)
{
break;
}
num1--;
}
else
{
if(num0 == 0)
{
break;
}
num0--;
}
}
cout << num1 + num0 << '\n';
}
return 0;
}
- 一个好的指挥官不会背叛他的军队,无论代价是什么。
- 一个更好的指挥官知道当他想背叛他的军队时,什么时候应该伪装。
- 一个最好的指挥官精明地确立自己的绝对领导,并引领着人民走向更好的未来。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下