NC51097 Parity game

题目链接

题目

题目描述

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[l1] ,即 sum[i] 表示 [1,i] 之间 1 的个数。若 [l,r] 是偶数,则 sum[r]sum[l1] 一定是同奇偶;否则就是异奇偶。

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

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

时间复杂度 O(qlogn)

空间复杂度 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;
}
posted @   空白菌  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示