学到了以邻接表方式建立的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. log4j日志文件乱码问题的解决方法

    近日在AIX上用log4j打印日志,出现乱码,经过努力解决问题. 症状:在默认语言非中文(或者说默认语言不支持中文的)的Windows.Linux.Unix上,用log4j打印日志,出现乱码,常见的就 ...

  2. python学习【第三篇】基本数据类型

    Number(数字) int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取 ...

  3. Linux 下安装svn

    安装步骤如下: 1.yum install subversion   2.输入rpm -ql subversion查看安装位置,如下图:   我们知道svn在bin目录下生成了几个二进制文件. 输入 ...

  4. shell中${}的使用

    1. 截断功能${file#*/}:       拿掉第一条/及其左边的字符串:dir1/dir2/dir3/my.file.txt${file##*/}:    拿掉最后一条/及其左边的字符串:my ...

  5. api xml database 设计一种数据库

    w 问题 0-新增和读取,可以忽略更新和删除: 1-被请求方的xml dom结构多层且不定,且未来可能增删某些键(dom节点),且键值长度最值可能无法确定: 3-请求过程可能出现异常exception ...

  6. 兼容ie的background-size: cover;

    .bg{ background: url() no-repeat; background-size:cover; filter: progid:DXImageTransform.Microsoft.A ...

  7. 【生产问题】--8KW的数据表导致业务卡顿

    问题描述:业务突然变得巨卡 分析思路: (1)分析用户请求进程:查看是否有长期运行霸占锁的情况,或者进程数量巨多.很明显我这里就是巨多,正常情况一般0~40来个的样子,在业务使用高峰期居然达到了140 ...

  8. 【转载】Java中使用Jedis操作Redis

    1 package com.test; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Li ...

  9. springboot整合 Thymeleaf模板

    首先引入maven jar依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  10. iOS学习之库

    一.什么是库 库是程序代码的集合,是共享程序代码的一种方式. 二.库的分类 根据源代码的公开情况,库可以分为2种类型. 1.开源库 公开源代码,能看到具体实现. 比如,SDWebImage.AFNet ...