zoukankan      html  css  js  c++  java
  • poj 1733

    Parity game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11042   Accepted: 4253

    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

    第一个数有1000000000这么大,所以要将数据离散化后才能去进行操作。
     1 #include<cstdio>
     2 #include<map>
     3 #include<set>
     4 #include<cstring>
     5 using namespace std;
     6 const int maxn=10006;
     7 struct node{
     8     int a,b;
     9     int w;
    10 }cv[maxn];
    11 int fa[maxn],val[maxn];
    12 set<int> seek;
    13 map<int,int> seek2;
    14 
    15 void init()
    16 {
    17     seek.clear();
    18     seek2.clear();
    19     for(int i=0;i<maxn;i++){
    20         fa[i]=i;
    21         val[i]=0;
    22     }
    23 }
    24 
    25 int find_fa(int x)
    26 {
    27     if(fa[x]==x) return x;
    28     int tmp=fa[x];
    29     fa[x]=find_fa(fa[x]);
    30     val[x]=val[x]^val[tmp];
    31     return fa[x];
    32 }
    33 
    34 int main()
    35 {
    36     int n,m;
    37     scanf("%d%d",&n,&m);
    38     init();
    39     for(int i=1;i<=m;i++){
    40         char str[10];
    41         scanf("%d%d%s",&cv[i].a,&cv[i].b,str);
    42         cv[i].w=( str[0]=='o');
    43         seek.insert(cv[i].a);
    44         seek.insert(cv[i].b);
    45     }
    46     set<int>::iterator it;
    47     int top=1;
    48     for(it=seek.begin();it!=seek.end();it++)
    49         seek2[*it]=top++;
    50 
    51     int ans=0;
    52     for(int i=1;i<=m;i++){
    53         int x,y,opx,opy,tmp;
    54         x=seek2[cv[i].a]-1; y=seek2[cv[i].b];
    55         tmp=cv[i].w;
    56         opx=find_fa(x);
    57         opy=find_fa(y);
    58         if(opx!=opy){
    59             fa[opy]=opx;
    60             val[opy]=(val[x]+val[y]+tmp)%2;
    61         }
    62         if(opx==opy&&val[x]^val[y]!=tmp){
    63             ans=i;
    64             break;
    65         }
    66     }
    67     if(!ans)
    68         ans=m+1;
    69 
    70     printf("%d
    ",ans-1);
    71     return 0;
    72 }
    
    
  • 相关阅读:
    2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum
    Codeforces Round #412 C. Success Rate (rated, Div. 2, base on VK Cup 2017 Round 3)
    2017 中国大学生程序设计竞赛 女生专场 Building Shops (hdu6024)
    51nod 1084 矩阵取数问题 V2
    Power收集
    红色的幻想乡
    Koishi Loves Segments
    Wood Processing
    整数对
    Room and Moor
  • 原文地址:https://www.cnblogs.com/ZQUACM-875180305/p/9108400.html
Copyright © 2011-2022 走看看