题目链接:

hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5183

bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=570&pid=1002

题解:

前缀和+哈希

维护两张哈希表hash1,hash2,一张维护前缀和sum=a[0]-a[1]+a[2]-a[3]+...+(-1)^i*a[i],另一张维护-sum=-a[0]+a[1]-a[2]+a[3]-...+(-1)^(i+1)*a[i];当i为奇数的时候,插入到第一张哈希表hash1,当i为偶数的时候插入到第二张表hash2,每次查询在hash1中是否存在sum-k,或在hash2中是否存在-sum-k,如果有一个条件成立,则说明有解。否则继续做下去,直到最后还没有答案则无解。

代码:

hash表大小设为100007,输入为int,用vector做hash,g++,1185MS高飘

 #include<map>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long LL; const int mod = ; int n,k; vector<LL> v[][mod]; int Hash(LL x) {
return (x%mod + mod) % mod;
} bool que(LL x,int type) {
int key = Hash(x);
for (int i = ; i < v[type][key].size(); i++) {
if (v[type][key][i] == x) return true;
}
return false;
} void add(LL x,int type) {
int key = Hash(x);
if (!que(x,type)) {
v[type][key].push_back(x);
}
} void init() {
for (int i = ; i < mod; i++) {
v[][i].clear();
v[][i].clear();
}
} int main() {
int tc, kase=;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d", &n, &k);
init();
LL sum = ;
bool ans = ;
for (int i = ; i < n; i++) {
int x;
scanf("%d", &x);
if (i & ) sum -= x;
else sum += x;
if (que(sum - k,) || que(-sum - k,)) {
ans = true;
}
if (i & ) add(sum, );
else add(-sum, );
}
if (sum == k) ans = true;
printf("Case #%d: ",++kase);
if (ans) printf("Yes.\n");
else printf("No.\n");
}
return ;
}

hash表大小1000007,输入int,邻接表做hash,g++, 811ms

 #include<map>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long LL; const int mod = ;
const int maxn=+; struct Edge{
LL x; int ne;
Edge(LL x,int ne):x(x),ne(ne){}
Edge(){}
}egs[maxn*]; int n,k; int v[][mod],tot; int Hash(LL x) {
return (x%mod + mod) % mod;
} bool que(LL x,int type) {
int key = Hash(x);
int p=v[type][key];
while(p!=-){
Edge& e=egs[p];
if(e.x==x) return true;
p=e.ne;
}
return false;
} void add(LL x,int type) {
int key = Hash(x);
if (!que(x,type)) {
egs[tot]=Edge(x,v[type][key]);
v[type][key]=tot++;
}
} void init() {
memset(v,-,sizeof(v));
tot=;
} int main() {
int tc, kase=;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d", &n, &k);
init();
LL sum = ;
bool ans = ;
for (int i = ; i < n; i++) {
int x;
scanf("%d", &x);
if (i & ) sum -= x;
else sum += x;
if (que(sum - k,) || que(-sum - k,)) {
ans = true;
}
if (i & ) add(sum, );
else add(-sum, );
}
if (sum == k) ans = true;
printf("Case #%d: ",++kase);
if (ans) printf("Yes.\n");
else printf("No.\n");
}
return ;
}

HDU 5183 Negative and Positive (NP) 前缀和+哈希的更多相关文章

  1. 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\ ...

  2. hdu 5183 Negative and Positive (NP)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5183 Negative and Positive (NP) Description When give ...

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

  4. HDU 5183 Negative and Positive (NP) (hashmap+YY)

    学到了以邻接表方式建立的hashmap 题意:给你一串数a和一个数k,都有正有负,问知否能找到一对数(i,j)(i<=j)保证a [i] - a [i+1] + a [i+2] - a [i+3 ...

  5. 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 i ...

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

    根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...

  7. hdu 5183. Negative and Positive (哈希表)

    Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

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

    题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCo ...

  9. hdu 5183(hash)

    传送门:Negative and Positive (NP) 题意:给定一个数组(a0,a1,a2,⋯an−1)和一个整数K, 请来判断一下是否存在二元组(i,j)(0≤i≤j<n)使得 NP− ...

随机推荐

  1. gulp之几个常用插件介绍

    今天给大家分享一篇gulp几款插件的使用 以下代码用到得模块加载‘ const gulp=require("gulp"); const gulpSass=require(" ...

  2. 浅谈es5和es6中的继承

    首先给大家介绍下在es5中构造函数的继承 function A(){ 2 //构造函数A 3 this.name="我是A函数"; 4 } 5 6 A.prototype={ 7 ...

  3. vue-cli 项目安装失败 tunneling socket could not be established, cause=connect ECONNREFUSED

    1.安装vue-cli npm install vue-cli -g 2.初始化项目 vue init webpack project 此时报错:vue-cli · Failed to downloa ...

  4. leetcode记录-反转整数

    给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假 ...

  5. 关于快速沃尔什变换(FWT)的一些个人理解

    定义 FWT是一种快速完成集合卷积运算的算法. 它可以用于求解类似 $C[i]=\sum\limits_{j⊗k=i}A[j]*B[k]$ 的问题. 其中⊗代表位运算中的|,&,^的其中一种. ...

  6. CF 868 F. Yet Another Minimization Problem

    F. Yet Another Minimization Problem http://codeforces.com/contest/868/problem/F 题意: 给定一个长度为n的序列.你需要将 ...

  7. pager-taglib分页注意事项

    必须先导包,尤其是 jsp 这种工具类和标签库的

  8. 【JUC源码解析】LinkedBlockingQueue

    简介 一个基于链表的阻塞队列,FIFO的顺序,head指向的元素等待时间最长,tail指向的元素等待时间最短,新元素从队列尾部添加,检索元素从队列头部开始,队列的容量,默认是Integer#MAX_V ...

  9. 【RAC搭建报错】在RAC搭建到grid安装前的检查时,报错

    这种ip的报错,无非是检查防火墙,ip配置的原因 而我防火墙已关闭,ip也没配错 最后的原因是因为我172.16.1.41/42这两个IP选的虚拟机没有配置网段 [grid@rac01 grid]$ ...

  10. arduino蜂鸣器的使用

    一:蜂鸣器的使用 控制要求:模拟救护车响声 实物连接图: 电路原理图: 控制代码: //智慧自动化2018.6.11 ;//设置控制蜂鸣器的数字IO脚 void setup() { pinMode(b ...