Parity

Parity

Time limit: 2.0 second
Memory limit: 64 MB

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

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.

Output

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.

Sample

input output
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
-1
3

分析:带权并查集,注意离散化;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t,id,p[maxn],a[maxn],s[maxn],d[maxn],q[maxn],v[maxn];
char b[10];
int find(int x)
{
    if(x!=p[x])
    {
        int fa=find(p[x]);
        a[x]^=a[p[x]];
        p[x]=fa;
    }
    return p[x];
}
bool join(int x,int y,int z)
{
    int px=find(x),py=find(y);
    if(px!=py)
    {
        p[py]=px;
        a[py]=a[y]^a[x]^z;
        return false;
    }
    return true;
}
int main()
{
    int i,j;
    while(~scanf("%d",&t)&&t!=-1)
    {
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        j=1;
        rep(i,1,n)scanf("%d%d%s",&s[i],&d[i],b),q[j++]=s[i],q[j++]=d[i],v[i]=(b[0]==‘o‘);
        sort(q+1,q+j+1);
        int num=unique(q+1,q+j+1)-q-1;
        rep(i,1,n)
        {
            s[i]=lower_bound(q+1,q+num+1,s[i])-q-2;
            d[i]=lower_bound(q+1,q+num+1,d[i])-q-1;
        }
        rep(i,0,num)p[i]=i;
        rep(i,1,n)
        {
            if(join(s[i],d[i],v[i]))
            {
                if(a[s[i]]^a[d[i]]!=v[i])break;
            }
        }
        printf("%d\n",i-1);
    }
    //system("pause");
    return 0;
}
时间: 2024-10-07 10:18:15

Parity的相关文章

Parity Game CodeForces - 298C

You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a an

Codeforces 549C The Game Of Parity(博弈)

The Game Of Parity Solution: 这个题只需要分类讨论就可以解决. 先分别统计奇数和偶数的个数. 然后判断谁走最后一步,如果走最后一步时候同时有偶数和奇数,那么走最后一步的赢.如果没有呢,就看剩下的是奇数还是偶数,是偶数不用说,是奇数看剩奇数个还是偶数个. 针对这几种情况讨论一下就可以. #include <bits/stdc++.h> using namespace std; int n, k, o, e; int main() { cin >> n &g

poj 1733 Parity game

Parity game 题意:一个长度为N(N < 1e9)内的01串,之后有K(K <= 5000)组叙述,表示区间[l,r]之间1的个数为odd还是even:问在第一个叙述矛盾前说了几句话? Sample Input 10 N 5 K 1 2 even 3 4 odd 5 6 even 1 6 even (错误) 7 10 odd Sample Output 3 思路:对于判断正误的问题,一定要知道什么情况会导致错误.如样例:当前输入的区间[1,6]的端点均包含于前面输入的端点中时(包含并

Codeforces #180 div2 C Parity Game

// Codeforces #180 div2 C Parity Game // // 这道题的题目意思就不解释了 // // 题目有那么一点难(对于我而言),不多说啦 // // 解题思路: // // 首先如果a串和b串相等,不多说直接YES // 如果b串全是0,直接YES // 注意到a串有一个性质,1的个数不会超过本身的加1. // a有个1的上限设为x,b有个1的个数设为y,则如果x < y // 那么直接NO. // // 现在一般情况下,就是模拟啦,找到a的后缀和b的前缀一样的

[2016-03-18][POJ][1733][Parity game]

时间:2016-03-18 09:55:52 星期五 题目编号:[2016-03-18][POJ][1733][Parity game] 题目大意:给定若干的区间范围内的数字的和,问从哪句开始是错误的 分析: 带权并查集 区间长度高达1000000000显然不可能直接建立数组,但是发现询问只有5000次,也就是最多出现了5000*2个点,离散化就可以解决问题 relation[i] i为区间的左端点,fa[i]为区间的右端点,relation[i]维护(i,fa[i])这段区间的和的奇偶状态,0

51nod 1204 Parity(并查集应用)

1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串(例如其中的第3到第5个数字)问他,该子串中包含了奇数个还是偶数个1,他会回答你的问题,然后你可以继续提问......你怀疑朋友的答案可能有错,或说同他之前的答案相互矛盾,例如:1 - 2 奇数,3 - 4 奇数,那么可以确定1 - 4 一定是偶数,如果你的朋友回答是奇数,就产生了矛盾.给出所有你朋友的

Parity game

Parity game Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status 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

POJ 1733 Parity game (并查集)

Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6816   Accepted: 2636 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 conti

UVA 11464 Even Parity(枚举递推)

11464 - Even Parity Time limit: 3.000 seconds We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a one(1). The parity of a cell is the number of 1s surrounding that cell. A cell is surrounded by at most 4 cells (top,