zoukankan      html  css  js  c++  java
  • Parity game POJ

    Parity game
    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

    题意:有一串由0和1组成的序列,长度为n。给m次信息,每次给出区间左边界l,右边界r,字符串str,表示[l, r]区间内1的数量,str为odd代表有奇数个1,为even代表有偶数个1。题目要求找到第一个出现逻辑错误的行数。

    思路:由前缀和的思想知道,sum[i]代表[0, i]内1的数量,那么sum[r] - sum[l-1]就可以表示[l, r]中的1的数量了。因为要找出逻辑错误,所以出现过的端点,我们要将其连接起来,并维护两个端点之间的信息。这就可以用带权并查集来维护了。两个偶数或两个奇数相减得到的都是偶数,一奇一偶相减得到奇数。所以我们只需要维护一个端点与其父端点当中1数量的奇偶性即可。但是长度n <= 1e9,如果数组空间开这么大肯定是不行的。所幸m <= 5000,意思就是出现的端点最多就是2*5000也就是10000,这个空间是我们可以接受的。所以离散化处理端点。

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 using namespace std;
     6 const int N = 5050;
     7 struct question{
     8     int l, r, v;
     9 }q[N<<1];
    10 int f[N<<1], rela[N<<1], n, m, cnt, a[N<<1];
    11 char str[10];
    12 
    13 int find(int x);
    14 int getid(int x);
    15 
    16 int main()
    17 {
    18     scanf("%d%d", &n, &m);
    19     for (int i=1;i<=m;i++)
    20     {
    21         scanf("%d %d %s", &q[i].l, &q[i].r, str);
    22         q[i].l --;
    23         if (str[0] == 'e') q[i].v = 0;
    24         else q[i].v = 1;
    25         a[++cnt] = q[i].l;
    26         a[++cnt] = q[i].r;
    27     }
    28     sort(a+1, a+1+cnt);
    29     cnt = unique(a+1, a+1+cnt) - a - 1;
    30 //    for (int i=1;i<=cnt;i++)
    31 //        printf("%d ", a[i]);
    32 //    puts("");
    33     for (int i=1;i<=cnt;i++)
    34         f[i] = i, rela[i] = 0;
    35     for (int i=1;i<=m;i++)
    36     {
    37         int x = getid(q[i].l), y = getid(q[i].r);
    38 //        printf("x=%d, y=%d
    ", x, y);
    39         int fx = find(x), fy = find(y);
    40         if (fx != fy)
    41         {
    42             rela[fy] = (rela[x] + rela[y] + q[i].v) % 2;
    43             f[fy] = fx;
    44         }else
    45         {
    46             if ((rela[x] + rela[y]) % 2 != q[i].v)
    47             {
    48                 printf("%d
    ", i-1);
    49                 return 0;
    50             }
    51         }
    52     }
    53 //    for (int i=1;i<=cnt;i++)
    54 //        printf("%d ", rela[i]);
    55 //    puts("");
    56     printf("%d
    ", m);
    57     return 0;
    58 }
    59 
    60 int getid(int x)
    61 {
    62     return lower_bound(a+1, a+1+cnt, x) - a;
    63 }
    64 int find(int x)
    65 {
    66     if (f[x] == x) return x;
    67     int temp = f[x];
    68     f[x] = find(f[x]);
    69     rela[x] = (rela[x] + rela[temp]) % 2;
    70     return f[x];
    71 }
    View Code

    总结:离散化算是第一次接触,还是写下具体的离散化过程吧。

    1、需要的函数unique(), 里面需要两个参数,需要去重开始位置地址和结束位置地址,和sort函数类似。头文件<algorithm>。这个函数只会把相邻的重复元素变成一个,所以要先排序再使用。返回值是去重后的“新数组”的最后一个元素地址往后一位,所以要得到“新数组”的长度的话,返回值还要减去开始元素的地址。(另外,最开始查的时候看到一种说法是,unique()函数会把重复的元素放到数组尾部去,但事实上这个函数没这么智能,只是把后一个不重复的元素放到第一个重复的位置上罢了)

    2、需要的函数lower_bound(),复制粘贴一下。

    lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

    upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

    基本上就已经讲清楚了。先将给出的所有元素一一存进数组,再sort排序,再unique去重。离散化过程就结束了。

  • 相关阅读:
    《社区教育在线学习平台评价体系的设计研究》 文献随笔(七)
    《基于个性化推荐的在线学习系统研究与实现》 文献随笔(六)
    《基于校园网云平台微课在线学习系统的设计分析》 文献随笔(五)
    《基于微服务架构的在线学习系统设计与实现》第三章 文献随笔(四)
    《基于微服务架构的在线学习系统设计与实现》第一章节5 文献笔记(三)
    《基于Android的微课程平台设计》论文笔记(二)
    《基于Android的在线学习软件的设计与实现》论文笔记一
    在线学习系统的设计与实现 文献综述
    《基于JavaEE的全丰集团OA系统的设计与实现》论文笔记(十六)
    《电子签名技术在 OA 系统公文流转中的应用》论文笔记(十五)
  • 原文地址:https://www.cnblogs.com/FantaDevourer/p/12792949.html
Copyright © 2011-2022 走看看