HDU 5183

呃。。其实挺水的一题,不说了。。伤心。。

这题应该是要分开奇偶性的,因为如果不分开,当a0+...+ai=0,a(i+1)+....+aj=K时,求出来会出错。

维护两种前缀和,

sum=a0-a1+a2......ai

sum=-a0+a1-a2.....ai

维护两个HASH,第一种前缀和当i为奇数时加入,第二种当i为偶数时加入,查找时查询sum-K,-SUM-K

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#define LL __int64
using namespace std; 

const LL N=1000050;
int a[N];
LL K;
struct Hash{
    int tot;
    int nxt[N];
    int head[N];
    LL sum[N];
    void init(){
        tot=0;
        memset(head,-1,sizeof(head));
    }
    bool findx(LL num){
        LL mod=((num%N)+N)%N;
        bool flag=false;
        for(int i=head[mod];i!=-1;i=nxt[i]){
            if(sum[i]==num){
                flag=true;
                break;
            }
        }
        return flag;
    }
    void addin(LL num){
        if(findx(num)) return ;
        LL mod=((num%N)+N)%N;
        sum[tot]=num;
        nxt[tot]=head[mod];
        head[mod]=tot++;
    }
}H0,H1;

void in(int &x)
{
    char ch = getchar();
    int tag = 1;
    x = 0;
    while((ch < ‘0‘|| ch > ‘9‘)) {
        if(ch == ‘-‘) tag = -1;
        ch = getchar();
    }
    while(ch >= ‘0‘ && ch <= ‘9‘) {
        x = x*10 + ch - 48;
        ch = getchar();
    }
    x*=tag;
}

int main(){
    int T,n,t=0;
    LL tmp;
    scanf("%d",&T);
    while(T--){
        H0.init();
        H1.init();
        scanf("%d%I64d",&n,&K);
//        getchar();
        LL pre=0;
        bool flag;
//        H0.addin(0);
        H1.addin(0);
          for(int i = 0; i < n; ++i) {
            in(a[i]);
        }
        for(int i=0;i<n;i++){
            flag=false;
            if(i%2==0)
            pre=pre+a[i];
            else pre=pre-a[i];
            if(H1.findx(pre-K)){ flag= true; break; }
            if(H0.findx(-pre-K)) { flag=true; break; }
            if(i%2){
                H1.addin(pre);
            }
            else H0.addin(-pre);
        }
        if(flag){
            printf("Case #%d: Yes.\n",++t);
        }
        else printf("Case #%d: No.\n",++t);
    }
    return 0;
}

  

时间: 2024-08-05 01:25:58

HDU 5183的相关文章

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

BC # 32 1002 题意:给出一个数组 a 和一个数 K ,问是否存在数对( i , j ),使 a i   - a i + 1 +……+ (-1)j - i  a j : 对于这道题,一开始就想到了是前缀和,但是如果只是记录下前缀和数组,那么查找就会成为一个大问题.补题的时候一开始考虑用 hash 数组或者是 set 存,但是很明显 TLE 了,在翔神的推荐下,我研究了一下 hash表的创建过程,惊奇地发现,其实就是建了一个 HashMap 结构体,而里面放了这个表所用的数组以及相应操作

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

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)(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 (哈希表)

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

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

hdu5183 hush

http://acm.hdu.edu.cn/showproblem.php?pid=5183 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)