题目链接:

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. jquery选择器基础

    简单选择器 类 id 元素/标签 * 复合(sel1,sel2)逗号隔开 层次选择器 s1 s2:后代选择器,空格隔开 p>c:子代选择器:不包括孙代及以下 p+next :相邻选择器 p~su ...

  2. docker 入门 (二)基本操作

    这一篇讲docker的基本操作. 请自行安装docker到自己的电脑上. 镜像的获取 要获取镜像,最简单的当然是从仓库去获取,docker的官方网站很不好练,其他的可选项有docker中国官网,阿里镜 ...

  3. day 34线程的其他方法,线程池

    线程的其他方法:  from threading import Thread,current_thread: currrent_thread().getName()  获取线程的名称 current_ ...

  4. elasticsearch启动时提示内存不足错误的解决方法

    Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0 ...

  5. Flume(3)-安装部署

    一. 下载 Flume官网地址 http://flume.apache.org/ 文档查看地址 http://flume.apache.org/FlumeUserGuide.html 下载地址 htt ...

  6. exynos4412—链接脚本复习

    在u-boot下,定义变量, 编译,编译完后  使用arm-linux-nm arm    没有去头的二进制可执行文件 都在BSS段,均为初始化. 打印之后会出算随机值. 目前还处于uboot阶段,如 ...

  7. EDID的简介和解析

    去年对EDID做了一个解析,下面是学习EDID过程中整理的资料. 一.EDID简介 EDID: Extended Display Identification Data (外部显示设备标识数据)--- ...

  8. web前端知识点1

    1. input属于窗体元素,层级显示比flash.其它元素都高.请判断这句话的正确与否. 错误 层级显示优先级: frameset > 表单元素 > 非表单元素 在html中,帧元素(f ...

  9. 浅入tcp

    1.认识TCP tcp协议是传输层协议,它的最主要的3个特点是面向连接.可靠保证.基于字节流.当应用层把数据给tcp层时,注意如果数据大于MSS是要在tcp层进行分段的.tcp协议为了保证不丢包会给每 ...

  10. DIRECT3D状态详解

    Microsoft® Direct3D®设备是一个状态机.应用程序设置光照.渲染和变换模块的状态,然后在渲染时传递数据给它们. 本节描述图形流水线用到的所有不同类型的状态. 渲染状态 取样器状态 纹理 ...