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− ...
随机推荐
- iOS 11.4.1 正式版越狱
在 2018 年 Electra 最新能支持到 11.3.1 越狱,很长的一段时间 11.4 只能支持 Beta 版本,临近春节给了我们一个大礼物,终于支持 iOS 11.4-11.4.1,目前 iO ...
- Elasticsearch入门和查询语法分析(ik中文分词)
全文搜索现在已经是很常见的功能了,当然你也可以用mysql加Sphinx实现.但开源的Elasticsearch(简称ES)目前是全文搜索引擎的首选.目前像GitHub.维基百科都使用的是ES,它可以 ...
- laravel artisan 命令列表
5.4版本新增 命令 说明 备注 php artisan make:resource ? 创建api返回格式化资源 >=5.4版本可用 php artisan make:rule ? 创建val ...
- Delphi RAD Server 应用服务基础平台
RAD Server是一个应用服务框架平台,可快速构建和部署应用服务.RAD Server提供自动化的Delphi和C++ REST/ JSON API的 发布与管理.企业数据库集成中间件.智能物联网 ...
- python教程(三)·函数进阶(下)
下半部分果然很快到来,这次介绍函数的更高级用法,装饰器! 函数嵌套 先来说说函数嵌套,python中的函数是可以嵌套的,也就是说可以将一个函数放在另一个函数里面,比如: >>> de ...
- 20155323 2016-2017-2 《Java程序设计》第10周学习总结
20155323 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 狭义的网络编程范畴就是程序 ...
- 20155331 2016-2017-2 《Java程序设计》
20155331 2016-2017-2 <Java程序设计> 教材学习内容总结 理解封装,继承和多态. 封装最简单的理解就是包装,把编译的class文件封装起来,便于管理,还可以设置密码 ...
- 【SQLSERVER】服务挂起解决办法
一. 问题描述:某项SQLSERVER服务,运行状态为“正在挂起更改”,导致该服务无法使用,也不能启动.停止.重新启动. 二.解决方法 方法一:从任务管理器 → 进程 (勾上 显示所有用户进程) → ...
- day3 前奏
1.第1个c语言 编辑---编译----运行 python@ubuntu:~/Desktop/pythons06$ vim -第1个c语言.c #include<stdio.h> int ...
- 【MySQL安装】MySQL5.6在centos6.4上的安装
卸载原来安装的mysql 安装从官网下载的mysql rpm包 发现有依赖,需要先安装libaio包和libnuma包 再装mysql就可以了 安装客户端 安装完成后,启动mysql 但是发现用没有m ...