学到了以邻接表方式建立的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. SCSS详解

    SCSS入门 CSS预处理器 定义了一种新的专门的编程语言,编译后成正常的CSS文件.为CSS增加一些编程的特性,无需考虑浏览器的兼容问题,让CSS更加简洁,适应性更强,可读性更佳,更易于代码的维护等 ...

  2. python学习【第四篇】python函数 (一)

    一.函数的介绍 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以 ...

  3. [Node.js] require背后的故事

    前言 熟悉Node.js的肯定对下面的代码熟悉 var http = require('http'); 这段代码很好理解,就是加载一个http模块.但是你有没有想过为什么要这么写?这其中的缘由是什么呢 ...

  4. Using InfluxDB in Grafana,influxDB在grafana中使用

    grafana带有功能丰富的数据源插件influxDB.支持丰富的查询编辑器.注释和templating(模版)查询. 增加数据源(Adding the data source) 点击顶部Grafan ...

  5. 进程 query foreach

    http://php.net/manual/en/pdo.query.php PDO::query() executes an SQL statement in a single function c ...

  6. 接口测试工具 — jmeter(基本使用)

    1.打开jemeter(bin目录下jemter.bat) 2.基本操作

  7. 我的Android进阶之旅------>Android项目运行报java.lang.NoClassDefFoundError错误的解决办法

    今天在运行一个Android项目的时候,报了以下错误: D/AndroidRuntime( 3859): Shutting down VM E/AndroidRuntime( 3859): FATAL ...

  8. 斯坦福大学Andrew Ng - 机器学习笔记(6) -- 聚类 & 降维

    大概用了一个月,Andrew Ng老师的机器学习视频断断续续看完了,以下是个人学习笔记,入门级别,权当总结.笔记难免有遗漏和误解,欢迎讨论. 鸣谢:中国海洋大学黄海广博士提供课程视频和个人笔记,在此深 ...

  9. jquerymobile模板

    <!DOCTYPE html> <html> <head> <title>Page Title</title> <meta name= ...

  10. 001-web基本程序搭建

    一.IDEA创建项目 1.基本项目创建 1.1.基本步骤 1.Create New Project [File→New→Project]→New Project 2.maven→group.artif ...