hdu 5183 Negative and Positive (NP)【fast IO +哈希表】
2016-02-01 16:43
337 查看
Negative and Positive (NP)
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3189 Accepted Submission(s): 851
Problem Description
When given an array (a0,a1,a2,⋯an−1)
and an integer K,
you are expected to judge whether there is a pair
(i,j)(0≤i≤j<n)
which makes that NP−sum(i,j)
equals to K
true. Here NP−sum(i,j)=ai−ai+1+ai+2+⋯+(−1)j−iaj
Input
Multi test cases. In the first line of the input file there is an integerT
indicates the number of test cases.
In the next 2∗T
lines, it will list the data for each test case.
Each case occupies two lines, the first line contain two integers
n
and K
which are mentioned above.
The second line contain (a0,a1,a2,⋯an−1)separated
by exact one space.
[Technical Specification]
All input items are integers.
0<T≤25,1≤n≤1000000,−1000000000≤ai≤1000000000,−1000000000≤K≤1000000000
Output
For each case,the output should occupies exactly one line. The output format is Case #id: ans, here id is the data number starting from 1; ans is “Yes.” or “No.” (without quote) according to whether you can find(i,j)
which makes PN−sum(i,j)
equals to K.
See the sample for more details.
Sample Input
2 1 1 1 2 1 -1 0
Sample Output
Case #1: Yes. Case #2: No. HintIf input is huge, fast IO method is recommended.
寻找sum(i,j)==k,我们这里可以sum(i,n-1),然后寻找到sum(j,n-1)使得sum(i,n-1)-sum(j,n-1)==k就满足了条件
这里就相当于sum(i,j)==k
这道题是卡常数的题目,所以被迫接触了哈希表,和读入外挂~也是学到了东西的~
这里直接上代码:
#include<stdio.h> #include<string.h> using namespace std; #define ll long long int const int maxn=1000010; const int hash=1000007; int a[12121212]; long long int read() { long long int res = 0; char ch; long long int flag = 0; if((ch = getchar()) == '-') //判断正负 flag = 1; else if(ch >= '0' && ch <= '9') //得到完整的数 res = ch - '0'; while((ch = getchar()) >= '0' && ch <= '9' ) res = res * 10 + ch - '0'; return flag ? -res : res; } struct hashmap { ll a[maxn]; int head[hash]; int next[maxn]; int size; void init() { memset(head,-1,sizeof(head)); size=0; } bool find(ll val) { int tmp=(val%hash+hash)%hash; for(int i=head[tmp];i!=-1;i=next[i]) { if(val==a[i])return true; } return false; } void add(ll val) { int tmp=(val%hash+hash)%hash; if(find(val))return ; a[size]=val; next[size]=head[tmp]; head[tmp]=size++; } }h1,h2; int main() { int t,n,kase=0,k; t=read(); while(t--) { n=read(); k=read(); for(int i=0;i<n;i++) { a[i]=read(); } h1.init(); h2.init(); h1.add(0); h2.add(0); int flag=0; ll sum=0; for(int i=n-1;i>=0&&flag==0;i--) { if(i%2==1)sum-=a[i]; else sum+=a[i]; if(i%2==1) { if(h2.find(-sum-k))flag=1; } else { if(h1.find(sum-k))flag=1; } h1.add(sum); h2.add(-sum); } if(flag==1) { printf("Case #%d: Yes.\n",++kase); } else { printf("Case #%d: No.\n",++kase); } } }
相关文章推荐
- WC2016随记
- Zoho Survey:如何提高你的问卷反馈率
- wpf 命名空间中不存在
- sublime text 3 添加 javascript 代码片段 ( snippet )
- Yii2所提倡的配置管理方案
- hadoop格式化报错:"May not run daemons as root. Please specify HADOOP_NAMENODE_USER"的解决办法
- USB集腋成裘:从Linux PC角度看sys.usb.config为adb时的Android设备
- jquery常用事件
- 【Flash逆向调试之三】几个比较有用的注入代码
- Storm搭建
- 权限系统设计
- HDU1213并查集
- 启动rsync服务的脚本并能用chkconfig管理
- Codeforces Round #341 (Div. 2) CF621B. Wet Shark and Bishops(排列组合+对角线规律)
- 3 属性与实例变量
- iOS开发 根据某个日期计算是星期几
- C语言初学之fscanf()函数理解
- ORACLE的一些常用操作
- java获取properties配置文件中的信息
- hdu 2095 find your present (2)