学到了以邻接表方式建立的hashmap

题意:给你一串数a和一个数k,都有正有负,问知否能找到一对数(i,j)(i<=j)保证a [i] - a [i+1] + a [i+2] - a [i+3]........(-1)^(j-i) a[j] 等于k

题解:想了很久才想出一个方法就是:记录前缀和,利用前缀和可以求所有可能性:对于每次求前缀和psum,psum[i]及其psum[i]-psum[比i小的](就是减去之前每次求出的前缀和)组成的小于n*n/2个数字就是总的可能出现的数(当然要处理一下减去前面的前缀和后不能变成先减a[i])。这样我们就是每次先求前缀和寻找map中是否出现psum[i]-k,再判断psum[i]满足条件后放入map中就好。

但是我们要注意奇数个与偶数个先加的两种不同的情况,分别处理。还有就是直接map查重会超时,我们可以hashmap查询,hash离散化后模拟邻接表插入与查询

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Mod=;
const int Max=;
//纯数字输入
int Scan()
{
char ch=getchar();int x=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct HashMap//模仿邻接表的hashmap
{
int head[Mod],nnext[Max],len;
ll state[Max];
void init()
{
len=;
memset(head,-,sizeof(head));
}
bool Find(ll num)//模拟邻接表建图的查询
{
int key=(num%Mod+Mod)%Mod;
for(int i=head[key];~i;i=nnext[i])
if(state[i]==num)return true;
return false;
}
void Insert(ll num)//模拟邻接表建图
{
if(Find(num))
return;
int key=(num%Mod+Mod)%Mod;
state[len]=num;
nnext[len]=head[key];
head[key]=len++;
return;
}
}hh1,hh2;
int num;
int Solve(int n,int k)
{
ll psum1=0ll,psum2=0ll;//奇数是加法前缀和 偶数是加法前缀和
hh1.init();
hh2.init();
hh1.Insert(0ll);//直接判等于psum
hh2.Insert(0ll);
int flag=;
for(int i=; i<n; ++i)
{
num=Scan();
if(i&)
{
psum1+=num;//奇数个是加法
psum2-=num;//偶数个是减法
}
else
{
if(i)//不能先减
psum1-=num;
psum2+=num;
}
if(i&&hh1.Find(psum1-(ll)k))//通过前缀和与k的差跟之前出现过的前缀和比对,之前出现过就一定可以相加得到k
flag=;
if(hh2.Find(psum2-(ll)k))//HashMap的查询
flag=;
if(i&&!(i&))
hh1.Insert(psum1);//放入HashMap表
if(i&)
hh2.Insert(psum2);
}
return flag;
}
int main()
{
int t,n,k,coun=;
t=Scan();
while(t--)
{
n=Scan();
k=Scan();
if(Solve(n,k))
printf("Case #%d: Yes.\n",++coun);
else
printf("Case #%d: No.\n",++coun);
}
return ;
}

HDU 5183 Negative and Positive (NP) (hashmap+YY)的更多相关文章

  1. HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)

    根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...

  2. 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 ...

  3. hdu 5183 Negative and Positive (NP)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5183 Negative and Positive (NP) Description When give ...

  4. 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\ ...

  5. 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 ...

  6. HDU 5183 Negative and Positive (NP) 前缀和+哈希

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5183 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  7. hdu 5183. Negative and Positive (哈希表)

    Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  8. [HDOJ 5183] Negative and Positive (NP) 【Hash】

    题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCo ...

  9. hdu-5183-Negative and Positive (NP)(hash模板)

    题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...

随机推荐

  1. hfut 1287

    http://acm.hfut.edu.cn/OnlineJudge/ 中文题. 区间线段树,需要剪枝.n的大小有问题. #include <iostream> #include < ...

  2. 32位Win7下安装与配置PHP环境(一)

    运行PHP网站,主要需要安装.配置三个软件,Apache.PHP和MySQL.如果需要编辑调试PHP程序,还要安装一个编辑调试软件. 一. Apache Apache是和IIS类似的一个软件,是运行在 ...

  3. 《从零开始学Swift》学习笔记(Day 52)——Cocoa错误处理模式

    原创文章,欢迎转载.转载请注明:关东升的博客 Swift错误处理模式,在Swift 1.x和Swift 2.0是不同的两种模式. Swift 1.x代码错误处理模式采用Cocoa框架错误处理模式,到现 ...

  4. 《从零开始学Swift》学习笔记(Day 20)——函数中参数的传递引用

    原创文章,欢迎转载.转载请注明:关东升的博客 参数的传递引用 类是引用类型,其他的数据类型如整型.浮点型.布尔型.字符.字符串.元组.集合.枚举和结构体全部是值类型. 有的时候就是要将一个值类型参数以 ...

  5. box-shadow阴影效果

    .shadowBox{ -moz-box-shadow:5px 5px 5px #A9A9A9; -webkit-box-shadow:5px 5px 5px #A9A9A9; box-shadow: ...

  6. COM 组件创建实例失败,原因是出现以下错误: c001f011 (Microsoft.SqlServer.ManagedDTS)

    从 IClassFactory 为 CLSID 为 {AA40D1D6-CAEF-4A56-B9BB-D0D3DC976BA2} 的 COM 组件创建实例失败,原因是出现以下错误: c001f011. ...

  7. jqprint 打印网页 jQuery print plugin

    ref://jQuery print plugin <!DOCTYPE html> <html lang="en"> <script src=&quo ...

  8. OVF and OVA

    最近测试的东西有关于ovf 和ova等相关用例,在网上找了点内容摘抄了下来. 一.什么是OVF文件 开源虚拟化格式OVF文件是一种开源的文件规范,它描述了一个开源.安全.有效.可拓展的便携式虚拟打包以 ...

  9. 剑指offer 面试19题

    面试19题: 题目:正则表达式匹配 题:请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是 ...

  10. windows下客户端开发hdf--环境搭建

    1.引入依赖 <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop- ...