HDU 2141 Can you find it? <二分>


Can you find it?

Time Limit:3000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

 

 

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
 

這個題稍微不注意就有點小坑出現,也不知道是什么原因,最開始自己的數組開小了,OJ上報錯反而是內存超限,導致一直在找其它的錯誤
這里給出兩個AC代碼,思路都是一樣的,運用二分來做

#include<cstdio>
#include<cctype>
#include<iostream>
#include<stack>
#include<map>
#include<cstring>
#include<string>
#include<sstream>
#include<queue>
#include<set>
using namespace std ;
  int a[505],b[505],c[505];
    set<int>sum;
int main()  {
    int l,n,m;
    int cou=1;
    while(~scanf("%d%d%d",&l,&n,&m)){
            sum.clear();
        for(int i=0;i<l;i++)
            scanf("%d",&a[i]);
         
        for(int i=0;i<n;i++)
            scanf("%d",&b[i]);
             for(int j=0;j<m;j++)
            scanf("%d",&c[j]);
          


        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            sum.insert(b[i]+c[j]);
        }

        int t;
        scanf("%d",&t);
        printf("Case %d:\n",cou++);
          while(t--){
            int tp;
            scanf("%d",&tp);
            int flag=0;
            set<int>::iterator f;
            for(int i=0;i<l;i++){
                    f=sum.find(tp-a[i]);//利用find函數來搜索值,其實set集合類的find函數內部就是用二分來實現的,所以想要快速AC的同學,可以使用這種方法.
                    if(f!=sum.end()){
                    flag=1;
                    break;
                    }
            }
            if(!flag)
                printf("NO\n");
            else
                printf("YES\n");
        }


    }
    return 0 ;
}

下面給出正常的二分AC代碼
#include<cstdio>
#include<cctype>
#include<iostream>
#include<stack>
#include<map>
#include<cstring>
#include<string>
#include<sstream>
#include<queue>
#include<set>
#include<algorithm>
using namespace std ;
int a[505],b[505],c[505];
int sum[250005];
int k=0,flag=0;
void bs(int tp,int i)
{
    int l=0,r=k-1;
    while(l<=r)
    {
        int m=(r+l)>>1;
        if(sum[m]+a[i]==tp)
        {
            //printf("%d\n",m);
            flag=1;
            break;
        }
        else if(sum[m]+a[i]>tp)
            r=m-1;
        else
            l=m+1;
    }
}
int main()
{
    int l,n,m;
    int cou=1;
    while(~scanf("%d%d%d",&l,&n,&m))
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(sum,0,sizeof(sum));
        for(int i=0; i<l; i++)
            scanf("%d",&a[i]);
        for(int i=0; i<n; i++)
            scanf("%d",&b[i]);
        for(int j=0; j<m; j++)
            scanf("%d",&c[j]);

        k=0;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                sum[k++]=b[i]+c[j];
            }

        sort(sum,sum+k);

        int t;
        scanf("%d",&t);

        printf("Case %d:\n",cou++);
        while(t--)
        {
            int tp;
            scanf("%d",&tp);
            flag=0;
            for(int i=0; i<l; i++)
            {
                bs(tp,i);
            }
            if(!flag)
                printf("NO\n");
            else
                printf("YES\n");
        }


    }
    return 0 ;
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com