UESTC Training for Data Structures——L

Problem  L

Problem 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

There are several cases in the input file. The first line of each case 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 for each case 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
/*用并查集,先将所有的元素hash一下,我用的是排序取他们的序号代替
  然后利用r[x]==r[y]^val(r[x]:集合代表到 x 之间奇偶关系)合并和判断是否合法*/

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 10001
using namespace std;
struct data
{
    int st, en;
    char s[10];
} qu[N]; //记录询问
int fa[N],r[N]; //fa[x]: x节点的父亲 r[x]:x到fa[x]的奇偶性
int a[N],b[N]; //用于离散化
int len,n,tot;
int find_father(int x)
{
    if(x==fa[x]) return x;
    else
    {
        int father=find_father(fa[x]);
        r[x]=r[x]^r[fa[x]];
        return fa[x]=father;
    }
}
bool marge_set(int x,int y,int val)
{
    int i=find_father(x);
    int j=find_father(y);
    if(i==j)
    {
        if(r[x]==r[y]^val) return true;
        else return false;
    }
    else
    {
        fa[j]=i;
        r[j]=r[x]^r[y]^val;
        return true;
    }
}
int find(int st,int en,int x)  //二分查找
{
    if(st==en) return st;
    int mid=(st+en)>>1;
    if(b[mid]>=x) return find(st,mid,x);
    else return find(mid+1,en,x);
}
void qsort(int st,int en)
{
    int i=st,j=en,x=a[st];
    while(i<j)
    {
        while(i<j && x<=a[j]) j--;
        if(i<j) { a[i]=a[j]; i++; }
        while(i<j && x>=a[i]) i++;
        if(i<j) { a[j]=a[i]; j--; }
    }
    a[i]=x;
    if(st<i-1) qsort(st,i-1);
    if(i+1<en) qsort(i+1,en);
}
void hash()
{
    tot=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%s",&qu[i].st,&qu[i].en,qu[i].s);
        a[++tot]=qu[i].st;
        a[++tot]=qu[i].en;
    }
    qsort(1,tot);
    int p1=0,p2=1;
    while(p2<=tot)  //删除重复的值
    {
        b[++p1]=a[p2];
        while(a[p2]==a[p2+1] && p2<=tot) p2++;
        p2++;
    }
    tot=p1;
}
int main()
{
    while(scanf("%d%d",&len,&n)!=EOF)
    {
        hash();
        for(int i=1;i<=10000;i++)
        {
            fa[i]=i;
            r[i]=0;
        }
        int ans=-1;
        int st,en;
        for(int i=1;i<=n;i++)
        {
            int st=qu[i].st,en=qu[i].en;
            if(st>en) { ans=i-1; break; }
            if(en>len) { ans=i-1; break; }
            char s=qu[i].s[0];
            st=find(1,tot,st-1);
            en=find(1,tot,en);
            int p=(s=='o');
            if(!marge_set(st,en,p))
            {
                ans=i-1;
                break;
            }
        }
        if(ans==-1) ans=n;
        printf("%d\n",ans);
    }
    return 0;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
互联网络程序设计是指在互联网上进行程序开发和设计的过程。UESTC则是我国的一所著名高校——电子科技大学。 互联网络程序设计 uestc包含了两个主要的方面:互联网络和程序设计。互联网络是指将多个计算机网络通过通信链路互相连接起来,实现信息共享和资源共享的网络系统。程序设计是指根据需求和目标,通过编写代码和设计算法,实现计算机程序的过程。 互联网络程序设计 uestc的学习内容主要包括以下几个方面: 1. 网络知识:学习互联网络的基本概念、原理和协议,如TCP/IP协议、HTTP协议等。掌握网络编程的基本技术,能够编写网络应用程序。 2. 数据通信:学习数据通信的基本原理和技术,包括数据传输的方式、数据压缩和加密等。了解网络安全和数据保护的基本知识。 3. 程序设计:学习编程语言和开发工具,如Java、C++和Python等。掌握常用的编程技巧和方法,能够设计和实现复杂的网络应用程序。 4. Web开发:学习Web开发的基本知识和技术,包括HTML、CSS、JavaScript等。能够设计和实现交互式的Web应用程序。 5. 数据库技术:学习数据库的基本原理和技术,如SQL语言和数据库管理系统。能够设计和管理数据库,实现数据的存储和检索。 通过学习互联网络程序设计 uestc,可以掌握互联网应用开发的基本技能,具备设计和实现网络应用程序的能力。这对于目前互联网行业的人才需求来说是非常重要的,也为学生提供了广阔的就业和创业机会。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值