hdu 5183 Negative and Positive (NP)(hash,思路)

3 篇文章 0 订阅

Negative and Positive (NP)

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3330    Accepted Submission(s): 896


Problem Description
When given an array  (a0,a1,a2,an1)  and an integer  K , you are expected to judge whether there is a pair  (i,j)(0ij<n)  which makes that  NPsum(i,j)  equals to  K  true. Here  NPsum(i,j)=aiai+1+ai+2++(1)jiaj
 

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  2T  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,an1) separated by exact one space.
[Technical Specification]
All input items are integers.
0<T25,1n1000000,1000000000ai1000000000,1000000000K1000000000
 

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  PNsum(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.
题意:给n个数字,问存不存在一个

NPsum(i,j)=aiai+1+ai+2++(1)jiaj等于k

思路:我们先维护两个前缀和,第一个是sum[i]=a[1]-a[2]+a[3]+,,,+(-1)的(i-1)次方*a[i]

第二个就等于-sum[i],就是说奇数项为负~

然后我们维护两个hash,一个保存sum[i],一个保存-sum[i]

然后枚举区间的左端点,因为sum[i]-sum[j]=k

因为区间[i,j]里,第一项a[i]必须为正。

所以如果当前枚举的i点下标为奇数,那么我们就从第一个hash里找有没有一个值等于sum[i]-k

反之如果当前枚举的i点下标为偶数,那么我们就从第二个hash里找有没有一个值等于-sum[i]-k(也就是把整个-sum[i]-k=-sum[j])

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 1000050
long long a[N],sum[N];
struct Hashmap
{
    int head[N],next[N],cnt;
    long long state[N];
    void init()
    {
        memset(head,-1,sizeof(head));
        cnt=0;
    }
    int check(long long val)
    {
        int h=(val%N+N)%N;
        for(int i=head[h];i!=-1;i=next[i])
            if(state[i]==val) return 1;
            return 0;
    }
    int Insert(long long val)
    {
        int h=(val%N+N)%N;
        for(int i=head[h];i!=-1;i=next[i])
            if(val==state[i])
                return 1;
        state[cnt]=val;
        next[cnt]=head[h];
        head[h]=cnt++;
        return 0;
    }
}H1,H2;
int main()
{
    int T;
    int n,k,t=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
            long long sum=0;
        H1.init();H2.init();
        H1.Insert(0);H2.Insert(0);
        int flag=0;
        for(int i=n;i>=1&&!flag;i--)
        {
            if(i&1) sum+=a[i];
            else sum-=a[i];
            if(i&1)
            {
                if(H1.check(sum-k))
                    flag=1;
            }
            else
            {
                if(H2.check(-sum-k))
                    flag=1;
            }
            H1.Insert(sum);
            H2.Insert(-sum);
        }
        printf("Case #%d: ",t++);
        if(flag) printf("Yes.\n");
        else printf("No.\n");
    }
    return 0;
}



NPsum(i,j)=aiai+1+ai+2++(1)jiaj
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值