poj 1733 Parity game
Parity game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5287 | Accepted: 2074 |
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.
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
Source
#include<stdio.h> #include<iostream> #include<cstdlib> #include<algorithm> using namespace std; struct st { int l; int r; }f[5002]; int vis[5003]; int a[10003]; int aa[10003]; int father[10003]; int rank[10003]; int flag; int EF(int l,int r,int num) { int mid=(l+r)/2; while(l<r) { if(a[mid]<num) l=mid+1; else if(a[mid]>num) r=mid-1; else if(a[mid]==num) return mid; mid=(l+r)/2; } return mid; } void makeini(int n) { int i; for(i=0;i<=n;i++) { father[i]=i; rank[i]=0; } } int find(int x) { int t; if(x==father[x]) return x; t=find(father[x]); rank[x]=(rank[x]+rank[father[x]])%2; father[x]=t; return t; } void Union(int x,int y,int num) { int x1,y1,tmp; x1=find(x-1); y1=find(y); if(x1==y1) { if((rank[y]+rank[x-1])%2!=num) flag=1; } else { if(x1<y1) { father[y1]=x1; rank[y1]=(num+rank[x-1]+rank[y])%2; } else if(y1<x1) { father[x1]=y1; rank[x1]=(num+rank[y]+rank[x-1])%2; } } } int main() { int i,j,k,n,m,x,y,len,num,alen; char c[10]; scanf("%d",&n); { scanf("%d",&m); len=0;alen=0; flag=0; for(i=1;i<=m;i++) { scanf("%d%d%s",&f[i].l,&f[i].r,c); aa[++alen]=f[i].l; aa[++alen]=f[i].r; if(c[0]=='e') vis[i]=0; else if(c[0]=='o')vis[i]=1; } sort(aa+1,aa+1+alen); for(i=1;i<=alen;i++) { if(aa[i]!=aa[i+1]) a[++len]=aa[i]; } makeini(len); for(i=1,k=m;i<=m;i++) { x=EF(1,len,f[i].l); y=EF(1,len,f[i].r); num=vis[i]; Union(x,y,num); if(flag==1) { k=i-1; break; } } printf("%d\n",k); } return 0; }
分类:
ACM---并查集及扩展
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)