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 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 (二分查找模版题)的更多相关文章
- Can you find it?(hdu 2141 二分查找)
Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others ...
- 集训第四周(高效算法设计)N题 (二分查找优化题)
原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...
- 集训第四周(高效算法设计)B题 (二分查找优化题)
---恢复内容开始--- Description Before the invention of book-printing, it was very hard to make a copy of ...
- Equations(hdu 1496 二分查找+各种剪枝)
Equations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Pie(hdu 1969 二分查找)
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 集训第四周(高效算法设计)C题 (二分查找优化题)
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 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 ...
- hdu2063 匈牙利算法 二分最大匹配模版题
过山车 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java class na ...
- HDU 4738 双连通模版题
九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/11711577 题意:给定n个点,m条无向边 下面m行表示u , v ,边权值 求 ...
随机推荐
- Python学习day16-模块基础
<!doctype html>day16 - 博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { ...
- Activiti流程定义语言
1.流程(process) bpmn文件一个流程的根元素.一个流程就代表一个工作流. 2.顺序流(sequenceFlow) 顺序流是连接两个流程节点的连线,代表一个节点的出口.流程执行完一个节点后, ...
- light oj 1219 树上贪心
#include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...
- (一)SpringBoot入门【基于2.x版本】
SpringBoot入门[基于2.x版本] 一.SpringBoot简介 首先大家学习SpringBoot的话,我希望大家是有一定java基础的,如果是有Spring的基础的话,上手会更加得心应手,因 ...
- Maven实战06_坐标和邮件服务模块
1:何为Maven坐标 为了能够自动化地解析任何一个Java构件,Maven就必须要将其唯一标识,这就是依赖管理的底层基础--坐标. 学过数学的人都知道平面直角坐标系,x,y分别为其横,纵坐标,将会在 ...
- mysql sum() 求和函数的用法
查询在record表中 name=? 的 money 加起来的值使用聚和函数 sum() 求和select sum(money) from record t where t.name = ?另外:co ...
- Linux下根目录root扩容
参考博客:https://blog.csdn.net/qq_36527339/article/details/81772996 1.首先虚拟机关机 —> 选中要扩容的虚拟机 —>编辑虚拟机 ...
- 你应该知道的25个非常有用的CSS技巧
在我们的前端CSS编码当中,经常要设置特殊的字体效果,边框圆角等等,还要考虑兼容性的问题, CSS网页布局,说难,其实很简单. 说它容易,往往有很多问题困扰着新手,在中介绍了非常多的技巧,这些小技巧与 ...
- linux目录结构详细说明
Linux各目录及每个目录的详细介绍 [常见目录说明] 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所 ...
- LINUX配置文件介绍
每个 Linux 程序都是一个可执行文件,它含有操作码列表,CPU 将执行这些操作码来完成特定的操作.例如,ls 命令是由 /bin/ls 文件提供的,该文件含有机器指令的列表,在屏幕上显示当前目录中 ...