hdu-2141(二分查找+暴力)

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三个序列,分别从这三个序列中找三个数,若存在三个数之和等于X,则输出”YES”,否则输出”NO”

解析

三重循环的搜索肯定是不能用的。我的解法是将a+b的值存到ab数组中,将该数组排序。再用二分搜索判断x-c是否存在于该数组中,若存在,则证明存在x=a+b+c,输出’YES’即可。若对于所有c,都不存在x-c属于该数组,则证明不存在x=a+b+c,输出’NO’即可。存在一定的技巧,下面贴一波代码

代码

#include <iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

int main()
{
    int a[510],b[510],c[510],ab[250010];
    int l,n,m;
    int s;
    int x[1010];
    int kase=0;
    bool ans;
    while(scanf("%d%d%d",&l,&n,&m)!=EOF){
        kase++;
        for(int i=0;i<l;i++){
            scanf("%d",&a[i]);
        }
        for(int i=0;i<n;i++){
            scanf("%d",&b[i]);
        }
        for(int i=0;i<m;i++){
            scanf("%d",&c[i]);
        }
        scanf("%d",&s);
        for(int i=0;i<s;i++){
            scanf("%d",&x[i]);
        }
        for(int i=0;i<l;i++){
            for(int j=0;j<n;j++){
                ab[i*l+j]=a[i]+b[j];
            }
        }
        sort(ab,ab+l*n);
        printf("Case %d:\n",kase);
        for(int i=0;i<s;i++){
            ans=false;
            for(int j=0;j<m;j++){
                if(binary_search(ab,ab+l*n,x[i]-c[j])){
                    ans=true;
                    break;
                }
            }
            if(ans){
                printf("YES\n");
            }else{
                printf("NO\n");
            }
        }
    }

}

原文地址:https://www.cnblogs.com/lklk/p/8782695.html

时间: 2024-08-29 12:08:50

hdu-2141(二分查找+暴力)的相关文章

Can you find it?(hdu 2141 二分查找)

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 19416    Accepted Submission(s): 4891 Problem Description Give you three sequences of numbers A, B, C, then we give you a number

hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737 Problem Description There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)

二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

题目传送门 1 /* 2 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 5e2 + 10; 10 const int MAXM = 1e6 + 10; 11 const int INF = 0

hdoj 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): 17036    Accepted Submission(s): 4337 Problem Description Give you three sequences of numbers A, B, C, then we give you a number

hdu 2141 二分

题意是给你三个数组   分别有n m k个数     从三个数组里分别拿一个数能不能等于s 刚开始没注意数可以为负数    wa了好多次 别想直接暴力  肯定超时 现将两个数组合并  暴力枚举数少的      二分枚举数多的(非常省时) #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int main() { int num1[510],num2[510],num3

hdu 2141 Can you find it?(二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. 1 /* 2 3 x^2+6*x-7==y 4 输入y 求x 精确度为10^-5 5 0=<x<=10000 6 7 */ 8 #include <iostream> 9 #include <cstdio> 10 using namespace std; 11 int main (void) 1

hdu 5676 ztr loves lucky numbers(BC——暴力打表+二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5676 ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 594    Accepted Submission(s): 257 Problem Description ztr loves luck

hdu 2141 Can you find it?(二分查找变例)

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

二分查找 HDOJ 2141 Can you find it?

题目传送门 1 /* 2 题意:给出一个数,问是否有ai + bj + ck == x 3 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAX