zoukankan      html  css  js  c++  java
  • poj1733Parity game

    Parity game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7288   Accepted: 2833

    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
    
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 using namespace std;
     6 const int MAX = 5010;
     7 int a[2*MAX],father[2*MAX],r[2*MAX];
     8 struct Node
     9 {
    10     int u,v;
    11     char str[10];
    12 };
    13 Node que[MAX];
    14 int n,m,cnt;
    15 int Bin(int x)
    16 {
    17     int r = cnt - 1;
    18     int l = 0;
    19 
    20     while(r >= l)
    21     {
    22         int mid = (r + l) / 2;
    23         if(a[mid] > x)
    24             r = mid - 1;
    25         else if(a[mid] < x)
    26             l = mid + 1;
    27         else
    28             return mid;
    29     }
    30     return -1;
    31 }
    32 int find_father(int x)
    33 {
    34     if(father[x] == x)
    35         return x;
    36     int t = find_father(father[x]);
    37     r[x] = r[x] ^ r[father[x]];
    38     return father[x] = t;
    39 }
    40 int main()
    41 {
    42     while(scanf("%d%d",&n,&m) != EOF)
    43     {
    44         cnt = 0;
    45         for(int i = 1; i <= m; i++)
    46         {
    47             scanf("%d%d%s", &que[i].u,&que[i].v,que[i].str);
    48             que[i].u --;
    49             a[cnt++] = que[i].u;
    50             a[cnt++] = que[i].v;
    51         }
    52         sort(a,a + cnt);
    53         cnt = unique(a,a + cnt) - a;
    54         for(int i = 1; i <= cnt; i++)
    55         {
    56             father[i] = i;
    57             r[i] = 0;
    58         }
    59         int ans = 0;
    60         for(int i = 1; i <= m; i++)
    61         {
    62             int x = Bin(que[i].u);
    63             int y = Bin(que[i].v);
    64             int fx = find_father(x);
    65             int fy = find_father(y);
    66             if(fx == fy)
    67             {
    68                 if(r[x] == r[y] && strcmp(que[i].str,"odd") == 0)
    69                     break;
    70                 if(r[x] != r[y] && strcmp(que[i].str,"even") == 0)
    71                     break;
    72                 ans++;
    73             }
    74             else
    75             {
    76                 if(strcmp(que[i].str,"odd") == 0)
    77                 {
    78                     father[fx] = fy;
    79                     r[fx] = r[x]^ 1 ^ r[y];
    80                 }
    81                 else
    82                 {
    83                     father[fy] = fx;
    84                     r[fy] = r[x] ^ r[y] ^ 0;
    85                 }
    86                 ans ++;
    87             }
    88         }
    89         printf("%d
    ",ans);
    90     }
    91 
    92 
    93 
    94     return 0;
    95 }
    View Code
  • 相关阅读:
    UVA1401 Remember the word DP+Trie
    LG5202 「USACO2019JAN」Redistricting 动态规划+堆/单调队列优化
    模拟赛总结合集
    LG5201 「USACO2019JAN」Shortcut 最短路树
    LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组
    LG5196 「USACO2019JAN」Cow Poetry 背包+乘法原理
    20190922 「HZOJ NOIP2019 Round #7」20190922模拟
    LG2530 「SHOI2001」化工厂装箱员 高维DP+记忆化搜索
    LG2893/POJ3666 「USACO2008FEB」Making the Grade 线性DP+决策集优化
    关于对QQ 输入法的评价
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/5018029.html
Copyright © 2011-2022 走看看