• POJ 1733 Parity game


    Parity game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4587   Accepted: 1799

    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 <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <cmath> #define N 5677 using namespace std; struct node { int val,next; }ed[N]; int h[N]; int f[N],s[N]; int Find(int x) { if(x!=f[x]) { int tp=Find(f[x]); s[x]^=s[f[x]];//想了一下午才明白为什么这样是对的,因为压缩路径后都直接连到根了,不会再有2次 return f[x]=tp; } return x; } int cnt; int hash(int n) { int e,m=n%N; for(e=h[m];e!=-1;e=ed[e].next) { if(ed[e].val==n) return e; if(ed[e].next==-1) break; } ed[cnt].val=n; ed[cnt].next=h[m]; h[m]=cnt++; return cnt-1; } int xo; bool un(int x,int y) { int rx=Find(x); int ry=Find(y); if(rx!=ry) { f[ry]=rx; s[ry]=s[x]^s[y]^xo; return false; } return true; } int main() { // freopen("in.txt","r",stdin); int i,n; int len; char op[6]; memset(h,-1,sizeof(h)); int from,to; for(i=0;i<=N;i++) f[i]=i; scanf("%d",&len); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d %d %s",&from,&to,op); xo=0; if(op[0]=='o') xo=1; from=hash(from-1); to=hash(to); if(un(from,to)) { if((s[from]^s[to])!=xo) { break; } } } printf("%d\n",i); return 0; }
    //以下是在 http://acm.timus.ru/problemset.aspx
    http://acm.timus.ru/problem.aspx?space=1&num=1003
    Ac的代码

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    using namespace std;
    #define N 5377
    #define M 10002
    struct node
    {
       int val;
       int next;
    }E[N];
    int v[N];
    int cnt;
    int hsh(int n)//这里不能写hash,会和STL里面hash重到
    {
        int k=n%N;
        for(int e=v[k];e!=-1;e=E[e].next)
        {
            if(E[e].val==n)
               return e;
        }
        E[cnt].next=v[k];
        E[cnt].val=n;
        v[k]=cnt++;
        return cnt-1;
    }
    int F[M],r[M];
    int Find(int x)
    {
        if(x!=F[x])
        {
            int pf=F[x];
            F[x]=Find(F[x]);
            r[x]=r[x]^r[pf];
            return F[x];
        }
        return x;
    }
    int main()
    {
        int len,q;
        int a,b;
        int i,Xor;
        char op[6];
        while(scanf("%d",&len),len!=-1)
        {
          scanf("%d",&q);
          cnt=0;
          int t=q<<1;
          for(i=0;i<t;i++)
           F[i]=i,r[i]=0;
          memset(v,-1,sizeof(v));
          for(i=0;i<q;i++)
          {
              scanf("%d %d %s",&a,&b,op);
              a=hsh(a-1);
              b=hsh(b);
              Xor=0;
              if(op[0]=='o')
              Xor=1;
              int x=Find(a);
              int y=Find(b);
              if(x==y)
              {
                  if((r[a]^r[b])!=Xor)
                    break;
              }
              else
              {
                  F[y]=x;
                  r[y]=r[a]^r[b]^Xor;
              }
          }
          printf("%d\n",i);
          if(i<q) i++;
          for(;i<q;i++)
            scanf("%d %d %s",&a,&b,op);
        }
        return 0;
    }


  • 相关阅读:
    文件操作小练习
    阶段练习1
    copy小练习
    小练习
    str 小列题
    条款50:使用自定义的new以及delete的时机会
    条款49:了解new-handle行为
    简单的说一下:tarits技法就是一种模板元编程,起可以将本来处于运行期的事拉到编译期来做,增加了运行效率。 看以非模板元编程的例子,就是前面的那个例子:
    条款47:请使用traits class表示类型信息
    条款46:需要类型转换的时候请为模板定义非成员函数
  • 原文地址:https://www.cnblogs.com/372465774y/p/2625360.html
Copyright © 2020-2023  润新知