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

    Parity game
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 7101 Accepted: 2751
    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

    CEOI 1999

    很好的一道题目,并查集解决。
    区间3到5中1的个数可以理解为区间1-5中1的个数减去区间1-2中1的个数。
    那么 如果区间1-5中有偶数个1 区间1-2中 有奇数个1 则区间3-5中有奇数个1.
    由于数据过大,用map离散化一下:

    /* ***********************************************
    Author        :pk28
    Created Time  :2015/8/16 13:03:28
    File Name     :4.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 12000+10
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    
    bool cmp(int a,int b){
        return a>b;
    }
    struct Line{
        int a,b;
        char c[50];
    }line[5005];
    int t[12005],fa[maxn],d[maxn],n,m,mark,cnt;
    map<int ,int>mp;
    void init(){
        for(int i=0;i<=12005;i++){//此处不能超过maxn
            d[i]=0;
            fa[i]=i;
            t[i]=0;
        }
        mark=0;
        cnt=0;
        mp.clear();
    }
    int findfa(int x){
        if(x==fa[x])return x;
        else{
            int root=findfa(fa[x]);
            d[x]+=d[fa[x]];
            fa[x]=root;
            return fa[x];
        }
    }
    void Union(int a,int b,int c){
        int x=findfa(a);
        int y=findfa(b);
        if(x==y){
            if(((abs(d[b]-d[a]))&1)!=(c&1))mark=1;
            return ;
        }
        else{
            fa[x]=y;
            d[x]=d[b]+c-d[a];
        }
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        while(cin>>n>>m){
            init();
            int i,j,ans;
            for(i=1;i<=m;i++){
                scanf("%d%d%s",&line[i].a,&line[i].b,line[i].c);
                t[cnt++]=line[i].a;
                t[cnt++]=line[i].b;
            }
            sort(t,t+cnt);
            j=1;
            for(i=0;i<cnt;i++){
                if(mp.count(t[i])==0)mp[t[i]]=j++;
            }
            for(i=1;i<=m;i++){
                int a=mp[line[i].a]-1;
                int b=mp[line[i].b];
                int cv;
                if(line[i].c[0]=='e')cv=0;
                else cv=1;
                if(mark)continue;
                Union(a,b,cv);
                if(mark)ans=i;
            }
            if(!mark)printf("%d
    ",m);
            else printf("%d
    ",ans-1);
        }
        return 0;
    }
    原文地址:http://www.cnblogs.com/pk28/ 与有肝胆人共事,从无字句处读书。
    欢迎关注公众号:
  • 相关阅读:
    tuple-1
    禅语-1
    综述的写作技巧-1
    皆大欢喜组合
    类和对象-3
    双棍练习
    CodeBlocks开发环境使用-1
    类和对象-2
    类和对象-1
    13-归并排序-分治策略应用于排序
  • 原文地址:https://www.cnblogs.com/pk28/p/4734492.html
Copyright © 2011-2022 走看看