题目链接:http://poj.org/problem?id=1733
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.
Input
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).
Output
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.
Sample Input
10 5 1 2 even 3 4 odd 5 6 even 1 6 even 7 10 odd
Sample Output
3
题目翻译:
现在你和你的朋友正在玩一种游戏。 你的朋友写下一串0和1的序列,然后你选择其中一串子序列(如[3,5])并且问他这个序列是包含奇数个1还是偶数个1(和是奇数还是偶数)。 你可以问你的朋友任意个问题,而你的朋友会回答你的问题。 你的任务是猜出整个朋友的序列。
但是,你发现你的朋友告诉你的信息可能有误,所以你想写一个程序来指出他的错误。这个程序应该接受一系列你的问题和答案。程序的目标是找到第一个错误的答案。
输入
输入的第1行是序列长度L。(L ≤ 1000000000)
输入的第2行是问题的数量Q。(Q ≤ 5000)
接下来Q行包括所有的问题
每一行由两个整数 l,r 和描述奇偶的一个单词even或者odd组成
当单词为even时 [l,r]的和为偶数。
当单词为odd时 [l,r]的和为奇数。
输出
输出只有一行,输出一个整数X,表明该序列符合了前X个问题和答案。(不满足第X+1个问题)
如果序列符合所有问题的要求,应输出问题的数量Q。
这个题本身不难,很经典的种类并查集的做法,但是考虑到L的范围和Q的范围,我们不能用常规套路去写。
这里需要预先离散化处理一下,这样就不会超时了。
其他的操作和kuangbin带你飞专题它前面几题的做法差不多。
不过在路径压缩后的操作和合并的操作都是通过^来实现的。
至于为什么这样可以自己推一下。
#include<iostream>
#include<map>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=1e4+7;
int p[maxn],flag[maxn];//p为父节点,flag[i]表示i与根节点的相连区间的奇偶性,0为偶,1位奇
//巧妙运用了抑或的性质,0^1=1,1^0=1,1^1=0,0^0=0;
map<int,int> mp;//用STL中的map来进行hash离散化
int tot=0;
int insert(int x){//离散化,tot为编号
if(mp.find(x)==mp.end()) mp[x]=tot++;//map里面没有x,加一个,否则返回其编号
return mp[x];
}
int find(int x){
if(p[x]==-1) return x;
int temp=find(p[x]);//找到根节点
flag[x]^=flag[p[x]];//巧用抑或性质
return p[x]=temp;//路径压缩
}
int main(){
int n,q,x,y;
char opt[10];
while(~scanf("%d",&n)){
tot=0;
memset(p,-1,sizeof(p));
memset(flag,0,sizeof(flag));//初始化
scanf("%d",&q);
int end=q;
mp.clear();
for(int i=0;i<q;++i){
scanf("%d %d %s",&x,&y,opt);
if(x>y) swap(x,y);
if(end<q) continue;
x=insert(x-1);
y=insert(y);//离散化编号
int xx=find(x);
int yy=find(y);//找根节点
int temp=opt[0]=='o'?1:0;
if(xx==yy){//同一个根节点
if(flag[x]^flag[y]!=temp)//符合条件
end=i;
}
else{
p[yy]=xx;//合并
flag[yy]=temp^flag[x]^flag[y];
}
}
printf("%d\n",end);
}
return 0;
}
并查集+离散化(hash)&spm=1001.2101.3001.5002&articleId=99711499&d=1&t=3&u=06c19284313b4ba0bfd55d28441ee915)
842

被折叠的 条评论
为什么被折叠?



