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

思路:枚举三个数组,时间复杂度O(N^3),肯定会超时。所以可以把前两个数组的和先枚举出来,存在数组sum[260000]中,

由于要找是否存在sum[i] + c[j] = X, 可以把问题转换为在sum数组中查找是否有X-c[j]这个数。用二分查找sum数组即可。时间复杂度为: S * c数组的大小 * log(sum数组的大小)

注意:不能在c数组中二分查找是否有X-sum[i]这个数,因为其时间复杂度为: S * sum数组的大小 * log(c数组的大小),由于题目中sum数组大小最大为250000,c数组大小最大为500,S为1000,所以会超时

 #include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm> using namespace std; int a[], b[], c[];
int sum[];
int L, N, M, S, X;
int cnt = ; int main()
{
while(scanf("%d %d %d", &L, &N, &M) != EOF)
{
for(int i = ; i < L; ++i)
scanf("%d", &a[i]);
for(int i = ; i < N; ++i)
scanf("%d", &b[i]);
for(int i = ; i < M; ++i)
scanf("%d", &c[i]); int k = ;
for(int i = ; i < L; ++i)
for(int j = ; j < N; ++j)
sum[k++] = a[i] + b[j]; sort(sum, sum+k);
scanf("%d", &S);
printf("Case %d:\n", cnt++);
while(S--)
{
scanf("%d", &X); int flag = ;
for(int i = ; i < M; ++i)
{
int target = X - c[i];
int left = , right = k; while(left <= right)
{
int mid = (left + right) / ;
if(sum[mid] == target)
{
flag = ;
break;
}
else if(sum[mid] < target)
left = mid + ;
else
right = mid - ;
}
if(flag == )
{
printf("YES\n");
break;
}
}
if(flag == )
printf("NO\n"); }
} return ;
}

Can you find it? HDU-2141 (二分查找模版题)的更多相关文章

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

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  2. 集训第四周(高效算法设计)N题 (二分查找优化题)

    原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...

  3. 集训第四周(高效算法设计)B题 (二分查找优化题)

    ---恢复内容开始--- Description   Before the invention of book-printing, it was very hard to make a copy of ...

  4. Equations(hdu 1496 二分查找+各种剪枝)

    Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. Pie(hdu 1969 二分查找)

    Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  6. 集训第四周(高效算法设计)C题 (二分查找优化题)

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  7. Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】

    1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  8. hdu2063 匈牙利算法 二分最大匹配模版题

    过山车 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java class na ...

  9. HDU 4738 双连通模版题

    九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/11711577 题意:给定n个点,m条无向边 下面m行表示u , v ,边权值 求 ...

随机推荐

  1. Redis学习笔记01-分布式锁

    1.分布式锁的定义与理解 在并发任务中,当对数据执行修改和删除时为了防止多个任务同时拿到数据而产生的混乱,这时就要用到分布式锁来限制程序的并发执行. Redis分布式锁本质上要实现的目标就是在Redi ...

  2. WPF MVVM模式不用Prism

    上一个例子使用了Prism.这个例子不用Prism.用自己封装的库LiuxhCSDLL,其实也差不多. 一.程序结构 二.界面代码以及界面效果 <Window x:Class="WPF ...

  3. JAVA缓存的实现

    缓存可分为二大类: 一.通过文件缓存,顾名思义文件缓存是指把数据存储在磁盘上,不管你是以XML格式,序列化文件DAT格式还是其它文件格式: 二.内存缓存,也就是实现一个类中静态Map,对这个Map进行 ...

  4. Activiti历史查看

    流程执行完毕后,究竟去了哪里有些疑问. 虽然已完成的任务在act_ru_task和act_ru_execution表中都已被删除,但是这些数据还存在activiti的数据库中,作为历史改由Histor ...

  5. Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯(bfs)

    P2845 [USACO15DEC]Switching on the Lights 开关灯 题意 题目背景 来源:usaco-2015-dec \(Farm\ John\)最近新建了一批巨大的牛棚.这 ...

  6. $cordovaNetwork 使用

    1 .安装插件 直接安装: cordova plugin add cordova-plugin-network-information 下载到本地安装: https://github.com/apac ...

  7. PAT甲级——A1031 Hello World for U

    Given any string of N (≥) characters, you are asked to form the characters into the shape of U. For ...

  8. 【DM642学习笔记二】dsp基础实验:发光二级管的显示 led.c

    1,OSDFPGA配置一个专用的8位寄存器控制指示灯亮灭,访问地址为90080017h,由电路图可知低电平点亮. 2,程序运行时,可直接editmemory.即修改90080017h地址的值(可在Ed ...

  9. WPF 线程中异常导致程序崩溃

    一般我们WPF中都加全局捕获,避免出现异常导致崩溃. Application.Current.DispatcherUnhandledException += Current_DispatcherUnh ...

  10. svn命令获取项目中被忽略文件情况

    第一步.通过终端进入项目目录下第二步.运行命令svn pg svn:ignore -R