Parity Game

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12853   Accepted: 4957

题目链接http://poj.org/problem?id=1733

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:

10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd

Sample Output:

3

题意:

给出一个01串,然后给出一些信息,是关于[x,y]区间中有奇数个1还是偶数个1,求出X,使得1-X条信息都可以满足。

题解:
如果不考虑数据范围,很明显就是带权并查集,用一个数组v来维护当前结点与其父亲结点的关系:v[x]=1 <==>(x,f[x]]有奇数个1;否则v[x]=0就是偶数个个1。

再来看一下数据范围,区间很大, 如果直接开数组那么肯定存不下的;再来看下信息个数,只有5000个,也就是最多也只有10000个区间端点。

所以我们考虑离散化(区间端点的具体值我们并不关心,主要是相对大小),把原来范围很大的区间变小。

我用的是map。代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std; typedef long long ll;
const int N = ;
int f[N],v[N];
map<ll,int> mp;
ll n,x,y;
int q,tot; int find(int x){
if(x==f[x]) return x;
int tmp = f[x];
f[x]=find(f[x]);
v[x]+=v[tmp];
if(v[x]>=) v[x]-=;
return f[x];
} int main(){
cin>>n>>q;
int flag = ,ans = q;
for(int i=;i<=;i++) f[i]=i,v[i]=;
for(int i=;i<=q;i++){
char s[];
scanf("%lld%lld %s",&x,&y,s);
if(flag) continue ;
if(!mp[x]) mp[x]=++tot;
if(!mp[y+])mp[y+]=++tot;
int fx=find(mp[x]),fy=find(mp[y+]),pd;
if(s[]=='e') pd=;else pd=;
if(fx==fy){
int now = (v[mp[x]]+v[mp[y+]])%;
if((pd && !now) || (!pd &&now)) continue;
else{
ans=i-;
flag=;
continue;
}
}
f[fx]=fy;
if(pd) v[fx]=(v[mp[x]]+v[mp[y+]])%;
else if(!pd) v[fx]=(v[mp[x]]+v[mp[y+]]+)%;
}
printf("%d",ans);
return ;
}

最新文章

  1. Java JDBCI批量插入数据
  2. 美帝的emal to message gateway
  3. NSDate 格式化 NSDate to NSString
  4. 浅析I/O模型及其设计模式
  5. WampServer phpadmin apache You don&#39;t have permission to access
  6. aspnetpager的2种分页方法
  7. #ifdef __OBJC__ 宏定义的作用
  8. [CLR via C#]5.2 引用类型和值类型
  9. js数组对象方法
  10. 学习Sass笔记之概念篇
  11. Hadoop 新生报道(二) hadoop2.6.0 集群系统版本安装和启动配置
  12. Hosts文件实际应用 配置内部服务器提高访问效率和速度
  13. Zabbix实战-简易教程--动作(Actions)--触发器
  14. elasticsearch开机启动脚本
  15. 100-days: twenty-eight
  16. SAP MM MM17里不能修改物料主数据&#39;Purchasing Value Key&#39;字段值?
  17. python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查
  18. Codechef EDGEST 树套树 树状数组 线段树 LCA 卡常
  19. 南阳219----An problem about date
  20. 模仿select下拉列表

热门文章

  1. python 排列组合
  2. 嵌入式框架Zorb Framework搭建二:环形缓冲区的实现
  3. Python3 函数return
  4. Kubernetes-Envoy(一种全新的Ingress实现方式)
  5. 【Java】关于Spring框架的总结 (二)
  6. KMP python实现
  7. [网站日志]当Memcached缓存服务挂掉时性能监视器中的表现
  8. Windows Server2003下安装IIS服务脑图
  9. ardupilot_gazebo仿真(四)
  10. iOS-技术细节整理