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− ...
随机推荐
- Overload and Override without Overwrite - Java
Override(覆盖/覆写): 子类Override父类中的函数(方法).Overload(重载): 同一个类中包含多个同名的函数(方法), 但各个函数的参数列表不同. Override和Overl ...
- Linux进程优先级查看及修改
进程cpu资源分配就是指进程的优先权(priority).优先权高的进程有优先执行权利.配置进程优先权对多任务环境的Linux很有用,可以改善系统性能.还可以把进程运行到指定的CPU上,这样一来,把不 ...
- asp.net 移除Server, X-Powered-By, 和X-AspNet-Version头
我们在开发Asp.net中,最后部署在IIS上. 然后发送HTTP请求,返回的HTTP头中包含Server, X-Powered-By, 和 X-AspNet-Version信息. 这些信息有时给攻击 ...
- ERROR 2003 (HY000): Can't connect to MySQL server on "" (113)
服务器为centos6. 这个原因是因为防火墙的问题 在mysql服务端执行 service iptables stop chkconfig iptables off #永久关闭防火墙 看情况执行 然 ...
- Linux Shell编程 test命令
概述 test 命令是Shell 脚本中用来进行条件判断的. test命令示例 按照文件类型进行判断 测试选项 作 用 -b 文件 判断该文件是否存在,并且是否为块设备文件(是块设备文件为真) -c ...
- Python编程-编码、文件处理、函数
一.字符编码补充知识点 1.文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就打开了启动了一个进程,是在内存中的,所以在编辑器编写的内容也都是存放与内存中的,断电后 ...
- 登陆weblogic后页面控制台卡主
输入http://localhost:7001/console进入控制页面,能登陆进去,但是登陆进去后页面就马上卡死,可以看到页面头部,其余都显示不出来. 重启后启动访问,能够正常进入,关闭weblo ...
- Scrapy安装方法
Scrapy安装在Python2.7环境下 1.配置环境变量: 2.安装基础软件 4个(64位系统) 安装twisted: C:\Users\Administrator>pip install ...
- jupyter && ipython notebook简介
2017-08-19 最近用了一下 ipython notebook 也就是 jupyter,这里有一个介绍还不错: http://www.cnblogs.com/howiewang/p/jupyte ...
- js替换字符串中的数字或非数字
替换字符串中的数字 var text = "abc123"; text=text.replace(/[0-9]/ig,""); 此时得到的text为" ...