HDU 5183 Negative and Positive (NP) (set + 读入外挂 乱搞)

Negative and Positive (NP)

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 502    Accepted Submission(s): 141


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.
 
Source
 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5183

题目大意:问是否存在(i, j,使) NPsum(i,j)=aiai+1+ai+2++(1)jiaj 这个东西算出来的值等于k,存在输出Yes,否则输出No

题目分析:这题时限太死,水过的,Hint里面提示用fast IO也就是读入外挂了,主要做法是处理一下前缀和,第一种是正负正,第二种是负正负,对于要求的k,我们边插边找,找的时候要分奇偶,若i为奇数,则在第一种情况中找,否则在第二种情况中找,找到就退出


#include <cstdio>
#include <cstring>
#include <set>
#define ll long long
using namespace std;
int const MAX = 1e6 + 5;
ll a[MAX], sum[MAX];
set <ll> s;

ll Scan()  
{  
    ll res = 0, ch, 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;  
}  

int main()
{
    int T, n, k;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ca++)
    {
        bool flag = false;
        s.clear();
        memset(sum, 0, sizeof(sum));
        scanf("%d %d", &n, &k);
        getchar();
        for(int i = 1; i <= n; i++)
            a[i] = Scan();
        for(int i = 1; i <= n; i++)
            sum[i] = sum[i - 1] + (i % 2 ? a[i] : -a[i]);
        for(int i = n; i > 0; i--)
        {
            s.insert(sum[i]);
            if(i % 2)
            {
                if(s.find(sum[i - 1] + k) != s.end())
                {
                    flag = true;
                    break;
                }
            }
            else
            {
                if(s.find(sum[i - 1] - k) != s.end())
                {
                    flag = true;
                    break;
                }
            }
        }
        printf("Case #%d: %s.\n", ca, flag ? "Yes" : "No");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值