Equalize
You are given two binary strings aa and bb of the same length. You can perform the following two operations on the string aa:
- Swap any two bits at indices ii and jj respectively (1≤i,j≤n1≤i,j≤n), the cost of this operation is |i−j||i−j|, that is, the absolute difference between ii and jj.
- Select any arbitrary index ii (1≤i≤n1≤i≤n) and flip (change 00 to 11 or 11 to 00) the bit at this index. The cost of this operation is 11.
Find the minimum cost to make the string aa equal to bb. It is not allowed to modify string bb.
The first line contains a single integer nn (1≤n≤1061≤n≤106) — the length of the strings aa and bb.
The second and third lines contain strings aa and bb respectively.
Both strings aa and bb have length nn and contain only '0' and '1'.
Output the minimum cost to make the string aa equal to bb.
3
100
001
2
4
0101
0011
1
In the first example, one of the optimal solutions is to flip index 11 and index 33, the string aa changes in the following way: "100" →→ "000" →→ "001". The cost is 1+1=21+1=2.
The other optimal solution is to swap bits and indices 11 and 33, the string aa changes then "100" →→ "001", the cost is also |1−3|=2|1−3|=2.
In the second example, the optimal solution is to swap bits at indices 22 and 33, the string aa changes as "0101" →→ "0011". The cost is |2−3|=1|2−3|=1.
还是太直线思维了,老是想着用哪个减哪个,就没想到让答案一直增加:(
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <xfunctional>
#define ll long long
#define mod 998244353
using namespace std;
int dir[][] = { {,},{,-},{-,},{,} };
const int maxn = 1e5 + ;
const long long inf = 0x7f7f7f7f7f7f7f7f; int main()
{
int n,res=;
cin >> n;
string a, b;
cin >> a >> b;
int ans=;
for (int i = ; i < a.size(); i++)
{
if (a[i] != b[i])
{
if (i + < a.size() && a[i + ] != b[i + ] && a[i]!=a[i+])
{
res++;
i++;
}
else
{
res++;
}
}
}
cout << res;
}
Equalize的更多相关文章
- D. Equalize Them All Codeforces Round #550 (Div. 3)
D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 999D Equalize the Remainders (set使用)
题目连接:Equalize the Remainders 题意:n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次? 每次操作可以把某个数字+1.输出最 ...
- 1037C_ Equalize(字符串)
modify 改变 C. Equalize time limit per test 1 second memory limit per test 256 megabytes input standar ...
- D. Equalize the Remainders (set的基本操作)
D. Equalize the Remainders time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- (原创)Codeforces Round #550 (Div. 3) D. Equalize Them All
D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces1144D(D题)Equalize Them All
D. Equalize Them All You are given an array aa consisting of nn integers. You can perform the follow ...
- CF1234A Equalize Prices
洛谷 CF1234A Equalize Prices Again 洛谷传送门 题目描述 You are both a shop keeper and a shop assistant at a sma ...
- D. Equalize the Remainders set的使用+思维
D. Equalize the Remainders set的学习::https://blog.csdn.net/byn12345/article/details/79523516 注意set的end ...
- D. Equalize the Remainders 解析(思維)
Codeforce 999 D. Equalize the Remainders 解析(思維) 今天我們來看看CF999D 題目連結 題目 略,請直接看原題 前言 感覺要搞個類似\(stack\)的東 ...
- Codeforces 1037C Equalize
原题 题目大意: 给你两个长度都为\(n\)的的\(01\)串\(a,b\),现在你可以对\(a\)串进行如下两种操作: 1.交换位置\(i\)和位置\(j\),代价为\(|i-j|\) 2.反转位置 ...
随机推荐
- Swaps and Inversions HDU - 6318 树状数组+离散化
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...
- Sass和Scss
Sass:https://www.sass.hk/ Sass是什么 Sass 是一款强化 CSS 的辅助工具,它在 CSS 语法的基础上增加了变量 (variables).嵌套 (nested rul ...
- PP: Taking the human out of the loop: A review of bayesian optimization
Problem: Design problem parameters consist of the search space of your model. Scientists design expe ...
- AduSkin - UI
追求极致,永臻完美 A Beautiful WPF Control UI 一款简单漂亮的WPF UI,融合部分开源框架的组件,为个人定制的UI,可供学者参考. Nuget 搜索"AduSki ...
- 使用Xpath爬取酷狗TOP500的歌曲信息
使用xpath爬取酷狗TOP500的歌曲信息, 将排名.歌手名.歌曲名.歌曲时长,提取的结果以文件形式保存下来.参考网址:http://www.kugou.com/yy/rank/home/1-888 ...
- phpstudy+phpstorm 浏览器没有解析php文件,直接显示源码
用phpstorm编辑完项目,右键浏览器预览时页面报错:502 Bad Gateway PhpStorm 2019.1 此时默认打开的地址是:localhost:63339/开头的一长串…… 查资料说 ...
- Yeoman+Bower+gulp web前端自动化工作流程(初级教程)
Yeoman包括了三个部分yo(脚手架工具).grunt/gulp(构建工具).bower(包管理器).听说gulp更容易上手,所以我就没用grunt而选的gulp 什么是开发流程? 在我看来一个完整 ...
- 将Python模块转变为命令行工具
问:如何输入命令行就能执行python代码呢? 答:要将python模块转变为命令行工具只用在 setup.py 文件中添加参数entry_points 例如: entry_points={ 'con ...
- windows-problem :电脑上网的无线图标带有黄色星号,但不影响正常上网!
电脑可以上网,但是无线图标带有黄色星号如何解决? 进入“网络和共享中心”,点击“更改适配器设置”,看看是不是有两个以上的“本地连接”,只有一个有用,其他的删除即可.
- cdn第三方前端依赖架包共享地址
https://cdnjs.com/ 可在此网站查找你需要的稳定第三方前端依赖架包