@xiaoziyao
2020-03-26T20:40:05.000000Z
字数 3157
阅读 1486
Timus
题目简述:5/4/4/5/离散化+并查集
传送门:1003. Parity
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 contains a series of tests. The first line of each test contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 109. In the second line, there is one non-negative integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5 000. 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). The input is ended with a line containing −1.
Each line of 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.
input:
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
-1
output:
3
题意:(有多组数据)给定一个数(),表示范围和()次操作,每次操作:l r even/odd
表示数字到之和为奇数/偶数,但是给定的操作中可能有错误的,找到第一个错误操作的位置,输出这个操作的编号减,否则输出
这道题思维比较妙,首先要将和转换为前缀和之差,这一点很显然
其次,可以把前缀和之差变成并查集维护的两个集合,集合相同的点奇偶性相同,集合不同的点奇偶性不同,然后就是套路了。
注意,这道题要离散化,因为范围()太大了,但询问最多会产生个点,所以是显然的离散化
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100005;
int i,j,k,m,n,nums,tot,flg;
int a[maxn][3],b[maxn],f[maxn],d[maxn];
string s;
struct number{
int val,pos,mrk;
}num[maxn];
inline int cmp(number a,number b){
return a.val<b.val;
}
int find(int x){
if(f[x]==x)
return x;
int res=find(f[x]);
d[x]^=d[f[x]];
f[x]=res;
return res;
}
int merge(int x,int y,int z){
int a=find(x),b=find(y);
if(a==b)
return d[x]^d[y]==z;
f[a]=b;
d[a]=d[x]^d[y]^z;
return 1;
}
int main(){
while(1){
scanf("%d",&n);
if(n==-1)
break;
scanf("%d",&m);
nums=0;
for(i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
cin>>s;
x--;
if(s=="even")
b[i]=0;
else b[i]=1;
num[++nums].val=x,num[nums].pos=i,num[nums].mrk=1;
num[++nums].val=y,num[nums].pos=i,num[nums].mrk=2;
}
sort(num+1,num+1+nums,cmp);
tot=1,a[num[1].pos][num[1].mrk]=tot;
for(i=2;i<=nums;i++){
if(num[i].val!=num[i-1].val)
tot++;
a[num[i].pos][num[i].mrk]=tot;
}
for(i=1;i<=tot;i++)
f[i]=i,d[i]=0;
flg=0;
for(i=1;i<=m;i++)
if(merge(a[i][1],a[i][2],b[i])==0){
printf("%d\n",i-1);
flg=1;
break;
}
if(flg==0)
printf("%d\n",m);
}
return 0;
}