hdu 5183-Negative and Positive (NP)

题目:大概意思就是给定一个序列a[0],a[1]....a[n-1]和一个整数k,问是否有这样的两个下标是的NP-Sum(i,j)=k,这里NP-Sum(i,j)=a[i]-a[i+1]+a[i+2]-...+(-1)^(j-1)*a[j]。

思路:我们可以维护这个序列的后缀和(前缀也是可以的),然后枚举sum[i]查看在set表中是否存在sum[j]=sum[i]-k。

这里要分成i为奇数时的计算和偶数时的计算(原因是我们只维护一次sum的值,或+-+-+的形式,或-+-+-的形式),具体看代码:

#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
typedef long long LL;

inline LL Scan()
{
    char ch;
    LL flag=1,Num=0;
    while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
    if(ch=='-') flag=-1;
    else if(ch>='0'&&ch<='9')
            Num=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
    {
        Num=Num*10+ch-'0';
    }
    return flag*Num;
}
LL nt[1000010],k,sum=0;
set<LL> Bt;

int main()
{
    Bt.clear();
    int Cas;
    scanf("%d",&Cas);
    for(int ca=1;ca<=Cas;ca++)
    {
        int n;
        bool flag=false;
        scanf("%d %I64d",&n,&k);
        getchar();
        for(int i=1;i<=n;i++)
        {
            nt[i]=Scan();
        }
        sum=0;
        Bt.clear();
        Bt.insert(0);
        for(int i=n;i>0;i--)
        {
            if(i%2) sum-=nt[i];
            else sum+=nt[i];
            if(i%2==0)
            {
                if(Bt.find(sum-k)!=Bt.end())
                {
                    flag=true;
                    break;
                }
            }
            else
            {
                if(Bt.find(sum+k)!=Bt.end())
                {
                    flag=true;
                    break;
                }
            }
            Bt.insert(sum);
        }
        printf("Case #%d: %s\n",ca,flag?"Yes.":"No.");
    }
    return 0;
}

时间卡的比较死,飘过的~~

时间: 2024-12-20 20:12:53

hdu 5183-Negative and Positive (NP)的相关文章

HDU 5183 Negative and Positive (NP) (set + 读入外挂 乱搞)

Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 502    Accepted Submission(s): 141 Problem Description When given an array (a0,a1,a2,?an?1) and an integer K, you are

HDU 5183 Negative and Positive (NP) (手写哈希)

题目链接:HDU 5183 Problem Description When given an array \((a_0,a_1,a_2,?a_{n?1})\) and an integer \(K\), you are expected to judge whether there is a pair \((i,j)(0≤i≤j<n)\) which makes that \(NP?sum(i,j)\) equals to \(K\) true. Here \(NP?sum(i,j)=a_i?

hdu 5183 Negative and Positive (NP)(STL-集合【HASH】)

题意: When given an array (a0,a1,a2,?an−1) and an integer K, you are expected to judge whether there is a pair (i,j)(0≤i≤j<n) which makes that NP−sum(i,j) equals to K true. Here NP−sum(i,j)=ai−ai+1+ai+2+?+(−1)j−iaj 1≤n≤1000000,−1000000000≤ai≤1000000000

HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)

根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namespace std; 5 const int N = 1000000 + 10; 6 const int HASH = 1000000 + 7; 7 typedef long long ll; 8 struct hashmap

HDU 5183 Negative and Positive (NP) --Hashmap

题意:问有没有数对(i,j)(0<=i<=j<n),使得a[i]-a[i+1]+...+(-1)^(j-i)a[j]为K. 解法:两种方法,枚举起点或者枚举终点. 先保存前缀和:a1-a2+a3....+/- an 枚举起点法: 设起点为x,实际是枚举x-1,分两种情况: 1.起点x为奇,那么就看有没有a[j]-a[x-1] = K的,即a[j] = a[x-1]+K.因为奇数位置的ai数符为正. 2.起点x为偶,那么就看有没有a[j]-(-K) = a[x-1],即a[j] = a[x

hdu 5183. Negative and Positive (哈希表)

Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2177    Accepted Submission(s): 556 Problem Description When given an array (a0,a1,a2,?an−1) and an integer K, you are

[HDOJ 5183] Negative and Positive (NP) 【Hash】

题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCoder比赛的时候我写了 STL map, 然后TLE... 注意: Hash负数的时候 % 了一个质数,得到的是负数还要 + Mod !! 代码 #include <iostream> #include <cstdio> #include <cstdlib> #includ

Negative and Positive (NP) ( Hash 维护 )

Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2846 Accepted Submission(s): 782 Problem Description When given an array (a0,a1,a2,?an?1) and an integer K, you are expec

hdu 5183(hash)

传送门:Negative and Positive (NP) 题意:给定一个数组(a0,a1,a2,?an−1)和一个整数K, 请来判断一下是否存在二元组(i,j)(0≤i≤j<n)使得 NP−sum(i,j) 刚好为K.这里NP−sum(i,j)=ai−ai+1+ai+2+?+(−1)j−iaj. 分析:根据a[i]的i为奇偶进行hash,维护两种前缀和 1)i为奇数开头:sum=a[i]-a[i+1]+a[i+2]... 2)i为偶数开头:sum=a[i]-a[i+1]+a[i+2]...

True Positive|True Negative|False Positive|False Negative

True Positive|True Negative|False Positive|False Negative 表示分类正确: True Positive:本来是正样例,分类成正样例. True Negative:本来是负样例,分类成负样例. 表示分类错误: False Positive :本来是负样例,分类成正样例,通常叫误报. False Negative:本来是正样例,分类成负样例,通常叫漏报. true positive rate = true positive / (true po