题目链接:

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. [已解决] 设置无效字段为-1 时,引发的 DataGridView DataError

    由于问题一句话说不清.所以标题里也没写明白.大概情况是这样.我一直使用dotNetBar控件来做UI,其中的DataGridView很常用.过去一直有发现DataError的错误,通过截取消息暂时屏蔽 ...

  2. js数组定义和方法 (包含ES5新增数组方法)

    数组Array 1. 数组定义 一系列数据的集合成为数组.数组的元素可以为任何类型的数据(包括数组,函数等),每个元素之间用逗号隔开,数组格式:[1,2,3]. 2. 数组创建方式 (1) 字面量方法 ...

  3. 第五章 C程序结构

    一.数值类型 1.实数常量的表示:3.5(双精度),3.5f(单精度),3.5L(长双精度) 2.整数常量:char字符常量(‘a’,‘b’,‘0’)当做一个整型常量参加运算 3.数字字符与英文字母字 ...

  4. npm audit fix

    执行npm install 出现如下提醒   added 253 packages from 162 contributors and audited 1117 packages in 42.157s ...

  5. Hadoop HA 高可用集群搭建

    一.首先配置集群信息 vi /etc/hosts 二.安装zookeeper 1.解压至/usr/hadoop/下 .tar.gz -C /usr/hadoop/ 2.进入/usr/hadoop/zo ...

  6. C语言顺序队列

    顺序队列是一种只能在一头进和另一头出的数据结构,所以结构体里设2个指针分别指向头部和尾部,用数组来存储数据. #define MAXSIZE 1024 typedef int elemtype; ty ...

  7. Docker搭建NSQ实时分布式消息集群

    NSQ是一个基于Go语言的分布式实时消息平台,它基于MIT开源协议发布,代码托管在GitHub.NSQ可用于大规模系统中的实时消息服务,并且每天能够处理数亿级别的消息,其设计目标是为在分布式环境下运行 ...

  8. POJ_1679_The Unique MST(次小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  9. 20155207 2016-2017-2 《Java程序设计》第十周学习总结

    20155207 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 狭义的网络编程范畴 ...

  10. 20155211 2016-2017-2 《Java程序设计》第五周学习总结

    20155211 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 这周学习了第八章和第九章的内容.第八章和第九章主要就是介绍一些类的应用,我先把教材上的内容看 ...