Can you find it?

来源:互联网 发布:历峰集团 知乎 编辑:程序博客网 时间:2024/05/01 11:31

Can you find it?

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


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 31 2 31 2 31 2 331410
 

Sample Output
Case 1:NOYESNO


值得研究!

现在是看了结题报告,就能敲出来。。。

第二次做,竟然题目都读不懂了,还是写出来吧,

题目大意:给出ai,bi,ci然后给出s,判断是否有相应的ai,bi,ci使得和为s

做法:由于给出3种可选数字,如果暴力必然会三层循环,导致超时,观察第一个例子和为1没有,说明每个aibici都要出现

因此,可以将ai+bi的所有结果,算出来作为新的si,与ci判断是否有某个和

因此可以判断si中是否有数字:和-ci,这样就是赤裸裸的二分查找了

还有一点有序性,因此一定要给si排序

//title:2141 Can you find it?

//问题分类:二分,先将ab相加为s,判断 s[i]+c[j]?=x
//即用二分查询找s中是否存在x-c[j]
//时间复杂度:500(查找次数) *log2(250000)(2的20次为100万),约等于500*20
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main(){
    freopen("in.txt","r",stdin);
    int a,b,c,zu,count=0,x;
    int aa[505],bb[505],cc[505],s[250500];
    while(scanf("%d%d%d",&a,&b,&c)!=EOF){
        count++;
        printf("Case %d:\n",count);
        memset(aa,0,sizeof(aa));
        memset(bb,0,sizeof(bb));
        memset(cc,0,sizeof(cc));
        memset(s,0,sizeof(s));
        for(int i=0;i<a;i++){
            scanf("%d",&aa[i]);
        }
        for(int i=0;i<b;i++){
            scanf("%d",&bb[i]);
        }
        for(int i=0;i<c;i++){
            scanf("%d",&cc[i]);
        }
        int k=0;
        for(int i=0;i<a;i++){
            for(int j=0;j<b;j++){
                s[k++]=aa[i]+bb[j];
            }
        }
        sort(s,s+k);
        scanf("%d",&zu);
        while(zu--){
            scanf("%d",&x);
            int flag=0;//表示没有找到
            for(int i=0;i<c;i++){
                int ans=x-cc[i];
                int top=k-1,base=0,mid=0;
                while(top>=base){
                    mid=(base+top)/2;
                    if(ans<s[mid]){
                        top=mid-1;
                    }else if(ans>s[mid]){
                        base=mid+1;
                    }else{
                        flag=1;
                        printf("YES\n");
                        break;
                    }
                }
                if(flag){
                    break;
                }
            }
            if(flag==0){
                printf("NO\n");
            }
            
            
            
        }
    
    }
    return 0;
}
0 0