CodeForces 1271B(思维)

B. Blocks
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are nn 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 3n3⋅n. If it is impossible to find such a sequence of operations, you need to report it.

Input

The first line contains one integer nn (2n2002≤n≤200) — the number of blocks.

The second line contains one string ss consisting of nn characters, each character is either "W" or "B". If the ii-th character is "W", then the ii-th block is white. If the ii-th character is "B", then the ii-th block is black.

Output

If it is impossible to make all the blocks having the same color, print 1−1.

Otherwise, print an integer kk (0k3n0≤k≤3⋅n) — the number of operations. Then print kk integers p1,p2,,pkp1,p2,…,pk (1pjn1)(1≤pj≤n−1), where pjpj is the position of the left block in the pair of blocks that should be affected by the jj-th operation.

If there are multiple answers, print any of them.

Examples
input
Copy
8
BWWWWWWB
output
Copy
3
6 2 4
input
Copy
4
BWBB
output
Copy
-1
input
Copy
5
WWWWW
output
Copy
0
input
Copy
3
BWB
output
Copy
2
2 1 
Note

In the first example, it is possible to make all blocks black in 33 operations. Start with changing blocks 66 and 77, so the sequence is "BWWWWBBB". Then change blocks 22 and 33, so the sequence is "BBBWWBB". And finally, change blocks 44 and 55, so all blocks are black.

It is impossible to make all colors equal in the second example.

All blocks are already white in the third example.

In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 22 and 33 (so the sequence is "BBW"), and then change blocks 11 and 22 (so all blocks are white).

题意:给你一个n还有长度为n的数组,该数组只由W和B组成,分别代表白和黑,每次你可以翻滚i且必须连带上i+1,使得其白变成黑,黑变成白。

求:为了使得所有的数组成为一个颜色,且操作次数不能大于3*n,输出多少次(不一定是最小解)k,接下来k个数表示操作了哪些位置的数。如果不存在,则输出-1.

思路:直接暴力即可,字符串a赋值给b,a从翻B第一个遍历到n-1.最后判断a[n],如果是W,则正确,不是则把b遍历1-n-1,翻W。存数队列即可

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector>
//const int maxn = 1e5+5;
#define ll long long
#define inf  0x3f3f3f3f
#define FOR(i,a,b) for( int i = a;i <= b;++i)
#define bug cout<<"--------------"<<endl
 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const int maxn = 100100;
using namespace std;
int n;
char a[210] ,aa[210];
int tail[200];
int main()
{
    //freopen("C:\ACM\input.txt","r",stdin);
    cin>>n;
    int b = 0,w = 0;
    for(int i = 1; i <= n; ++i)
    {
        cin>>a[i];
        aa[i] = a[i];
        if(a[i] == 'B') b++;
        else w++;
    }
    if(n % 2 == 0 && b % 2 == 1){
        printf("-1\n");
        return 0;
    }
    //queue<int>que;
    int k = 0 ;
    for(int i = 1; i <= n-1; ++i)
    {
        if(a[i] == 'B')
        {
            tail[++k] = i;
            //que.push(k);
            if(a[i+1] == 'W') a[i+1] = 'B';
            else a[i+1] = 'W';
        }
    }
    if(a[n] == 'B')
    {
        k = 0;
        for(int i = 1; i <= n-1; ++i)
        {
            if(aa[i] == 'W')
            {
                tail[++k] = i;
                //que.push(k);
                if(aa[i+1] == 'W') aa[i+1] = 'B';
                else aa[i+1] = 'W';
            }
        }        
    }
 
 
    printf("%d\n",k );
    for(int i = 1; i <= k; ++i)
    {
        printf("%d ",tail[i] );
    }
 
}

猜你喜欢

转载自www.cnblogs.com/jrfr/p/12061127.html
今日推荐