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]........(-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)的更多相关文章
- HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)
根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...
- 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)
题目连接 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) --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) 前缀和+哈希
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5183 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- 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-Negative and Positive (NP)(hash模板)
题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...
随机推荐
- Android-NDK编译
(2013-12-19 21:48:21 其实一切还是先看看官网的好,乱百度浪费时间.... http://developer.android.com/tools/sdk/ndk/index.htm ...
- 1154 回文串划分(DP+Manacher)
1154 回文串划分 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有多种划分方式. ...
- the core of Git is a simple key-value data store The objects directory stores all the content for your database
w https://git-scm.com/book/en/v1/Git-Internals-Plumbing-and-Porcelain Git is a content-addressable f ...
- jquery.fileDownload plugin: Success msg alert before actual pdf download completed
Currently , I use jquery fileDownload plugin to download multiple pdf that in a list page, which eve ...
- cvs 文件如何解析?
QList<QStringList> RwCvs::readToList(const QString &path, const QString separator) { QList ...
- python 元类metaclass
文章转自:http://www.cnblogs.com/linhaifeng/articles/8029564.html 一 知识储备 exec:三个参数 参数一:字符串形式的命令 参数二:全局作用域 ...
- 设置请求timeout超时
import requests r = requests.get("http://www.cnblogs.com/yoyoketang/", timeout=1) # 设置超时 p ...
- Springboot入门2-配置druid
Druid是Java语言中最好的数据库连接池,在连接池之外,还提供了非常优秀的监控功能. 下面来说明如何在 Spring Boot 中配置使用Druid 1.添加Maven依赖 (或jar包) < ...
- R语言中abline和lines的区别
函数lines()其作用是在已有图上加线,命令为lines(x,y),其功能相当于plot(x,y,type="1")函数abline()可以在图上加直线,其使用方法有四种格式.( ...
- GPS USB驱动串口被占用
1.一般是装了错误的驱动,显示如下 2.实际装好应该是显示的 3.驱动选择,先卸载了上面的virtual驱动,安装下面箭头指向的驱动 这里的卸载很重要,先点设备管理器的--查看--显示隐藏设备, 然后 ...