您的位置:首页 > 其它

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);
       }
    }
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: