avatar
fireworks99
keep hungry keep foolish

HDU 2141 Can you find it?

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

题意

给出三个数组,每个数组选一个数相加,让其等于下面给出的和

二分查找

将a + b + c = s 视为 s - a = b + c

将b + c形成一个新数组k,并sort(排序,二分查找的前提)

一个for循环(s - a)

暴力超时

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int L, N, M;
int l[505];
int n[505];
int m[505];
int k[250005];

bool diy_search(int x)
{
    int l = 0, r = M * N;
    while(r - l >= 1)
    {
        int i = (l + r) / 2;
        if(k[i] == x)
            return 1;
        else if(k[i] < x)
            l = i + 1;
        else
            r = i;
    }
    return 0;
}

int main()
{
    int tot = 1;
    while(~scanf("%d%d%d", &L, &N, &M))
    {
        printf("Case %d:\n", tot);
        tot++;
        for(int i = 0; i < L; ++i)
            scanf("%d", &l[i]);
        for(int i = 0; i < N; ++i)
            scanf("%d", &n[i]);
        for(int i = 0; i < M; ++i)
            scanf("%d", &m[i]);

        for(int i = 0; i < N; ++i)
            for(int j = 0; j < M; ++j)
                k[i * N + j] = n[i] + m[j];
        sort(k, k + N * M);

        int s;
        scanf("%d", &s);
        for(int i = 0; i < s; ++i)
        {
            int tem;
            scanf("%d", &tem);
            bool flag = 0;
            for(int j = 0; j < L; ++j)
                if(diy_search(tem - l[j]))
                {
                    flag = 1;
                    cout << "YES" << '\n';
                    goto over;///goto跳出多重循环
                }
            if(!flag)
                cout << "NO" << '\n';
over:
            ;
        }
    }
    return 0;
}

用(upper_bound - lower_bound > 0)代替自己写的二分查找函数

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int L, N, M;
int l[505];
int n[505];
int m[505];
int k[250005];

int main()
{
    int tot = 1;
    while(~scanf("%d%d%d", &L, &N, &M))
    {
        printf("Case %d:\n", tot);
        tot++;
        for(int i = 0; i < L; ++i)
            scanf("%d", &l[i]);
        for(int i = 0; i < N; ++i)
            scanf("%d", &n[i]);
        for(int i = 0; i < M; ++i)
            scanf("%d", &m[i]);

        for(int i = 0; i < N; ++i)
            for(int j = 0; j < M; ++j)
                k[i * N + j] = n[i] + m[j];
        sort(k, k + N * M);

        int s;
        scanf("%d", &s);
        for(int i = 0; i < s; ++i)
        {
            int tem;
            scanf("%d", &tem);
            bool flag = 0;
            for(int j = 0; j < L; ++j)
                if(upper_bound(k, k + N * M, tem - l[j]) - lower_bound(k, k + N * M, tem - l[j]))
                {
                    flag = 1;
                    cout << "YES" << '\n';
                    goto over;
                }
            if(!flag)
                cout << "NO" << '\n';
over:
            ;
        }
    }
    return 0;
}

关于goto

goto 语句可以无条件地转移到过程中指定的行。

goto 语句通常与条件语句配合使用。可用来实现条件转移, 构成循环,跳出循环体等功能。

弊端

在结构化程序设计中一般不主张使用 goto 语句, 以免造成程序流程的混乱,使理解和调试程序都产生困难。

goto的使用

Site by Baole Zhao | Powered by Hexo | theme PreciousJoy