HDU2141——二分——Can you find it?

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了。。否则TLE

*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAX = 500 + 10;
int L[MAX], N[MAX], M[MAX];
int LN[1000100],Ln[1000100];
int l, n, m, q;

int main()
{
    int cas = 0;
    while(~scanf("%d%d%d", &l, &n, &m)){
        for(int i = 1; i <= l; i++)
            scanf("%d", &L[i]);
        for(int i = 1; i <= n; i++)
            scanf("%d", &N[i]);
        for(int i = 1; i <= m; i++)
            scanf("%d", &M[i]);
        int cout = 1;
        for(int i = 1; i <= l; i++){
            for(int j = 1; j <= n; j++){
                LN[cout++] = L[i] + N[j];
            }
        }
        sort(LN + 1, LN + cout);
        int cout1 = 1;
        for(int i = 1; i < cout; i++){
            if(LN[i] != LN[i+1]) ;
            Ln[cout1++] = LN[i];
        }
        scanf("%d", &q);
        cas++;
        printf("Case %d:\n", cas);
        int p;
        for(int i = 1; i <= q; i++){
            scanf("%d", &p);
            int flag = 0;
            for(int  j = 1; j <= m; j++){
                int ll = 1, rr = cout1 - 1;
                while(ll <= rr){
                    int mid = (ll + rr) >> 1;
                    if(Ln[mid] + M[j] == p){
                    //    printf("%d %d\n", mid, M[j]);
                        flag = 1;
                        break;
                    }
                    else if(Ln[mid] + M[j] > p)
                        rr = mid - 1;
                    else ll = mid + 1;
                }
                if(flag == 1) break;
            }
            if(flag == 1) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

  

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

HDU2141——二分——Can you find it?的相关文章

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 descr

hdu2141

试题链接:点击这里 在我看来,对于这道题的解法,有很多常规的方法,但是都是使用二分.但是这段代码我第一眼看见是非常的短的,当时很怀疑,因为自己写的东西还是在细节方面处理的非常的糟糕.然后看见这段代码相对于网上的其他代码来讲是非常的短的.仔细看了看,思想是非常的精妙的,和以前自己处理的一个问题有异曲同工之美. 好了,先把他的代码贴出来,然后最后在自己写一些常规的代码吧.希望以后能够将模板完全的敲出来. #include <iostream> #include <algorithm>

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

2.1 二分分类

本周学习神经网络编程的基础知识 构建神经网络,有些技巧是非常重要 神经网络的计算过程中,通常有一个正向的过程(正向传播步骤),接着会有一个反向步骤(反向传播步骤), 为什么神经网络的计算可以分为前向传播和反向传播两个分开的过程?本周课程通过使用logistic回归来阐述,以便于能够更好的理解, logistic回归是一个用于二分分类的算法 比如有一个二分分类问题的例子, 假如有一张图像作为输入是这样的,你想输出识别此图的标签,如果是猫,输出1,如果不是,则输出0 使用y来表示输出的结果标签, 来

HDU3715(二分+2-SAT)

Go Deeper Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3184    Accepted Submission(s): 1035 Problem Description Here is a procedure's pseudocode: go(int dep, int n, int m)beginoutput the valu

二分查找

递归版(在区间[x, y)中找v的位置) 1 //递归版二分查找 2 int bsearch(int * A, int x, int y, int v) 3 { 4 5 if(v<a[x] || v>a[y-1]) return -1; 6 int m = x + (y-x)/2; //此处能不能用int m = (x+y)/2,需要仔细考虑(暂时想不到原因) 7 if(A[m]==v) return m; 8 else if(A[m]>v) return bsearch(A, x, m

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

二分查找总结

最近刷leetcode和lintcode,做到二分查找的部分,发现其实这种类型的题目很有规律,题目大致的分为以下几类: 1.最基础的二分查找题目,在一个有序的数组当中查找某个数,如果找到,则返回这个数在数组中的下标,如果没有找到就返回-1或者是它将会被按顺序插入的位置.这种题目继续进阶一下就是在有序数组中查找元素的上下限.继续做可以求两个区间的交集. 2.旋转数组问题,就是将一个有序数组进行旋转,然后在数组中查找某个值,其中分为数组中有重复元素和没有重复元素两种情况. 3.在杨氏矩阵中利用二分查

BZOJ 1044 木棍分割 解题报告(二分+DP)

来到机房刷了一道水(bian’tai)题.题目思想非常简单易懂(我的做法实际上参考了Evensgn 范学长,在此多谢范学长了) 题目摆上: 1044: [HAOI2008]木棍分割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3162  Solved: 1182[Submit][Status][Discuss] Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个