hdu-5183-Negative and Positive (NP)(hash模板)
题目链接
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long LL; const int MAXN=;
const int HASH=; inline LL read()//输入外挂
{
char ch=getchar();LL x=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
} struct hashmap//建立哈希表
{
LL a[MAXN];
int head[HASH],next[MAXN],size;
void init(){//初始化
memset(head,-,sizeof(head));
size=;
}
bool find(LL val){//查找一个元素是否在哈希表内,一个hash值可能对应多个值,在这几个值中查找
int tmp = (val%HASH + HASH)%HASH;
for(int i = head[tmp];i!=-;i=next[i])
if(val==a[i]) return true;
return false;
}
void add(LL val){//添加元素到哈希表中
int tmp =(val%HASH+HASH)%HASH;
if(find(val)) return;
a[size]=val;
next[size]=head[tmp];//冲突值对应同一个 hash值
head[tmp]=size++;//head保存每个值的下标
}
}h1,h2; LL a[MAXN]; int main()
{
int t,n,cas=,k;
t=read();
while(t--){
n=read();
k=read();
for(int i=;i<n;i++)
a[i]=read();
LL sum=;
h1.init(),h2.init();
h1.add(),h2.add();
bool flag = ;
for(int i=n-;i>=;i--){
if(i&) sum-=a[i];
else sum+=a[i];
if(i%==){
if(h1.find(sum-k)) flag=;
}
else{
if(h1.find(sum+k)) flag=;
}
h1.add(sum);
h2.add(-sum);
if(flag) break;
}
printf("Case #%d: ",cas++);
if(flag)puts("Yes.");
else puts("No.");
}
return ;
}
hdu-5183-Negative and Positive (NP)(hash模板)的更多相关文章
- 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) (手写哈希)
题目链接: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)(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 (NP) 前缀和+哈希
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5183 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- 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 (哈希表)
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− ...
随机推荐
- 3.6.使用STC89C52控制MC20解析GPS的经纬度数据上传到指定服务器
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- KVM虚拟化安装配置
一.KVM的基础配置及安装: 1.查看是CPU否支持虚拟化: [root@oldboy-node1 ~]# grep -E "(vmx|svm)" /proc/cpuinfo vm ...
- ZOJ 3961 Let's Chat 【水】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3961 题意 给出两个人的发消息的记录,然后 如果有两人在连续M天 ...
- event driven model
http://www.jdon.com/eda.html http://blog.csdn.net/gykimo/article/details/9182287 事件代表过去发生的事件,事件既是技术架 ...
- Python学习进程(14)异常处理
本节介绍Python进行异常处理的方式,异常处理机制可以帮助我们调试python程序. (1)异常的简介: 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行 ...
- $《利用Python进行数据分析》学习笔记系列——IPython
本文主要介绍IPython这样一个交互工具的基本用法. 1. 简介 IPython是<利用Python进行数据分析>一书中主要用到的Python开发环境,简单来说是对原生python交互环 ...
- 嵌入式Qt程序启动参数-qws 不需要X11桌面系统
1 背景 通过串口终端启动arm开发板(linux系统)的Qt应用程序,提示: [root@FORLINX6410]# /opt/qt-4.7.1/demos/textedit/textedit s3 ...
- React Native的生命周期解析
在React Native中使用组件来封装界面模块时,整个界面就是一个大的组件,开发过程就是不断优化和拆分界面组件.构造整个组件树的过程. 上张图涵盖了一个组件从创建.运行到销毁的整个过程.大家可以看 ...
- MATLAB更换字体后 中文显示乱码问题
MATLAB的脚本代码默认宋体显示,对于大多数720P的显示器来说,无论是中文还是英文,显示效果都非常差.Windows环境下,Consolas是一种比较理想的英文代码字体. Consolas 字体是 ...
- Android编译系统环境过程初始化分析【转】
本文转载自:http://blog.csdn.net/luoshengyang/article/details/18928789 Android源代码在编译之前,要先对编译环境进行初始化,其中最主要就 ...