首页 > 其他 > 详细

HDU 5183 Negative and Positive (NP)

时间:2017-10-02 20:39:31      阅读:218      评论:0      收藏:0      [点我收藏+]

HDU 5183 Negative and Positive (NP)

思路:维护一下前缀和,从后往前向set里面插入前缀和,然后查找sum[i-1]+(-1)i+1*k在不在set里面。

代码(快读+set):

1466ms险过,用hash表应该快一点。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) 

inline int read(){  
   int s=0,w=1;  
   char ch=getchar();  
   while(ch<0||ch>9){if(ch==-)w=-1;ch=getchar();}  
   while(ch>=0&&ch<=9) s=s*10+ch-0,ch=getchar();  
   return s*w;  
} 

const int N=1e6+5;
ll a[N];
ll sum[N]={0};
set<ll>s;
int main()
{
    /*ios::sync_with_stdio(false);
    cin.tie(0);*/
    int t,cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        cnt++;
        int n,k;
        scanf("%d %d",&n,&k);
        getchar();
        for(int i=1;i<=n;i++)a[i]=read();
        for(int i=1;i<=n;i++)sum[i]=sum[i-1]+(i%2?a[i]:-a[i]);
        /*for(int i=1;i<=n;i++)cout<<a[i]<<‘ ‘;
        cout<<endl;*/
        bool f=false;
        for(int i=n;i>=1;i--)
        {
            s.insert(sum[i]);
            if(i&1)
            {
                if(s.find(sum[i-1]+k)!=s.end())
                {
                    f=true;
                    break;
                }
            }
            else
            {
                if(s.find(sum[i-1]-k)!=s.end())
                {
                    f=true;
                    break;
                }
            }
        }
        printf("Case #%d: ",cnt);
        if(f)puts("Yes.");
        else puts("No.");
        s.clear();
    }
    return 0;
}

 

HDU 5183 Negative and Positive (NP)

原文:http://www.cnblogs.com/widsom/p/7622243.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!