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.反转位置 ...
随机推荐
- VAR向量自回归模型学习笔记2
向量自回归模型 今天的你 和昨天的你 和前天的你,是否具有相关性. 1. 定义 向量自回归(VAR,Vector Auto regression)分析联合内生变量间的动态关系 联合:n个变量间的相互影 ...
- 我的翻译--一个针对TP-Link调试协议(TDDP)漏洞挖掘的故事
前言 我写这篇文章原本是为了简化WiFi渗透测试研究工作.我们想使用去年由Core Security发布的WIWO,它可以在计算机网络接口和WiFi路由器之间建立一个透明的通道. 研究的第一步,就是选 ...
- BZOJ3473&&BZOJ3277串
BZOJ3473&&BZOJ3277串 题面 自己找去 HINT 对于所有串建立一个广义后缀自动机,对于每一个节点开一个set表示这个节点接受的子串在哪些串里出现过,然后在parent ...
- 2019-08-21 纪中NOIP模拟A组
T1 [JZOJ6315] 数字 题目描述
- Python中numpy模块的简单使用
# encoding:utf-8 import numpy as np data1 = np.array([1, 2, 3, 4, 5]) print(data1) data2 = np.array( ...
- Xlrd模块读取Excel文件数据
Xlrd模块使用 excel文件样例:
- JDBC——DriverManager驱动管理对象
功能 1.注册驱动 注册驱动:告诉程序使用哪个驱动jar包 写代码使用:Class.forName("com.mysql.jdbc.Driver"); 查看源码 mysql-con ...
- 四、CentOS 7安装Oracle JDK
CentOS 7安装Oracle JDK,查看Linux是否自带的JDK,如有openJDK,则卸载 CentOS7.1 JDK安装 1.卸载自带OPENJDK 用 java -version ...
- php中构建树状图
/** * 指定根层级的树状图 * @param array $list 初始数组 * @param int $root 最上级一条数据的id * @param string $pk 每一条数据的id ...
- detach() 使用和.detach()和.data的区别 、cpu()函数的作用
detach() 使用和.detach()和.data的区别 .cpu()函数的作用 待办 detach使用 https://blog.csdn.net/qq_27825451/article/det ...