CEOI 1999 Parity game (并查集+离散化)

来源:互联网 发布:虚拟专用网络 编辑:程序博客网 时间:2024/05/19 17:56

Parity game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7174 Accepted: 2785

Description

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

1051 2 even3 4 odd5 6 even1 6 even7 10 odd

Sample Output

3

Source

CEOI 1999

[Submit]   [Go Back]   [Status]   [Discuss]


解析:我们用s[i]表示数列前 i 项得和,则有:

          若[a,b]区间内 1 的个数为偶数,则s[a-1]与s[b]的奇偶性相同;

          若[a,b]区间内 1 的个数为奇数,则s[a-1]与s[b]的奇偶性不同。

          我们用f[i]记录与s[i]奇偶性相同的,f[i+x]记录与s[i]奇偶性不同的,因为长度很大,但我们用到的也只是区间端点,最多也就m*2<=1e4,所以要采用离散化。

 

代码:

#include<cstdio>#include<cstring>#include<cctype>#include<algorithm>using namespace std;const int maxn=5e3;struct tnode{int x,y;             bool flag;}p[maxn+20];int q[maxn*2+20];int len,f[maxn*4+20];int getin(){  int ans=0;char tmp;  while(!isdigit(tmp=getchar()));  do ans=(ans<<3)+(ans<<1)+tmp-'0';  while(isdigit(tmp=getchar()));  return  ans;}int find(int x){  if(f[x]==x)return x;  return f[x]=find(f[x]);}int get(int x){  int l=1,r=q[0],mid;  while(l<=r)    {      mid=(l+r)/2;      if(x>q[mid])l=mid+1;      else r=mid-1;}  return l;}int main(){  int i,j,k,m,x1,x2,y1,y2;  char s[10];  k=getin(),m=getin();  for(i=1;i<=m;i++)    {       p[i].x=getin()-1,p[i].y=getin();       q[2*i-1]=p[i].x,q[2*i]=p[i].y;       scanf("%s",s);       p[i].flag=(s[0]=='e');}  k=m*2,q[0]=1,sort(q+1,q+1+k);  for(i=2;i<=k;i++)if(q[i]!=q[i-1])q[++q[0]]=q[i];  for(len=q[0]*2,i=0;i<len;i++)f[i]=i;    for(k=1;k<=m;k++)    {      i=get(p[k].x)-1,j=get(p[k].y)-1;      x1=find(i),x2=find((i+q[0])%len);      y1=find(j),y2=find((j+q[0])%len);  if(p[k].flag)        {          if(x1==y1)continue;          if(x2==y1){printf("%d\n",k-1);return 0;}          f[x1]=y1,f[x2]=y2;}  else    {      if(x1==y2)continue;      if(x1==y1){printf("%d\n",k-1);return 0;}      f[x1]=y2,f[x2]=y1;}}  printf("%d\n",m);  return 0;}



0 0
原创粉丝点击