HDU 5183 Negative and Positive (NP) 前缀和+哈希
题目链接:
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) 前缀和+哈希的更多相关文章
- 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\ ...
- hdu 5183 Negative and Positive (NP)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5183 Negative and Positive (NP) Description When give ...
- 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 ...
- 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 ...
- 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 ...
- HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)
根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...
- hdu 5183. Negative and Positive (哈希表)
Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- [HDOJ 5183] Negative and Positive (NP) 【Hash】
题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCo ...
- hdu 5183(hash)
传送门:Negative and Positive (NP) 题意:给定一个数组(a0,a1,a2,⋯an−1)和一个整数K, 请来判断一下是否存在二元组(i,j)(0≤i≤j<n)使得 NP− ...
随机推荐
- 在node中使用jwt签发与验证token
1.什么是token token的意思是“令牌”,是服务端生成的一串字符串,作为客户端进行请求的一个标识. token是在服务端产生的.如果前端使用用户名和密码向服务端发送请求认证,服务端认证成功,那 ...
- Python-逻辑运算
1 or 3>2 and 4<5 or 6 and 2<7
- linux打patch简单示例
在项目中,有些模块是开源的,没有源码或者不能改动源码,想要修复.优化里面的Bug,这时就需要用到patch了. 1. 生成patch 制作补丁有两种法法,diff和quilt. 1.1 di ...
- hadoop伪分布式组件安装
一.版本建议 Centos V7.5 Java V1.8 Hadoop V2.7.6 Hive V2.3.3 Mysql V5.7 Spark V2.3 Scala V2.12.6 Flume V1. ...
- 第一节 如何用Go实现单链表
一.概念介绍 下面这副图是我们单链表运煤车队. 每节运煤车就是单链表里的元素,每节车厢里的煤炭就是元素中保存的数据.前后车通过锁链相连,作为单链表运煤车,从1号车厢开始,每节车厢都知道后面拉着哪一节车 ...
- VC6无法生成Release版本程序
在工程设置,将Setting for后面的选项改为Win32 Release.然后重新编译.结果仍然没有生成release,而且打开设置时,依然是Win32 Debug. 解决办法,在VC6.0的工具 ...
- 纪中OJ 2019.01.25【NOIP提高组】模拟 B 组 T2 数字对
声明 数字对 Time Limits: 2000 ms Memory Limits: 262144 KB Description 小 H 是个善于思考的学生,现在她又在思考一个有关序列的问题. ...
- Java基础——NIO(一)通道与缓冲区
一.概述 1.什么是NIO NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多. 在Java ...
- Hadoop学习总结之Map-Reduce的过程解析111
一.客户端 Map-Reduce的过程首先是由客户端提交一个任务开始的. 提交任务主要是通过JobClient.runJob(JobConf)静态函数实现的: public static Runnin ...
- 20155310 2016-2017-2 《Java程序设计》第四周学习总结
20155310 2016-2017-2 <Java程序设计>第四周学习总结 一周两章新知识的自学与理解真的是很考验和锻炼我们,也对前面几章我们的学习进行了检测,遇到忘记和不懂的知识就再复 ...