zoukankan      html  css  js  c++  java
  • 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.

    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
    ",k);
        }
        return 0;
    }
  • 相关阅读:
    解决CentOS java环境不生效的问题
    大数据学习 概念篇
    java实用技能 上传文件 等等
    ubuntu hadoop伪分布式部署
    list去重
    fastjson生成JSON字符串的时候出现$ref
    《Swift by Tutorials》学习笔记(第二章)
    《Swift by Tutorials》学习笔记(第一章)
    《JavaScript高级程序设计》学习笔记(第七章)
    《JavaScript高级程序设计》学习笔记(第五章)- 下
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3158719.html
Copyright © 2011-2022 走看看