链接:
https://codeforces.com/contest/1271/problem/B
题意:
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3⋅n. If it is impossible to find such a sequence of operations, you need to report it.
思路:
只要有偶数颜色就能修改完,
把偶数修改为奇数即可,都为偶数随便。
代码:
#include<bits/stdc++.h>
using namespace std;
char s[210];
int n;
int main()
{
cin >> n >> s;
int b = 0, w = 0;
for (int i = 0;i < n;i++)
{
if (s[i] == 'B')
b++;
else
w++;
}
if (b&1 && w&1)
{
puts("-1");
return 0;
}
if (b == 0 || w == 0)
{
puts("0");
return 0;
}
vector<int> ans;
int op = b&1 ? 1 : 2;
for (int i = n-1;i > 0;i--)
{
if (op == 1 && s[i] == 'W')
s[i] = 'B', s[i-1] = (s[i-1] == 'B' ? 'W' : 'B'), ans.push_back(i);
else if (op == 2 && s[i] == 'B')
s[i] = 'W', s[i-1] = (s[i-1] == 'W' ? 'B' : 'W'), ans.push_back(i);
}
cout << (int)ans.size() << endl;
for (auto v: ans)
cout << v << ' ' ;
cout << endl;
return 0;
}