题意:
思路:
观察发现相邻元素不同的结果只有两种,要么是101010101...要么是010101010,因此我们可以对结果分类讨论。直接模拟算出两种情况最少需要操作多少次,再取min即可。
需要注意的是,如果是奇数串,那么结果只有一种,数量多的一定要放两侧。
code:
#include <bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using PII = pair<i64, i64>;
const int inf = 0x3f3f3f3f;
const i64 INF = 0x3f3f3f3f3f3f3f3f;
#define Z cout << "\n"
#define lb lower_bound
#define ub upper_bound
#define D(x) cerr << #x << ": " << (x) << "\n"
#define DV(v) cerr<<#v<<": ";for(int i=0;i<(v).size();i++)cerr<<((v)[i])<<",";cerr<<"\n"
#if 1
#define int i64
#endif
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
string s;
cin >> s;
int n = s.size();
s = ' ' + s;
char c = '0';
if (n & 1) {
if (count(s.begin() + 1, s.end(), '0') < count(s.begin() + 1, s.end(), '1'))c = '1';
}
vector<int>p;
p.push_back(5959);
for (int i = 1; i <= n; i++) {
if (s[i] == c)p.push_back(i);
}
int sum1 = 0;
for (int i = 1; i < p.size(); i++) {
int z = 2 * i;
if (z <= n)sum1 += abs(z - p[i]);
else sum1 = inf;
}
int sum2 = 0;
for (int i = 1; i < p.size(); i++) {
int z = 2 * i - 1;
if (z <= n)sum2 += abs(z - p[i]);
else sum2 = inf;
}
cout << min(sum1, sum2);
return 0;
}
标签:周赛,结果,int,long,i64,牛客,游游,using
From: https://www.cnblogs.com/iscr/p/18280304