题目大意:给你一个长度为$n$的$01$串,一次操作定义为:选取$3$个等距的元素,使其$0$变$1$,$1$变$0$,要求在$\Big\lfloor \dfrac n 3\Big\rfloor+12$次操作内变为全$0$。输出是否可行以及方案

题解:skip1978的博客讲的十分详细。发现给的操作次数很少,基本上一次操作要使得$3$个元素变成$0$

对于区间$[l,r]$,若$a_l=0$,这一位不用翻转,缩小到区间$[l+1,r]$,这样$a_l,a_{l+1},a_{l+2}$有$4$中可能:

  1. $1,1,1:$翻转$a_l,a_{l+1},a_{l+2}$就可以使得这三位变$0$
  2. $1,0,1:$翻转$a_l,a_{l+2},a_{l+4}$就可以使得这三位变$0$
  3. $1,0,0:$翻转$a_l,a_{l+3},a_{l+6}$就可以使得这三位变$0$
  4. $1,1,0:$可以按相同方法收缩右端点,直到右端点也出现这样的情况
    • 若$l,r$奇偶性相同,可以翻转$a_l,a_{\frac{l+r}{2}},a_r$和$a_{l+1},a_{\frac{l+r}{2}},a_{r-1}$使得这六位变成$0$
    • 若$l,r$奇偶性不同,可以翻转$a_l,a_{\frac{l+r-1}{2}},a_{r-1}$和$a_{l+1},a_{\frac{l+r+1}{2}},a_r$使得这六位变成$0$

然后发现若区间长度大于等于$8$,一定可以翻转成$0$,并且所有合法的翻转只有$12$次,可以暴力枚举每一个操作是否使用即可

卡点:找到答案后忘记退出

C++ Code:

