hdu 5183 Negative and Positive (NP)【fast IO +哈希表】

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,an1) and an integer K , you are expected to judge whether there is a pair (i,j)(0ij<n) which makes that NPsum(i,j) equals to K true. Here NPsum(i,j)=aiai+1+ai+2++(1)jiaj
 


Input
Multi test cases. In the first line of the input file there is an integer T indicates the number of test cases.
In the next 2T 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,an1) separated by exact one space.
[Technical Specification]
All input items are integers.
0<T25,1n1000000,1000000000ai1000000000,1000000000K1000000000
 


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 PNsum(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.
Hint
If 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);
       }
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值