H电-Problem Archive-2141-Can you find it?

Can you find it?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 12153    Accepted Submission(s): 3157


Problem Description
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.
 

Input
There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.
 

Output
For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO".
 

Sample Input
  
  
3 3 3 1 2 3 1 2 3 1 2 3 3 1 4 10
 

Sample Output
  
  
Case 1: NO YES NO
 

Author
wangye
 

Source

HDU 2007-11 Programming Contest






循环做二分,如果三者直接相加再进行sort排序,排列组合是500×500×500,肯定会超时,所以先加和前两个数组500×500,后一个通过循环二分查找来实现加和。

其他的就是输入输出之类的问题了。






S1:

#include<stdio.h>
#include<algorithm>

using namespace std;

int A[521], B[521], f[251111], g, C[521], L, N, M, i, j, k, X, S;

int binary(int X,int M,int g)
{
    for(int h = 1; h <= M; h++)
    {
        int l = 1, r = g;
        while(l <= r)
        {
            int mid = (l+r)/2;
            if(X > C[h] + f[mid]) l = mid + 1;
            else if(X < C[h] + f[mid]) r = mid - 1;
            else return 1;
        }
    }
    return 0;
}

int main()
{
    int tt = 0;
    while(scanf("%d%d%d", &L, &N, &M) != EOF)
    {
        tt++;
        for(i = 1; i <= L; i++)
            scanf("%d", &A[i]);
        for(j = 1; j <= N; j++)
            scanf("%d", &B[j]);
        g = 1;
        for(i = 1; i <= L; i++)
            for(j = 1; j <= N; j++)
        {
            f[g] = A[i] + B[j];
            g++;
        }
        sort(f + 1, f + g);
        for(k = 1; k <= M; k++)
            scanf("%d", &C[k]);
g--;
        scanf("%d", &S);
        printf("Case %d:\n", tt);
        for(i = 1; i <= S; i++)
        {
            scanf("%d", &X);
            printf(binary(X, M, g) ? "YES\n" : "NO\n");
        }
    }
    return 0;
}






sort(a + 0,a  +  l + 1);第一位是数组名+某数,加0为a[0],是排序开始的元素。第二位是数组名加结束排序的位置(l)再加一,不是排序结束的位置,而是排序真实结束位置的后一位。所以也可以对数组C排序,虽然并没有任何代码上的帮助。






S2:

#include<stdio.h>
#include<algorithm>

using namespace std;

int A[521], B[521], f[251111], g, C[521], L, N, M, i, j, k, X, S;

int binary(int X,int M,int g)
{
    for(int h = 1; h <= M; h++)
    {
        int l = 1, r = g;
        while(l <= r)
        {
            int mid = (l+r)/2;
            if(X > C[h] + f[mid]) l = mid + 1;
            else if(X < C[h] + f[mid]) r = mid - 1;
            else return 1;
        }
    }
    return 0;
}

int main()
{
    int tt = 0;
    while(scanf("%d%d%d", &L, &N, &M) != EOF)
    {
        tt++;
        for(i = 1; i <= L; i++)
            scanf("%d", &A[i]);
        for(j = 1; j <= N; j++)
            scanf("%d", &B[j]);
        g = 1;
        for(i = 1; i <= L; i++)
            for(j = 1; j <= N; j++)
        {
            f[g] = A[i] + B[j];
            g++;
        }
        sort(f + 1, f + g);
        for(k = 1; k <= M; k++)
            scanf("%d", &C[k]);
        sort(C + 1, C + M + 1);
g--;
        scanf("%d", &S);
        printf("Case %d:\n", tt);
        for(i = 1; i <= S; i++)
        {
            scanf("%d", &X);
       
                printf(binary(X, M, g) ? "YES\n" : "NO\n");
         
        }
    }
    return 0;
}








但是通过了不代表就是完全正确的,其中虽然保证了每个输入的数据都不超过32位整数(不超出int),但是三个整数相加仍然有可能会超出int,故正式上来讲要用long long型!之所以用int也能对,完全是因为当超出int时,虽然三者相加超出了int,但是X不会超出int故实际上超出int的部分没有对代码造成影响(就算X能超出int,输进去的时候就是负数,三者加和也是负数~~~依然能够查询到)。






S3:

#include<stdio.h>
#include<algorithm>

using namespace std;

long long A[521], B[521], f[251111], g, C[521], L, N, M, i, j, k, X, S;

long long binary(long long X,long long M,long long g)
{
    for(long long h = 1; h <= M; h++)
    {
        long long l = 1, r = g;
        while(l <= r)
        {
            long long mid = (l+r)/2;
            if(X > C[h] + f[mid]) l = mid + 1;
            else if(X < C[h] + f[mid]) r = mid - 1;
            else return 1;
        }
    }
    return 0;
}

int main()
{
    long long tt = 0;
    while(scanf("%I64d%I64d%I64d", &L, &N, &M) != EOF)
    {
        tt++;
        for(i = 1; i <= L; i++)
            scanf("%I64d", &A[i]);
        for(j = 1; j <= N; j++)
            scanf("%I64d", &B[j]);
        g = 1;
        for(i = 1; i <= L; i++)
            for(j = 1; j <= N; j++)
        {
            f[g] = A[i] + B[j];
            g++;
        }
        sort(f + 1, f + g);
        for(k = 1; k <= M; k++)
            scanf("%I64d", &C[k]);
        sort(C + 1, C + M + 1);
g--;
        scanf("%I64d", &S);
        printf("Case %d:\n", tt);
        for(i = 1; i <= S; i++)
        {
            scanf("%I64d", &X);
            if (X < (f[1] + C[1]) || X > (f[g] + C[M]))
            {
                printf("NO\n");
            }
            else
            {
                printf(binary(X, M, g) ? "YES\n" : "NO\n");
            }
        }
    }


    return 0;
}




if else是发现三者相加其实超过了int的关键点~~~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值