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?a_{i+1}+a_{i+2}+?+(?1)^{j?i}a_j\)

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 \((a_0,a_1,a_2,?a_{n?1})\) separated by exact one space.

[Technical Specification]

All input items are integers.

\(0<T≤25,1≤n≤1000000,?1000000000≤a_i≤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

Solution

题意

给定长度为 \(n\) 的数组,问是否存在一段区间其加减交错的和等于 \(k\)。

思路

对该数组求前缀和然后哈希就行。

不过这题不同的 set 过不了。

所以要手写哈希。

当然 unordered_set 也可以过。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 7;

struct HashMap {
    int head[maxn], next[maxn];
    ll num[maxn];
    int tot;
    inline void init() {
        tot = 0;
        memset(head, -1, sizeof(head));
    }
    inline void insert(ll val) {
        int h = abs(val) % maxn;
        num[tot] = val, next[tot] = head[h], head[h] = tot++;
    }
    inline bool find(ll val) {
        int h = abs(val) % maxn;
        for(int i = head[h]; ~i; i = next[i]) {
            if(num[i] == val) {
                return true;
            }
        }
        return false;
    }
} hashmap;

ll arr[maxn], sum[maxn];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    int kase = 0;
    while(T--) {
        hashmap.init();
        ll n, k;
        cin >> n >> k;
        for(int i = 1; i <= n; ++i) {
            cin >> arr[i];
            sum[i] = sum[i - 1] + (i & 1? arr[i]: -arr[i]);
        }
        int flag = 0;
        for(int i = n; i; --i) {
            hashmap.insert(sum[i]);
            if(i & 1) {
                if(hashmap.find(sum[i - 1] + k)) {
                    flag = 1;
                    break;
                }
            } else {
                if(hashmap.find(sum[i - 1] - k)) {
                    flag = 1;
                    break;
                }
            }
        }
        cout << "Case #" << ++kase << ": " << (flag? "Yes." : "No.") << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wulitaotao/p/11823679.html

时间: 2024-10-02 05:46:00

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) ——(后缀和+手写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 (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

手写哈希类

1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #define Mod 100007 //取模的大小,哈希表的大小... 5 #define Max 50 //存放的总数 6 typedef long long LL; 7 class Hash //手写哈希 8 { 9 public: 10 int hs[Mod]; //哈希值 设定的哈希函数为 原值 % Mod ,所以哈希值有可能是 0 ~

[Codevs 1230]元素查找(手写哈希表)

题目连接:http://codevs.cn/problem/1230/ 说白了就是要我们自己手写一个哈希表的数据结构来实现添加和查找功能,map也能直接过(我第一次写就是用map骗AC的) 提一下个人理解的哈希表的实现(下面说的是线性寻址法),如果有误还请各位大神不吝指教 用一个数组模拟哈希表,函数f(x)=数字x在哈希表中出现的下标的最小可能值,一般f(x)=x mod t,t就是哈希表的长度 下面就是一个哈希表的示例,如果遍历哈希表时指针走出了哈希表的终点,就进入起点重新遍历 对于每次向哈希