(补题 CF 1271B) Blocks

原题链接

Sample Input & Output

#1

8
BWWWWWWB
3
6 2 4

#2

4
BWBB
-1

#3

5
WWWWW
0

#4

3
BWB
2
2 1

解题思路

只要最后步骤数小于600就行,直接暴力:分别计算全部转成黑色以及白色各需要的步骤数输出小的一种结果即可。(当格子数为偶数且白格子或黑格子为奇数时不能全部变为同一颜色)

代码样例

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
using namespace std;

const int maxn = 1e6 + 5, INF = 0x3f3f3f3f;
char s[maxn];
int bans[maxn], wans[maxn], stn[maxn];
int n, b, w;

void init()
{
    for (int i = 0; i < n; i++)
        if (s[i] == 'W')
            stn[i] = 0;
        else
            stn[i] = 1;
}

void turnw()
{
    for (int i = 0; i < n - 1; i++)
    {
        if (stn[i] != 0)
        {
            stn[i] = 1 - stn[i];
            stn[i + 1] = 1 - stn[i + 1];
            wans[w++] = i + 1;
        }
    }
}

void turnb()
{
    for (int i = 0; i < n - 1; i++)
    {
        if (stn[i] != 1)
        {
            stn[i] = 1 - stn[i];
            stn[i + 1] = 1 - stn[i + 1];
            bans[b++] = i + 1;
        }
    }
}

int main()
{
    int wcnt = 0, bcnt = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> s[i];
        if (s[i] == 'W')
            wcnt++;
        else
            bcnt++;
    }
    // printf("W=%d;B=%d\n",wcnt,bcnt);
    if (bcnt == 0 && wcnt == 0)
        cout << "0" << endl;
    if (n % 2 == 0 && wcnt % 2 != 0)
        cout << "-1" << endl;
    else
    {
        bool flag = false;
        b = 0, w = 0;
        init();
        turnb();
        if (stn[n - 1] != 1)
            b = INF;
        init();
        turnw();
        if (stn[n - 1] != 0)
            w = INF;
        if (w < b)
        {
            cout << w << endl;
            for (int i = 0; i < w; i++)
            {
                if (i == 0)
                    cout << wans[i];
                else
                    cout << " " << wans[i];
            }
        }
        else
        {
            cout << b << endl;
            for (int i = 0; i < b; i++)
            {
                if (i == 0)
                    cout << bans[i];
                else
                    cout << " " << bans[i];
            }
        }
        cout << endl;
    }
    return 0;
}
posted @ 2020-01-11 10:13  cafu-chino  阅读(200)  评论(0编辑  收藏  举报