1. 程式人生 > >HDU 2143 Can you find it?(基礎二分)

HDU 2143 Can you find it?(基礎二分)

i++ php http miss then represent ise for stream

Can you find it?

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


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

Recommend 威士忌 | We have carefully selected several similar problems for you: 2899 2289 1597 1551 2298 今天從基礎開始學起,以為二分挺容易的,但是忽視了題目需要考慮時間復雜度,會不會爆int,等等。 存不存在ai,bi,ci加起來是x。 如果直接枚舉那也太簡單了吧,我就想著,大循環a,小循環b,小小循環對c二分,發現這樣也超時!!! 然後,我就大循環a,小循環對b二分,小小循環對c二分,結果這樣的思路是完全錯的!!! 然後看看題解,要先把a+b的所有可能都存起來放到sum裏,再大循環c,對sum二分。開始還覺得這個思路不是和我一開始的思路差不多嗎?仔細一想,我大循環a,小循環b,一方面會有很多重復的a+b,重復帶入算,費時,另一方面,每輸入一個x,又要大循環啊,小循環b的,很費時。
#include <iostream>
#include <stack>
#include <string.h>
#include <stdio.h>
#include<queue>
#include<algorithm>
#define ll long long
using namespace std;
int a[505];
int b[505];
int c[505];
int sum[500005];
int main()
{
    int L,N,M;
    int k=0;
    while(~scanf("%d %d %d",&L,&N,&M))
    {
        k++;
        for(int i=1;i<=L;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&b[i]);
        }
        for(int i=1;i<=M;i++)
        {
            scanf("%d",&c[i]);
        }
        int n,x;
        printf("Case %d:\n",k);
        scanf("%d",&n);
        int p=1;

        for(int i=1;i<=L;i++)
        {
            for(int j=1;j<=N;j++)
            {
                sum[p++]=a[i]+b[j];
            }
        }
        sort(sum+1,sum+p);
        sort(c+1,c+1+M);
        while(n--)
        {
            scanf("%d",&x);
            bool f=0;
            for(int i=1;i<=M;i++)
            {
                if(sum[1]>x-c[i])//不能寫成sum[1]+c[1]>x,因為可能會爆int
                    break;
                int le=1;int ri=p-1;
                while(le<=ri)
                {
                    int mid=(le+ri)/2;
                    if(sum[mid]==x-c[i])
                    {
                        f=1;
                        break;
                    }
                    else if(sum[mid]>x-c[i])
                    {
                        ri=mid-1;
                    }
                    else if(sum[mid]<x-c[i])
                        le=mid+1;
                }
                if(f)
                    break;
            }
            if(f)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}       

HDU 2143 Can you find it?(基礎二分)