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-a2+a3....+/- an
枚举起点法: 设起点为x,实际是枚举x-1,分两种情况:
1.起点x为奇,那么就看有没有a[j]-a[x-1] = K的,即a[j] = a[x-1]+K。因为奇数位置的ai数符为正。
2.起点x为偶,那么就看有没有a[j]-(-K) = a[x-1],即a[j] = a[x-1]-K。因为偶数位置ai数符为负,即x到j这一段的数是负的 选x为起点的x到j的这一段和,所以中间实际上是-K。
每次将sum[i]标记为出现过。
只需要一个hashmap即可。
由于枚举到一个起点x,需要判断a[j](j>x)是否出现,所以要逆序枚举。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define Mod 1000000007
#define lll __int64
#define ll long long
using namespace std;
#define N 1000007 lll sum[N];
const unsigned long long SMod=;
struct hashmap{
struct Edge
{
long long num;
int next;
};
Edge edge[*N];
int countedge;
int head[SMod+]; void init()
{
memset(head,-,sizeof(head));
countedge=;
} void addedge(long long num)
{
int start=num%SMod;
edge[countedge].next=head[start];
edge[countedge].num=num;
head[start]=countedge;
countedge++;
} int Find(long long num)
{
int start=num%SMod;
int ind;
for(ind=head[start]; ind!=-; ind=edge[ind].next)
{
if(edge[ind].num==num)break;
}
return ind;
}
}ST; int main()
{
int n,i,j,cs = ,t,x,K;
scanf("%d",&t);
while(t--)
{
ST.init();
scanf("%d%d",&n,&K);
sum[] = ;
for(i=;i<=n;i++) {
scanf("%d",&x);
if(i%) sum[i] = sum[i-] + x;
else sum[i] = sum[i-] - x;
}
ST.addedge(sum[n]);
int tag = ;
for(i=n-;i>=;i--) {
if(i% == && ST.Find(sum[i]+K) != -) { tag = ; break; }
if(i% && ST.Find(sum[i]-K) != -) { tag = ; break; }
ST.addedge(sum[i]);
}
printf("Case #%d: ",cs++);
if(tag) puts("Yes.");
else puts("No.");
}
return ;
}
枚举终点法:
建立两个hashmap,一个记录sum[1],sum[3],...sum[2*cnt+1] (2*cnt+1<=n)即奇数位置是否出现过,另一个记录偶数位置的sum值是否出现过。
枚举终点y的话,起点可能是1~y的任何一个(这里下标从题目中的0~n-1转为了1~n),当起点x=1的时候,这时NP-SUM(x,y) = sum[y], 记为XX。以n=4为例。
那么起点为2的时候整个值就等于 -XX+a1, (-(a1-a2+a3-a4) +a1 = a2-a3+a4))
起点为3的时候整个值等于 XX-sum[2] (a1-a2+a3-a4 - (a1-a2) = a3-a4 )
...以此类推,归为两类 :
1. XX-sum[0] , XX-sum[2] , ... XX-sum[偶数] 是否为K
2. -XX+sum[1], -XX+sum[3], ... -XX+sum[奇数] 是否为K
设他们为K,那么即判断 XX-K在偶数的hashmap中有没有出现, 判断XX+K在奇数的hashmap中有没有出现。
每次将sum[i]加入到对应的hashmap中。
顺序枚举。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define Mod 1000000007
#define lll __int64
#define ll long long
using namespace std;
#define N 1000007 const unsigned long long SMod=;
struct hashmap{
struct Edge
{
long long num;
int next;
};
Edge edge[*N];
int countedge;
int head[SMod+]; void init()
{
memset(head,-,sizeof(head));
countedge=;
} void addedge(long long num)
{
int start=num%SMod;
edge[countedge].next=head[start];
edge[countedge].num=num;
head[start]=countedge;
countedge++;
} int Find(long long num)
{
int start=num%SMod;
int ind;
for(ind=head[start]; ind!=-; ind=edge[ind].next)
{
if(edge[ind].num==num)break;
}
return ind;
}
}mpe,mpo; int main()
{
int n,i,j,cs = ,t,x,K;
scanf("%d",&t);
for(cs=;cs<=t;cs++)
{
mpo.init();
mpe.init();
scanf("%d%d",&n,&K);
lll sum = ;
mpe.addedge();
int tag = ;
for(i=;i<=n;i++) {
scanf("%d",&x);
if(i&) sum += x;
else sum -= x;
if(i&) mpo.addedge(sum);
else mpe.addedge(sum);
if(mpe.Find(sum-K) != -) { tag = ; }
if(mpo.Find(sum+K) != -) { tag = ; }
}
printf("Case #%d: ",cs);
if(tag) puts("Yes.");
else puts("No.");
}
return ;
}
注意:
如果hashmap中的SMod 用宏定义的方式就会T, 用const unsigned long long 就不会。不知道为什么。
hashmap模板借鉴了love_dn的代码。
HDU 5183 Negative and Positive (NP) --Hashmap的更多相关文章
- 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+YY)
学到了以邻接表方式建立的hashmap 题意:给你一串数a和一个数k,都有正有负,问知否能找到一对数(i,j)(i<=j)保证a [i] - a [i+1] + a [i+2] - a [i+3 ...
- 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 (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 (哈希表)
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(hash)
传送门:Negative and Positive (NP) 题意:给定一个数组(a0,a1,a2,⋯an−1)和一个整数K, 请来判断一下是否存在二元组(i,j)(0≤i≤j<n)使得 NP− ...
随机推荐
- Android使用ViewPager实现左右循环滑动及轮播效果
边界的时候会看到一个不能翻页的动画,可能影响用户体验.此外,某些区域性的ViewPager(例如展示广告或者公告之类的ViewPager),可能需要自动轮播的效果,即用户在不用滑动的情况下就能够看到其 ...
- 关于SQL Server 安装程序在运行 Windows Installer 文件时遇到错误
前几日安装sql server2008r2 的时候碰到这个问题: 出现以下错误: SQL Server 安装程序在运行 Windows Installer 文件时遇到错误. Windows Insta ...
- 解决BUG:CS1617: 选项“6”对 /langversion 无效;必须是 ISO-1、ISO-2、3、4、5 或 Default
vs 2015的项目用vs2013,更改.net版本之后,打开会报以下错误,原因是配置文件修改出了问题.已经验证是BUG 你只需要把Web.config换成以前的就好了. https://conn ...
- 如何给Ubuntu12.10 安装Vmware Tools
众所周知在VMware虚拟机中安装好了VMware Tools,才能实现主机与虚拟机之间的文件共享,同时可支持自由拖拽的功能,鼠标也可在虚拟机与主机之前自由移动(而不再用按ctrl+alT释放),而且 ...
- php 文件下载
public function down() { header("Content-type:text/html;charset=utf-8"); $file_name = I('g ...
- EF 分页查询优化
按照通常的方式分页查询至少要查询数据两遍,一个操作是查询总数,另一个是查询数据,这样有些耗时 这里介绍一个基于EF的插件 EntityFramework.Extended,当然这个插件有很多的功能,比 ...
- Web AppBuilder Widget使用共享类库的方式
Web AppBuilder是Esri公司推出的快速WebGIS应用搭建工具,具有以下特性: 不需要编程,快速创建应用 WYSIWYG 交互式应用 支持2D和3D应用 基于ArcGIS API for ...
- 使用NuGet助您玩转代码生成数据————Entity Framework 之 Code First
[前言] 如果是Code First老鸟或者对Entity Framework不感兴趣,就不用浪费时间往下看了. 记得09年第一次接触ORM————Linq2Sql,从此对她的爱便一发不可收拾,一年后 ...
- iOS--UICollectionView(滚动视图)入门
UICollectionView @interface UICollectionView : UIScrollView UICollectionView 和UICollectionViewCon ...
- linux数据误删后,灾难性数据备份与数据还原
一 准备工作 #rm –rf 误删重要数据怎么办? 1. 要冷静,通知停止该服务器一切操作 2. 查看被删除文件所在分区 #mount 3. 将该分区设置为只读 #mount -r -n -o re ...