AT_abc393_d [ABC393D] Swap to Gather 题解

· · 题解

AT_abc393_d [ABC393D] Swap to Gather 题解

题面翻译

英文版

题目描述

给定一个长度为 N01串S。保证 S 中至少包含一个1

你可以进行以下操作任意(可以为零)次:

找到使所有1连续的最少操作次数。

本题中,所有1连续当且仅当:

\exists l,r(l,r\in \N\land l,r\in \left[1,n\right]),\forall 1\le i\le n,S_i=\left[l\le i\le r\right]

输入格式

共两行,第一行一个整数 N,第二行一个字符串 S,意义如上。

输出格式

一行一个整数,表示答案。

数据保证

int cal(int pos){ int res = 0, cnt = 0; for(int i = 1; i <= n; ++ i) if(s[i] == '1') res += abs(pos + (cnt ++) - i); return res; }

signed main(){ cin >> n >> s; s = " " + s; for(int i = 1; i <= n; ++ i) if(s[i] == '1') pos.push_back(i - (tot ++)); cout << cal(pos[pos.size() >> 1]) << endl; }