题目链接

题目

题目描述

Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers.

You suspect some of your friend's answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.

输入描述

The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1000000000. In the second line, there is one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either even or odd (the answer, i.e. the parity of the number of ones in the chosen subsequence, where even means an even number of ones and odd means an odd number).

输出描述

There is only one line in output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X+1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.

示例1

输入

10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd

输出

3

题解

知识点:并查集。

每次条件给出了一个区间 \([l,r]\) 的 \(01\) 串中 \(1\) 的个数是奇数还是偶数,区间很难处理,但我们可以转化为 \(sum[r]-sum[l-1]\) ,即 \(sum[i]\) 表示 \([1,i]\) 之间 \(1\) 的个数。若 \([l,r]\) 是偶数,则 \(sum[r]\) 和 \(sum[l-1]\) 一定是同奇偶;否则就是异奇偶。

如此就转换成端点的种类的相对关系了,而且题目只需要我们检查条件矛盾个数,那就自然而然使用扩展域并查集。

由于节点编号很大,采用遇到一个节点初始化一个节点的策略,并用 \(map\) 离散化记录。

时间复杂度 \(O(q \log n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>

using namespace std;

unordered_map<int, int> fa;

int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
} void merge(int x, int y) {
fa[find(x)] = find(y);
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, q;
cin >> n >> q;
int cnt;
n++;///范围从0开始到n
for (cnt = 0;cnt < q;cnt++) {
int x, y;
string op;
cin >> x >> y >> op;
if (!fa.count(x - 1)) fa[x - 1] = x - 1, fa[x - 1 + n] = x - 1 + n;
if (!fa.count(y)) fa[y] = y, fa[y + n] = y + n;
if (op == "even") {
if (find(x - 1) == find(y + n)) break;
else {
merge(x - 1, y);
merge(x - 1 + n, y + n);
}
}
else if (op == "odd") {
if (find(x - 1) == find(y)) break;
else {
merge(x - 1, y + n);
merge(x - 1 + n, y);
}
}
}
cout << cnt << '\n';
return 0;
}

NC51097 Parity game的更多相关文章

  1. Codeforces 549C. The Game Of Parity[博弈论]

    C. The Game Of Parity time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. 51nod1204 Parity

    如果sm[j]和sm[i]奇偶性相同,那么(i+1,j)个数为偶数如果奇偶性相同看成是朋友,不同的看成是敌人,那么就跟bzoj1370的做法差不多了. 如果奇偶性相同,就将x和y合并,x+n,y+n合 ...

  3. Codeforces Round #180 (Div. 2) C. Parity Game 数学

    C. Parity Game 题目连接: http://www.codeforces.com/contest/298/problem/C Description You are fishing wit ...

  4. POJ 1733 Parity game (并查集)

    Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6816   Accepted: 2636 Descr ...

  5. 1003. Parity(并查集)

    1003 看篇国家论文 <从<parity>的解法谈程序优化> 对于区间i,j 如果用sum[i],sum[j]来表示到i的1的个数的奇偶性 那么仔细想下 sum[i-1] 若 ...

  6. poj 1733 Parity game

    Parity game 题意:一个长度为N(N < 1e9)内的01串,之后有K(K <= 5000)组叙述,表示区间[l,r]之间1的个数为odd还是even:问在第一个叙述矛盾前说了几 ...

  7. UVA 11464 Even Parity(部分枚举 递推)

    Even Parity We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a on ...

  8. HDU-2700 Parity

    http://acm.hdu.edu.cn/showproblem.php?pid=2700 题目意思很重要:  //e:是要使字符串中1的个数变成偶数.o:是要使字符串中1的个数变成奇数 Parit ...

  9. Codeforces 549C The Game Of Parity(博弈)

    The Game Of Parity Solution: 这个题只需要分类讨论就可以解决. 先分别统计奇数和偶数的个数. 然后判断谁走最后一步,如果走最后一步时候同时有偶数和奇数,那么走最后一步的赢. ...

  10. HDOJ/HDU 2700 Parity(奇偶判断~)

    Problem Description A bit string has odd parity if the number of 1's is odd. A bit string has even p ...

随机推荐

  1. Zookeeper 的 ZAB 协议 以及 zookeeper 与 nacos 注册中心比对

    本文为博主原创,未经允许不得转载: 目录: 1. ZAB 协议 2. zookeer 节点状态 3. zookeeper 注册中心与 nacos 注册中心比较 4. zookeeper 配置注册中心 ...

  2. SV 数据类型-3

    联合数组 在内存中分配的空间可以是不连续的 联合数组方法 数组的方法 数组使用推荐 结构体 枚举类型 字符串变量类型String 操作符

  3. AHB 设计要点

    Hreadyout 每个slave回复hreadyout通过mux给到master master会将hreadyin信号给到每个slave hreadyout开始的时候都为1,如果是为0,会出现问题, ...

  4. 百度网盘(百度云)SVIP超级会员共享账号每日更新(2024.01.21)

    一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...

  5. [转帖]Linux下清理内存和Cache方法见下文:

    https://www.cnblogs.com/the-tops/p/8798630.html 暂时目前的环境处理方法比较简单: 在root用户下添加计划任务: */10 * * * * sync;e ...

  6. [转帖]Linux 监测服务心跳、服务重启策略

    文章目录 前言 背景 一.curl服务可用验证 二.服务探测脚本 三.配置系统定时任务 四.Linux特殊字符转义 总结 前言 请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i. 提示:以下是 ...

  7. [转帖]浅谈Redis大Key与热Key

    https://www.cnblogs.com/jelly12345/p/16424080.html 如何定义大 Key 和 热 Key 如何定义大 Key 如何定义热 Key 大 Key 和 热 K ...

  8. [百度贴吧]部分CPU的SPEC2006int 结果

    这些测试成绩基本上是本人自己测试的结果.下表中有来自spec官网的两个成绩,因为测试年份较早,系统环境和编译器都较老,测试成绩本人实测的还差,所以仅作为参考.部分测试启用了自动并行和附加的优化库,是为 ...

  9. frp 的简单使用

    在出差现场. 开着VPN 就没法用出差现场的网络, 想了想 好像 只能用 frp 来搞一下比较好 借了下同事的vps 进行相应的处理 进行简单的内容穿透工作. 1. 下载相关的文件. wget htt ...

  10. Windows 远程时提示CredSSP 加密数据库修正 问题的简单处理.

    最近在公司内部远程机器的时候更新了 windows上面远程部分服务器的时候出现异常如图示: 查了下 还是有比较简单的解决办法的 问题是需要在 自己的客户端机器上面进行设置 不需要修改服务器端的服务器. ...