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 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

Input

Multi test cases. In the first line of the input file there is an integer T indicates the number of test cases.

In the next 2?T lines, it will list the data for each test case.

Each case occupies two lines, the first line contain two integers n and K which are mentioned above.

The second line contain (a0,a1,a2,?an?1)separated by exact one space.

[Technical Specification]

All input items are integers.

0<T≤25,1≤n≤1000000,?1000000000≤ai≤1000000000,?1000000000≤K≤1000000000

Output

For each case,the output should occupies exactly one line. The output format is Case #id: ans, here id is the data number starting from 1; ans is “Yes.” or “No.” (without quote) according to whether you can find (i,j) which makes PN?sum(i,j) equals to K.

See the sample for more details.

Sample Input

2

1 1

1

2 1

-1 0

Sample Output

Case #1: Yes.

Case #2: No.

Hint

If input is huge, fast IO method is recommended.

Source

BestCoder Round #32

Positive and Negative

维护前缀和sum[i]=a[0]-a[1]+a[2]-a[3]+…+(-1)^i*a[i]

枚举结尾i,然后在hash表中查询是否存在sum[i]-K的值。

如果当前i为奇数,则将sum[i]插入到hash表中。

上面考虑的是从i为偶数为开头的情况。

然后再考虑以奇数开头的情况,按照上述方法再做一次即可。

不同的是这次要维护的前缀和是sum[i]=-(a[0]-a[1]+a[2]-a[3]+…+(-1)^i*a[i])

I为偶数的时候将sum[i]插入到hash表。

总复杂度o(n)

#include<bits/stdc++.h>
using namespace std;
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32)if(c==EOF)return 0;
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return 1;
}
template<class T> inline T read_(T&x,T&y)
{
    return read(x)&&read(y);
}
template<class T> inline T read__(T&x,T&y,T&z)
{
    return read(x)&&read(y)&&read(z);
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//-------ZCC IO template------
const int maxn=1e6+1000;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long  LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod  100007

LL sum[maxn];
LL a[maxn];

vector<LL> hash_[mod];
void insert(LL key)
{
    int pos=(key%mod+mod)%mod;
    hash_[pos].push_back(key);
}
bool find(LL key)
{
    int pos=(key%mod+mod)%mod;
    int N=hash_[pos].size();
    for(int i=0;i<N;i++)
        if(hash_[pos][i]==key)return true;
    return false;
}

bool solve(LL n,LL k)
{
    for(int i=0;i<mod;i++)hash_[i].clear();
    insert(0);
    for(int i=0;i<n;i++)
    {
        if(find(sum[i]-k))return true;
        if(i&1)insert(sum[i]);
    }

    for(int i=0;i<mod;i++)hash_[i].clear();
    for(int i=0;i<n;i++)
    {
        if(find(-sum[i]-k))return true;
        if(!(i&1))insert(-sum[i]);
    }

    return false;
}

int main()
{
    int T,cas=1;
    read(T);
    while(T--)
    {
        LL k,n;
        read_(n,k);
        for(int i=0;i<n;i++)
        {
            read(a[i]);
        }
        sum[0]=a[0];
        for(int i=1;i<n;i++)
            if(i&1)
               sum[i]=sum[i-1]-a[i];
            else
               sum[i]=sum[i-1]+a[i];
        printf("Case #%d: %s\n",cas++,solve(n,k)?"Yes.":"No.");
    }
    return 0;
}
时间: 2024-11-06 21:21:59

Negative and Positive (NP) ( Hash 维护 )的相关文章

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)(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

[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

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) --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

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

AC dreamoj 1011 树状数组+hash维护字符串的前缀和

http://acdream.info/problem?pid=1019 Problem Description Now we have a long long string, and we will have two kinds of operation on it. C i y : change the ith letter to y. Q i j : check whether the substring from ith letter to jth letter is a palindr