ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[GYM101173K] CERC 16 K.Key Knocking

2021-10-27 00:02:18  阅读:191  来源: 互联网

标签:&& fir cnt GYM101173K CERC Knocking sec str ans


题目大意:定义一个串的价值为相邻不同的个数 + 1 +1 +1,给定一个长度为 3 n 3n 3n的字符串,要求通过最多 n n n次操作,使得串的价值至少为 2 n 2n 2n。操作反转两个相邻元素:将 [ x ] , [ x + 1 ] [x],[x+1] [x],[x+1]两个元素的值反转, 0 → 1 , 1 → 0 0 \rightarrow 1,1 \rightarrow 0 0→1,1→0.

思路:每 3 3 3个字符执行一次分割,只要能把每组分成 2 2 2段而且和前面的不相连,最后段数一定不小于 2 n 2n 2n。因此首先判断前一个字符,然后根据前一个字符枚举当前 3 3 3个字符的状态,共 16 16 16种状态,去除已经分好不需要操作的状态共 8 8 8种状态。

  1. bef = 0
    • 000
    • 001
    • 011
    • 111
  2. bef = 1
    • 000
    • 100
    • 110
    • 111

分别计数判断即可。

#include <bits/stdc++.h>
using namespace std;

int ans[5000010], cnt = 0;

inline void solve(){
    string str; cin >> str;
    int n = str.length();
    str = ' ' + str;
    for(int i = 1; i + 2 <= n; i += 3){
        int bef = str[i - 1] - '0', fir = str[i] - '0', sec = str[i + 1] - '0', thd = str[i + 2] - '0';
        if(!bef){
            if(!fir && !sec && !thd) ans[++cnt] = i;
            else if(!fir && !sec && thd) ans[++cnt] = i + 1, str[i + 2] = '0';
            else if(!fir && sec && thd) ans[++cnt] = i;
            else if(fir && sec && thd) ans[++cnt] = i + 1, str[i + 2] = '0';
        }
        else{
            if(!fir && !sec && !thd) ans[++cnt] = i + 1, str[i + 2] = '1';
            else if(fir && !sec && !thd) ans[++cnt] = i;
            else if(fir && sec && !thd) ans[++cnt] = i + 1, str[i + 2] = '1';
            else if(fir && sec && thd) ans[++cnt] = i;
        }
    }
    cout << cnt << endl;
    for(int i = 1; i <= cnt; i++) cout << ans[i] << (i == cnt ? '\n' : ' ');
}

signed main(){
    solve();
}

标签:&&,fir,cnt,GYM101173K,CERC,Knocking,sec,str,ans
来源: https://blog.csdn.net/yanweiqi1754989931/article/details/120983968

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有