#include <cstdio>
#include <vector>
#define maxn 100010 int n, s[maxn];
struct Step {
int a, b, c;
inline Step() {}
inline Step(int __a, int __b, int __c) {a = __a, b = __b, c = __c;}
};
std::vector<Step> Ans;
bool find_ans = false; inline void reverse(int a, int b, int c) {
s[a] ^= 1, s[b] ^= 1, s[c] ^= 1;
Ans.push_back(Step(a, b, c));
}
inline void reverse(int a, int c) {
int b = a + c >> 1;
reverse(a, b, c);
} std::vector<std::pair<int, int> > V;
int tmp[maxn];
#define rev(x) tmp[x.first] ^= 1, tmp[x.first + x.second >> 1] ^= 1, tmp[x.second] ^= 1
inline bool end(int l, int r) {
for (int i = l; i <= r; i++) if (tmp[i]) return false;
return true;
}
void calc(int l, int r) {
for (int i = l; i <= r - 2; i++) {
for (int j = i + 2; j <= r; j += 2) {
V.push_back(std::make_pair(i, j));
}
}
int sz = V.size(), U = 1 << sz;
for (int i = 0; i < U; i++) {
for (int i = l; i <= r; i++) tmp[i] = s[i];
for (int j = 0; j < sz; j++) if (i & 1 << j) rev(V[j]);
if (end(l, r)) {
find_ans = true;
for (int j = 0; j < sz; j++) if (i & 1 << j) reverse(V[j].first, V[j].second);
return ;
}
}
} #define checkl(x, a, b, c) (s[x] == a && s[x + 1] == b && s[x + 2] == c)
#define work(l, r, L, R) {reverse(l, r), solve(L, R); return ;}
#define checkr(x, a, b, c) (s[x] == a && s[x - 1] == b && s[x - 2] == c)
void solve(int l, int r) {
if (r - l + 1 <= 8) {
while (r - l + 1 < 8 && l > 1) l--;
while (r - l + 1 < 8 && r < n) r++;
calc(l, r);
return ;
}
if (!s[l]) {solve(l + 1, r); return ;}
if (!s[r]) {solve(l, r - 1); return ;}
if (checkl(l, 1, 1, 1)) work(l, l + 2, l + 3, r);
if (checkl(l, 1, 0, 1)) work(l, l + 4, l + 3, r);
if (checkl(l, 1, 0, 0)) work(l, l + 6, l + 3, r);
if (checkr(r, 1, 1, 1)) work(r - 2, r, l, r - 3);
if (checkr(r, 1, 0, 1)) work(r - 4, r, l, r - 3);
if (checkr(r, 1, 0, 0)) work(r - 6, r, l, r - 3);
if (r - l & 1) {
reverse(l, r - 1), reverse(l + 1, r);
solve(l + 3, r - 3);
} else {
reverse(l, r), reverse(l + 1, r - 1);
solve(l + 3, r - 3);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", s + i);
solve(1, n);
if (find_ans) {
puts("YES");
printf("%d\n", Ans.size());
for (std::vector<Step>::iterator it = Ans.begin(); it != Ans.end(); it++) {
printf("%d %d %d\n", it -> a, it -> b, it -> c);
}
} else puts("NO");
return 0;
}

  

[CF1031E]Triple Flips的更多相关文章

  1. Codeforces 1071 C - Triple Flips

    C - Triple Flips 思路: 小范围暴力 大范围递归构造 构造方法: solve(l, r) 表示使l 到 r 区间全变为0的方法 为了使反转次数小于等于n/3 + 12 我们只需要保证每 ...

  2. Codeforces1071C Triple Flips 【构造】【Four Russians】

    题目分析: 这种题目显然可以先考虑哪些无解.我们发现我们不考虑操作次数的时候,我们可以选择连续的三个进行异或操作. 这样我们总能使得一个序列转化为$000...000xy$的形式.换句话说,对于$00 ...

  3. Codeforces 1071C Triple Flips 构造

    原文链接 https://www.cnblogs.com/zhouzhendong/p/CF1071C.html 题目传送门 - CF1071C 题意 给定一个长度为 n 的 01 数列,限定你在 $ ...

  4. Technocup 2019 - Elimination Round 2

    http://codeforces.com/contest/1031 (如果感觉一道题对于自己是有难度的,不要后退,懂0%的时候敲一遍,边敲边想,懂30%的时候敲一遍,边敲边想,懂60%的时候敲一遍, ...

  5. Codeforces Round #517

    传送门 A. Cram Time 你有一本书,阅读第\(i\)页需要花费\(i\)的时间.你第一天有\(a\)的时间,第二天有\(b\)的时间,问你的总阅读页数的最大值. Input: 一行包含\(2 ...

  6. 山东省第七届ACM省赛------Triple Nim

    Triple Nim Time Limit: 2000MS Memory limit: 65536K 题目描述 Alice and Bob are always playing all kinds o ...

  7. hdu 3908 Triple(组合计数、容斥原理)

    Triple Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  8. Flips测试类(page43)

    测试用例:所用java类: StdOut,StdIn , Counter, StdRandom, public class Flips { public static void main(String ...

  9. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

随机推荐

  1. Java源码解析——集合框架(五)——HashMap源码分析

    HashMap源码分析 HashMap的底层实现是面试中问到最多的,其原理也更加复杂,涉及的知识也越多,在项目中的使用也最多.因此清晰分析出其底层源码对于深刻理解其实现有重要的意义,jdk1.8之后其 ...

  2. 微信小程序图片上传

    uploadImage : function (){ wx.chooseImage({ count: 9, // 默认9 sizeType: ['original', 'compressed'], / ...

  3. ruby 数据类型Range

    范围(Range)无处不在:a 到 z. 0 到 9.等等.Ruby 支持范围,并允许我们以不同的方式使用范围: 作为序列的范围 作为条件的范围 作为间隔的范围 作为序列的范围 (1..5) #==& ...

  4. vue2.0 Axios 的简单用法

    安装 使用 npm: $ npm install axios 使用 bower: $ bower install axios 使用 cdn: <script src="https:// ...

  5. 如何在Moodle中显示PPT课件

    Moodle中目前是不直接支持PPT的,所以需要曲线救国: 1.安装 iSpring Free 8版本,免费版,其实是一个PPT的插件,在PPT的工具栏中有显示. 2.打开PPT后,直接在该工具中进行 ...

  6. Java:Random函数及其种子的作用

    伪随机(preundorandom):通过算法产生的随机数都是伪随机!! 只有通过真实的随机事件产生的随机数才是真随机!!比如,通过机器的硬件噪声产生随机数.通过大气噪声产生随机数 Random生成的 ...

  7. Error: Error while compiling statement: FAILED: SemanticException Unable to determine if hdfs://hadoopNode2:8020/user/hive/warehouse/test is encrypted...

    1.发现问题: 在hive客户端或者beeline查询hive表时候报错: 根据报错信息查看,是在集群namenode做了HA之后,产生的hdfs路径不对的问题: 2.解决问题,修改hive元数据my ...

  8. 微信H5支付 在其他浏览器调用微信支付

    微信H5支付的相关资料不是很多.不过步骤上来说不是很复杂 比公众号支付简单很多. 先上官方文档吧 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapt ...

  9. cadence17.2的OrCAD启动找不到license的问题

    1. cadence17.2的OrCAD每次启动都说找不到license 2. 提示是找不到licence,看下系统变量,licence的路径是在的 3. 估计是读取licence的路径的服务未开启, ...

  10. I两种冒泡算法

    两种冒泡算法: 第一个循环,I 定位当前坐标,第二个循环 把 I 之后的每个数都与 I 比较(比 I 小的都去坐标I),第二个循环之后 坐标 I 为数组里最小的数值. 效率比较高的冒泡算法: stat ...