hdu 5183(hash)
传送门:Negative and Positive (NP)
题意:给定一个数组(a0,a1,a2,⋯an−1)和一个整数K, 请来判断一下是否存在二元组(i,j)(0≤i≤j<n)使得 NP−sum(i,j) 刚好为K。这里NP−sum(i,j)=ai−ai+1+ai+2+⋯+(−1)j−iaj。
分析:根据a[i]的i为奇偶进行hash,维护两种前缀和
1)i为奇数开头:sum=a[i]-a[i+1]+a[i+2]...
2)i为偶数开头:sum=a[i]-a[i+1]+a[i+2]...
最后枚举sum[i]时在hash表里找是否存在sum[j]=sum[i]-k即可。
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 1000010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
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;
}
const int MAXN=;
const int HASH=;
struct HASHMAP
{
int head[HASH],next[MAXN],size;
LL state[MAXN];
void init()
{
size=;
memset(head,-,sizeof(head));
}
bool check(LL val)
{
int h=(val%HASH+HASH)%HASH;
for(int i=head[h];~i;i=next[i])
if(state[i]==val)return true;
return false;
}
int insert(LL val)
{
int h=(val%HASH+HASH)%HASH;
for(int i=head[h];~i;i=next[i])
{
if(val==state[i])
return ;
}
state[size]=val;
next[size]=head[h];
head[h]=size++;
return ;
}
}H1,H2;
LL a[N];
int main()
{
int T,n,k,cas=;
T=read();
while(T--)
{
n=read();k=read();
for(int i=;i<n;i++)a[i]=read();
LL sum=;
H1.init();H2.init();
H1.insert();H2.insert();
int flag=;
for(int i=n-;i>=&&!flag;i--)
{
if(i&)sum-=a[i];
else sum+=a[i];
if(i%==)
{
if(H1.check(sum-k))flag=;
}
else
{
if(H2.check(-sum-k))flag=;
}
H1.insert(sum);
H2.insert(-sum);
}
printf("Case #%d: ",cas++);
if(flag)puts("Yes.");
else puts("No.");
}
}
hdu 5183(hash)的更多相关文章
- hdu 1029(hash)
传送门:Ignatius and the Princess IV 题意:给n个数,找出出现次数大于等于(n+1)/2的那个数. 分析:大水题,排个序输出中间那个即可,这里随便写个HASHMAP找出次数 ...
- 哈希(Hash)与加密(Encrypt)相关内容
1.哈希(Hash)与加密(Encrypt)的区别 哈希(Hash)是将目标文本转换成具有相同长度的.不可逆的杂凑字符串(或叫做消息摘要),而加密(Encrypt)是将目标文本转换成具有不同长度的.可 ...
- 数据结构之哈希(hash)表
最近看PHP数组底层结构,用到了哈希表,所以还是老老实实回去看结构,在这里去总结一下. 1.哈希表的定义 这里先说一下哈希(hash)表的定义:哈希表是一种根据关键码去寻找值的数据映射结构,该结构通过 ...
- redis学习-散列表常用命令(hash)
redis学习-散列表常用命令(hash) hset,hmset:给指定散列表插入一个或者多个键值对 hget,hmget:获取指定散列表一个或者多个键值对的值 hgetall:获取所欲哦键值以及 ...
- 【Redis】命令学习笔记——哈希(hash)(15个超全字典版)
本篇基于redis 4.0.11版本,学习哈希(hash)相关命令. hash 是一个string类型的field和value的映射表,特别适合用于存储对象. 序号 命令 描述 实例 返回 HSET ...
- A * B Problem Plus HDU - 1402 (FFT)
A * B Problem Plus HDU - 1402 (FFT) Calculate A * B. InputEach line will contain two integers A and ...
- 《数据结构与算法分析——C语言描述》ADT实现(NO.05) : 散列(Hash)
散列(Hash)是一种以常数复杂度实现查找功能的数据结构.它将一个关键词Key,通过某种映射(哈希函数)转化成索引值直接定位到相应位置. 实现散列有两个关键,一是哈希函数的选择,二是冲突的处理. 对于 ...
- D - 淡黄的长裙 HDU - 4221(贪心)
D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...
- HDU 1880 魔咒词典 (Hash)
魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- vb和vb.net事件机制
学习java事件前,回顾了下vb6和vb.net的事件机制,总结在这里,供对比用. 事件是面对对象中对象间通信的方法.事件发生者(又叫事件源)发生一个事件时,通过发送一条消息,给事件接受者(事件处理者 ...
- 模式匹配KMP
字符串朴素模式匹配算法的2种实现: //1.朴素的模式匹配算法,用while实现 int StrStr_While(const char* pStr, const char* pSub, int* p ...
- WebView.destroy() called while still attached 的解决的方法
能够如今webView的父组件中删除该webview,然后再Destroy parent.removeView(webView); 然后 webView.removeAllViews(); webVi ...
- E. Riding in a Lift(Codeforces Round #274)
E. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- VS2010 ,工程文件减肥
写一个bat文件来清理VS2010工程文件. 将其放入到工程文件夹下,双击即可自动将sdf,ipch等占用空间很大的文件删除. 这样的方式较之于转移浏览数据库文件的位置而言更为灵活一些,不用特别的设置 ...
- 使用java对执行命令行 或 执行bat文件
public class Hellotianhao { public static void main(String[] args) throws Exception{ System.out.prin ...
- Android 如何引用com.android.internal.R目录下的资源
Android 如何引用com.android.internal.R目录下的资源 项目需求 有一个资源跟系统上的一个资源相同,想要引用它:frameworks/base/core/res/res/dr ...
- 基于Andoird 4.2.2的Account Manager源代码分析学习:AccountManagerService系统服务的添加
从启动说起 Android系统加载时,首先启动init进程,该进程会启动Zygote进程.Zygote进程执行/system/bin/app_process程序.app_process程序在执行中,通 ...
- Leetcode 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
- Unity Interface Serialization-Expose Interface field In Inspector
Unity has some quirks about their inspector, so as a preface they are listed here: If you add a [Ser ...