hdu 5183-Negative and Positive (NP)
来源:互联网 发布:淘宝如何签署食品协议 编辑:程序博客网 时间:2024/05/21 10:53
题目:大概意思就是给定一个序列a[0],a[1]....a[n-1]和一个整数k,问是否有这样的两个下标是的NP-Sum(i,j)=k,这里NP-Sum(i,j)=a[i]-a[i+1]+a[i+2]-...+(-1)^(j-1)*a[j]。
思路:我们可以维护这个序列的后缀和(前缀也是可以的),然后枚举sum[i]查看在set表中是否存在sum[j]=sum[i]-k。
这里要分成i为奇数时的计算和偶数时的计算(原因是我们只维护一次sum的值,或+-+-+的形式,或-+-+-的形式),具体看代码:
#include <iostream>#include <cstdio>#include <set>#include <algorithm>using namespace std;typedef long long LL;inline LL Scan(){ char ch; LL flag=1,Num=0; while((ch=getchar())!='-'&&(ch<'0'||ch>'9')); if(ch=='-') flag=-1; else if(ch>='0'&&ch<='9') Num=ch-'0'; while((ch=getchar())>='0'&&ch<='9') { Num=Num*10+ch-'0'; } return flag*Num;}LL nt[1000010],k,sum=0;set<LL> Bt;int main(){ Bt.clear(); int Cas; scanf("%d",&Cas); for(int ca=1;ca<=Cas;ca++) { int n; bool flag=false; scanf("%d %I64d",&n,&k); getchar(); for(int i=1;i<=n;i++) { nt[i]=Scan(); } sum=0; Bt.clear(); Bt.insert(0); for(int i=n;i>0;i--) { if(i%2) sum-=nt[i]; else sum+=nt[i]; if(i%2==0) { if(Bt.find(sum-k)!=Bt.end()) { flag=true; break; } } else { if(Bt.find(sum+k)!=Bt.end()) { flag=true; break; } } Bt.insert(sum); } printf("Case #%d: %s\n",ca,flag?"Yes.":"No."); } return 0;}
时间卡的比较死,飘过的~~
0 0
- hdu 5183 Negative and Positive (NP)
- HDU 5183 Negative and Positive (NP)
- HDU 5183 - Negative and Positive (NP)(Hash)
- hdu 5183 Negative and Positive (NP)
- HDU 5183 Negative and Positive (NP) (hash)
- hdu 5183-Negative and Positive (NP)
- hdu 5183 Negative and Positive (NP)
- hdu 5183 Negative and Positive (NP) (hash大法)
- [HDU 5183][BestCoder #32]Negative and Positive (NP) (STL模板)
- hdu 5183-Negative and Positive (NP) (hash+输入外挂)
- Negative and Positive (NP) (hdu 5183 set+输入外挂)
- hdu 5183 Negative and Positive (NP)【fast IO +哈希表】
- hdu 5183 Negative and Positive (NP)(hash,思路)
- 【HDU5183】Negative and Positive (NP)
- HDU 5183 Negative and Positive (NP) (set + 读入外挂 乱搞)
- HDU 5183 Negative and Positive (NP)(哈希 hash)——BestCoder Round #32
- hdu5138 Negative and Positive (NP) 快速读入
- Negative and Positive (NP) HDU5183 && Bestcoder#32
- 第四章
- Git学习
- path环境变量的作用
- Java中哈希值的作用
- typedef函数指针用法
- hdu 5183-Negative and Positive (NP)
- spoj 1811 Longest Common Substring (第一道后缀自动机)
- 第五章
- C++之单继承
- UNIX与LINUX的区别
- 110警察叔叔训练智力
- 第六章
- JavaScript--JavaScript 简介
- 黑马程序员——Java IO流 4——功能流