首页 > 其他 > 详细

Codeforces Round #608 (Div. 2) B. Blocks

时间:2019-12-24 01:48:43      阅读:216      评论:0      收藏:0      [点我收藏+]

链接:

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;
}

Codeforces Round #608 (Div. 2) B. Blocks

原文:https://www.cnblogs.com/YDDDD/p/12089056.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!