zoukankan      html  css  js  c++  java
  • Codeforces Round #608 (Div. 2) B. Blocks

    链接:

    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;
    }
    
  • 相关阅读:
    大型网站数据库架构分析
    Mysql 存储引擎中InnoDB与Myisam的主要区别
    设计模式培训之一:为什么要用单例模式?
    架构师成长之路
    hdoj1257 最少拦截系统
    hdoj2571 命运
    hdoj1010 Temperor of the bone
    hdoj1175 连连看
    ny220 推桌子
    ny168 房间安排
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12089056.html
Copyright © 2011-2022 走看看