【HDU5183】Negative and Positive (NP)

本文提供了一道来自杭电OJ编号为5183的题目解决方案,采用C++实现,主要涉及数组操作、查找算法及数据结构中的集合应用。通过对输入数据进行特定的预处理,并利用集合进行高效查询,最终判断是否存在符合条件的元素组合。

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

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5183

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <set>
using namespace std;
typedef long long LL;
set<LL>s;
LL a[1000005];
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

int main()
{
    int t;
    int flag;
    scanf("%d",&t);
    int cnt=1;
    while(t--)
    {
        int n;
        LL k;
        n=read();
        k=read();
        for(int i=1; i<=n; i++)
        {
            a[i]=read();
            if(i%2==0)
                a[i]=-a[i];
        }
        printf("Case #%d: ", cnt++);
        for(int i=1; i<=n; i++)
        {
            a[i]+=a[i-1];
        }
        flag=1;
        s.clear();
        for(int i=n; i>=0&&flag; i--)
        {
            if(i%2==0)
            {
                if(s.find(a[i]+k)!=s.end())
                    flag=0;
            }
            else
            {
                if(s.find(a[i]-k)!=s.end())
                    flag=0;
            }
            s.insert(a[i]);
        }
        if(flag==0)
           puts("Yes.");
        else
           puts("No.");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值