POJ 1733 Parity game

本文介绍了一个基于零一序列的游戏,玩家需要通过询问子序列中1的数量来猜测整个序列,并检查朋友给出的答案是否一致。文章提供了详细的算法实现,利用并查集来解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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' orodd’ (the answer, i.e. the parity of the number of ones in the chosen subsequence, where even' means an even number of ones andodd’ 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


这是一道鲜血淋漓的题,大家记着,一定要在外面写f[x]=fnd(x)(半位长者的忠告)。
题目大意是:一个由0,1组成的数字串~~,现在你问一个人,第i位到第j位的1的个数为奇数还是偶数。一共会告诉你几组这样的数
要你判断前k组这个人回答的都是正确的,到第k+1组,这个人说的是错的,要你输出这个k,要是这个人回答的都是正确的,则输出组数
odd为奇数,even为偶数。
分析:
思路:赤裸裸的种类并查集吧…….其中,我们把一段区间为奇数标记为0,为偶数标记为1,然后如果区间连贯,也就是说区间1到区间2,区间3到区间4,那么就是可以连贯成区间1到区间4的,如此的话,可以是左极限-1,或者右极限+1……判断是否在同一个树上,在的话,判断是否正确,不在的话,连接起来,在连接的时候,按照种类并查集的操作即可…….


#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
const int maxq=50005;
int n,q,tp,num,f[maxq],w[maxq],d[maxq];
struct qa
{
    int xx,yy,z;
}a[maxq];
int fnd(int x)
{
    if(f[x]!=x)
    {
        int ff=fnd(f[x]);
        w[x]=(w[f[x]]+w[x])%2;
        f[x]=ff;
    }
    return f[x];
}
int solve()
{
    for(int i=1;i<=q;i++)
    {
        int t1=lower_bound(d+1,d+num+1,a[i].xx)-d,t2=lower_bound(d+1,d+num+1,a[i].yy)-d;
        int fx=fnd(t1),fy=fnd(t2);
        if(fx==fy)
        {
            if((w[t1]-w[t2]+2)%2!=a[i].z)
                return i-1;
        }
        else
        {
            w[fx]=(w[t2]-w[t1]+a[i].z+2)%2;
            f[fx]=fy;
        }
    }
    return q;
}
int main()
{
    scanf("%d%d",&n,&q);
    char ch[11];
    for(int i=1;i<=q;i++)
    {
        scanf("%d%d%s",&a[i].xx,&a[i].yy,ch);
        a[i].xx--;
        d[++tp]=a[i].xx;
        d[++tp]=a[i].yy;
        if(ch[0]=='o')
            a[i].z=1;
        else
            a[i].z=0;
    }
    sort(d+1,d+tp+1);
    num=unique(d+1,d+1+tp)-d+1;
    for(int i=1;i<=num;++i)
        f[i]=i;
    printf("%d\n",solve());
    return 0;
}
内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值