二分查找 HDOJ 2141 Can you find it?
/*
题意:给出一个数,问是否有ai + bj + ck == x
二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std; typedef long long ll;
const int MAXN = 5e2 + ;
const int INF = 0x3f3f3f3f;
ll a[MAXN], b[MAXN], c[MAXN];
ll sum[MAXN*MAXN];
int tot; bool my_binary_search(int l, int r, ll k) {
while (l < r) {
int mid = (l + r) >> ;
if (sum[mid] == k) return true;
else if (sum[mid] > k) r = mid;
else l = mid + ;
}
return false;
} int main(void) { //HDOJ 2141 Can you find it?
//freopen ("HDOJ_2141.in", "r", stdin); int l, n, m, s, cas = ;
while (scanf ("%d%d%d", &l, &n, &m) == ) {
for (int i=; i<=l; ++i) scanf ("%I64d", &a[i]);
for (int i=; i<=n; ++i) scanf ("%I64d", &b[i]);
for (int i=; i<=m; ++i) scanf ("%I64d", &c[i]);
scanf ("%d", &s); printf ("Case %d:\n", ++cas);
sort (a+, a++l); sort (b+, b++n); sort (c+, c++m);
tot = ;
for (int i=; i<=l; ++i) {
for (int j=; j<=n; ++j) {
sum[++tot] = a[i] + b[j];
}
}
sort (sum+, sum++tot); ll mn = a[] + b[] + c[], mx = a[l] + b[n] + c[m];
while (s--) {
ll q; scanf ("%I64d", &q);
if (q < mn || q > mx) {
puts ("NO"); continue;
}
bool flag = false;
for (int i=; i<=m; ++i) {
int p = lower_bound (sum+, sum++tot, q - c[i]) - sum;
if (p < || p > tot) continue;
if (sum[p] + c[i] == q) {
flag = true; puts ("YES"); break;
}
//if (my_binary_search (1, tot, q - c[i])) {
//flag = true; puts ("YES"); break;
//}
}
if (!flag) {
puts ("NO");
}
}
} return ;
}
二分查找 HDOJ 2141 Can you find it?的更多相关文章
- hdoj 2141 Can you find it?【二分查找+暴力】
Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others ...
- hdu 2141 Can you find it?(二分查找)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. /* x^2+6*x- ...
- 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 yo ...
- Can you find it? HDU - 2141 (二分查找)
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...
- hdu 2141:Can you find it?(数据结构,二分查找)
Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others ...
- HDU 2141 Can you find it?【二分查找是否存在ai+bj+ck=x】
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...
- 二分查找+数学 HDOJ 4342 History repeat itself
题目传送门 题意:计算从1开始到第n个非完全平方数的开方和 分析:设第n个非完全平方数的值为a,x * x < a < (x+1) * (x+1),而且易得(tmp = sqrt (a) ...
- 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers
题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...
- jvascript 顺序查找和二分查找法
第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...
随机推荐
- Labeling Balls(poj 3687)
题意:N个球,从1-N编号,质量不同,范围1-N,无重复.给出小球间的质量关系(<), 要求给每个球贴标签,标签表示每个球的质量.按编号输出每个球的标签.如果解不唯一,按编号小的质量小排. /* ...
- 创建Django项目(七)——表单
2013-08-15 19:43:01| 1.URL配置和视图 "blog\urls.py"文件中:添加url(r'write_article/$', 'write ...
- [bzoj3160]万径人踪灭_FFT_Manacher
万径人踪灭 bzoj-3160 题目大意:给定一个ab串.求所有的子序列满足:位置和字符都关于某条对称轴对称而且不连续. 注释:$1\le n\le 10^5$. 想法: 看了大爷的题解,OrzOrz ...
- bzoj——3555: [Ctsc2014]企鹅QQ
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2617 Solved: 921[Submit][Statu ...
- Canon iP2780/iP2788 清零软件
http://www.drvsky.com/driver/iP2780_Tools.htm http://www.dyjqd.com/soft/6085.html#download http://v. ...
- python: filter, map, reduce, lambda
filter built-in function filter(f,sequence) filter can apply the function f to each element of seque ...
- mybatis collection标签和association标签(一对多,一对一)转载
mybatis 一对一与一对多collection和association的使用 在mybatis如何进行一对一.一对多的多表查询呢?这里用一个简单的例子说明. 一.一对一 1.associati ...
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...
- symfony could not load type 'datetime'
当用curd生成控制器后,当修改的时候,会有这个提示,解决方法 在orm中通过事务的方式填充时间,然后把生成的form中的文件的时间段去掉 $builder ->add('title') -&g ...
- 如何利用神经网络和Python生成指定模式的密码
今天给大家介绍的是Github上一个名叫PyMLProjects的项目,这个项目的目的是为了训练AI来学习人类构造密码的模式,然后我们就可以用AI来生成大量同一模式或种类的密码了.这种方法也许可以用来 ...