1002 Positive and Negative

题目链接

Negative and Positive (NP)


Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1161    Accepted Submission(s): 59


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]=a[0]-a[1]+a[2]-a[3]+…+(-1)^i*a[i]
枚举结尾i,然后在hash表中查询是否存在sum[i]-K的值。
如果当前i为奇数,则将sum[i]插入到hash表中。
上面考虑的是从i为偶数为开头的情况。
然后再考虑以奇数开头的情况,按照上述方法再做一次即可。
不同的是这次要维护的前缀和是sum[i]=-(a[0]-a[1]+a[2]-a[3]+…+(-1)^i*a[i])
I为偶数的时候将sum[i]插入到hash表。
总复杂度比o(n)大一点
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
ll sum[1000000+5];
int T,n,k,x;
bool success;
set<ll>es;

/////////////////////G++条件下快速输入输出模板
template<class T>
inline bool scan_d(T &ret) {
    char c;
    bool sgn;
    if(c=getchar(),c==EOF)return 0;
    while(c!='-'&&(c<'0'||c>'9'))c=getchar();
    sgn=(c=='-');
    ret=sgn?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9')ret=ret*10+(c-'0');
    if(sgn)ret=-ret;
    return 1;
}
////////////////////////////sum[i]-sum[j]=k  ->   sum[i]-k=sum[j],进行枚举


int main() {
    //freopen("D://imput.txt","r",stdin);
    scanf("%d",&T);
    for(int i=1; i<=T; i++) {
        success=false;
        es.clear();
        scanf("%d%d",&n,&k);
        sum[0]=0;
        for(int j=1; j<=n; j++) {
            scan_d(x);
            if(j&1) {
                sum[j]=sum[j-1]+x;
            } else {
                sum[j]=sum[j-1]-x;
            }
        }
        for(int j=n; j>0; j--) {
            es.insert(sum[j]);
            if(j&1) {//奇偶不同导致他们的开头数字的正负不同,之后的数字和则会相反
                if((j&1) && es.find(sum[j-1]+k)!=es.end()||!(j&1) && es.find(sum[j-1]-k)!=es.end()) {
                    success=true;
                    break;
                }
            }
        }
        printf("Case #%d: ",i);
        if(success) {
            printf("Yes.\n");
        } else {
            printf("No.\n");
        }
    }
    return 0;
}


你可能感兴趣的:(1002 Positive and Negative)