CF1809D Binary String Sorting 题解
贪心。由于每次操作的代价都很大,所以需要优先减少操作次数,然后尽量多使用交换操作。
易得交换操作最多只会发生一次,因为每次交换操作只能消除一个逆序对,当存在两个或多个逆序对时,可以通过删除操作来减少更多的逆序对,减少操作次数。当只存在一个逆序对时,自然也只能交换一次。
然后,由于排序结束后 \(0\) 和 \(1\) 之间必然有一条分界线,左边是 \(0\),右边是 \(1\)。我们考虑枚举这条分界线,在分界线左边的 \(1\) 需要删除,在分界线右边的 \(0\) 需要删除。在分界线的同侧进行交换操作是无意义的,因为这并不会减少左边的 \(1\) 或右边的 \(0\)。
如果一次交换操作能恰好把左边的 \(1\) 换到右边,右边的 \(0\) 换到左边,那么可以优先执行这个交换操作,我们发现这种情况只会在分界线左右第一个元素发生,正好印证了上文交换操作最多只会发生一次。所以,当分界线左边第一个元素为 \(1\),右边第 \(1\) 个元素为 \(0\) 时,需要进行一次交换操作,从而减少两次修改操作。
如果分界线不满足左边第一个元素为 \(1\),右边第 \(1\) 个元素为 \(0\) 时,其实也不用处理分界线左右第一个元素。因为无论是 \(00\) 还是 \(11\) 还是 \(01\),其实都是符合条件的数对,并不会影响结果。
#include <bits/stdc++.h>
using namespace std;
long long t,n,sf=1e12,df=1e12+1;
char str[400000];
int main()
{
scanf("%lld",&t);
while(t--)
{
long long s0=0,s1=0,z=0,y=0,ans=1e18;
scanf("%s",str+1);
n=strlen(str+1);
for(int i=2;i<=n;i++)
if(str[i]=='0')y++;
for(int i=1;i<=n-1;i++)
{
if(str[i+1]=='0')y--;
if(str[i-1]=='1')z++;
if(str[i]=='1'&&str[i+1]=='0')ans=min(ans,(z+y)*df+sf);
else ans=min(ans,(z+y)*df);
}
if(ans==1e18)printf("0\n");
else printf("%lld\n",ans);
}
return 0;
}
CF1809D Binary String Sorting 题解的更多相关文章
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Codeforces 862D. Mahmoud and Ehab and the binary string (二分)
题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...
- Codeforces1107E Vasya and Binary String 记忆化dp
Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...
- Codeforces Round #598 (Div. 3) D. Binary String Minimizing 贪心
D. Binary String Minimizing You are given a binary string of length n (i. e. a string consisting of ...
- Educational Codeforces Round 94 (Rated for Div. 2) String Similarity、RPG Protagonist、Binary String Reconstruction、Zigzags 思维
题目链接:String Similarity 题意: 首先题目定义了两个串的相似(串的构成是0.1),如果两个串存在对于一个下标k,它们的值一样,那么这两个串就相似 然后题目给你一个长度为2n-1的串 ...
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- encode_json 会对给定的Perl的数据结构转换为一个UTF-8 encoded, binary string.
use JSON qw/encode_json decode_json/ ; use Encode; my $data = [ { 'name' => 'Ken' , 'age' => 1 ...
- perl encode_json 会产生 UTF-8 (binary) string decode_json 需要一个 UTF-8 (binary) string
encode_json $json_text = encode_json $perl_scalar Converts the given Perl data structure to a UTF-8 ...
随机推荐
- Python科学计算系列3—多项式操作
1.因式分解 例1:分解下列多项式 代码如下: from sympy import symbols, factor x, y = symbols('x y') f = 3 * x ** 4 - 2 * ...
- Grafana将弃用AngularJS-我们该如何迁移
AngularJS 弃用时间线 AngularJS 支持已在 Grafana 9 中正式弃用.在 2024 年 5 月发布的 Grafana 11 中,所有 Grafana Cloud 和自托管安装默 ...
- php-ffmpeg保存为.mp4格式时报错
原文: php-ffmpeg保存为.mp4格式时报错:FFMpeg\Exception\RuntimeException: Encoding failed in - 搜栈网 (seekstack.c ...
- 关于:js使用$.parseJSON字符串转json对象报错Uncaught SyntaxError- Unexpected token o in JSON at position 1
今天使用js使用$.parseJSON字符串转json对象报错Uncaught SyntaxError- Unexpected token o in JSON at position 1,一直找不到原 ...
- JWT Token解析
参照:c#中token的使用方法实例_C#教程_脚本之家 (jb51.net) (7条消息) JWT 算法_み旋律的博客-CSDN博客_jwt算法
- springboot分页查询并行优化实践
--基于异步优化与 MyBatis-Plus 分页插件思想的实践 适用场景 数据量较大的单表分页查询 较复杂的多表关联查询,包含group by等无法进行count优化较耗时的分页查询 技术栈 核心框 ...
- CentOS 7怎么开放端口
转自:https://www.jb51.net/os/Ubuntu/617627.html 以开放8080端口为例,其他类似 centos7已经开始使用firewall作为防火墙,而不是iptable ...
- servlet 转发与重定向
目录 转发 重定向 重定向与转发本质都是跳转到新的URL 重定向与转发的本质区别在于:转发是一个服务端的行为,而重定向是一个浏览器的行为. 下面是图解: 转发 转发的作用在服务器端,将请求发送给服务器 ...
- 工具 | burpgpt
0x00 简介 burpgpt是一款利用AI来检测安全漏洞的burpsuite插件.Burp Suite GPT扩展集成了OpenAI的GPT,以执行额外的被动扫描以发现高度定制的漏洞,并支持运行任何 ...
- jsp技术之“如何在jsp中判断属性为空”
一.判断对象列表为空不显示某段代码 <%-- 展开子属性 --%> <c:if test="${not empty product.variations}"> ...