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 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
题意:给你L个A,N个B,M个C以及S个X,问对于每一个x,能否从A,B,C中各找出一个值,使得A+B+C=X.
思路:这是一道二分题,可以把A+B+C=X看做A+B=X-C,那么我们先把A+B并成一个集合Z,然后枚举X-C的值,二分寻找Z中是否有这个值。本来挺简单的二分,自己做的时候一直考虑二分C数组,时间复杂度怎么算都超了= .= 。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define eps 1e-15
#define maxn 506
ll a[maxn],b[maxn],c[maxn],d[2*maxn],e[maxn];
ll bing[maxn*maxn]; int main()
{
int n,m,i,j,cas=0,t,p;
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
for(i=1;i<=m;i++){
scanf("%lld",&b[i]);
}
int tot=0;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
tot++;
bing[tot]=a[i]+b[j];
}
}
sort(bing+1,bing+1+tot);
for(i=1;i<=t;i++){
scanf("%lld",&c[i]);
}
scanf("%d",&p);
for(i=1;i<=p;i++){
scanf("%lld",&e[i]);
}
cas++;
printf("Case %d:\n",cas);
for(i=1;i<=p;i++){
int flag=0;
for(j=1;j<=t;j++){
int wei=lower_bound(bing+1,bing+1+tot,e[i]-c[j])-bing;
if(bing[wei]==e[i]-c[j]){
flag=1;break;
}
}
if(flag)printf("YES\n");
else printf("NO\n");
}
}
return 0;
}

hdu2141 Can you find it? (二分)的更多相关文章

  1. hdu-2141 Can you find it?---暴力+二分

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意: 给ABC三个数组,给一个X,求是否存在Ai+Bj+Ck = X 思路: 等式转化成 ...

  2. Can you find it?(数组+二分hdu2141)

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

  3. 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 t ...

  4. HDU2199,HDU2899,HDU1969,HDU2141--(简单二分)

    二分是一种很有效的减少时间开销的策略, 我觉得单列出二分专题有些不太合理, 二分应该作为一中优化方法来考虑 这几道题都是简单的使用了二分方法优化, 二分虽然看似很简单, 但一不注意就会犯错. 在写二分 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. /* x^2+6*x- ...

  6. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  7. BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 3352  Solved: 919[Submit][Stat ...

  8. 整体二分QAQ

    POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...

  9. [bzoj2653][middle] (二分 + 主席树)

    Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...

随机推荐

  1. tf.argmax(vector,axis)函数的使用

    1.返回值 vector为向量,返回行或列的最大值的索引号: vector为矩阵,返回值是向量,返回每行或每列的最大值的索引号. 2.参数 vector为向量或者矩阵 axis = 0 或1 0:返回 ...

  2. LeetCode24 两两交换链表中的节点

    给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...

  3. LeetCode53 最大子序列问题

    题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和.     示例:     输入: [-2,1,-3,4,-1,2,1,-5,4],   ...

  4. Hive Query生命周期 —— 钩子(Hook)函数篇

    无论你通过哪种方式连接Hive(如Hive Cli.HiveServer2),一个HQL语句都要经过Driver的解析和执行,主要涉及HQL解析.编译.优化器处理.执行器执行四个方面. 以Hive目前 ...

  5. [oracle] exp-00091

    产生原因: 在数据库的服务器端和客户端字符集不同的情况下,导出(dump)数据库表时,会产生这个错误.虽然产生这个错误,但好像对导入没有影响. 解决办法: 查看服务器端字符集: 打开SQLPLUS,执 ...

  6. 树莓派3B装ubuntu server后开启wifi

    树莓派官网选择ubuntu server下载映像 step 1: 使用SDFormatter格式化SD卡: step2: 使用win32diskimager工具将映像写入准备好的SD卡: step3: ...

  7. consul是什么?

    consul概念: consul是用来做注册中心的 他和eureka是一样的 注册中心一般都是集群的形式存在保证高可用 consul像是一个nosql 存储着键值对 可以做存储consul是c/s架构 ...

  8. 微人事项目-mybatis-持久层

    摘要 最近将微人事这个开源项目进行了复现,这篇文章记录mybaits访问数据库这一块. 其中MyBatis是一个流行的持久层框架,支持自定义SQL.存储过程和高级映射.MyBatis消除了几乎所有的J ...

  9. 获取Java线程转储的常用方法

    1. 线程转储简介 线程转储(Thread Dump)就是JVM中所有线程状态信息的一次快照. 线程转储一般使用文本格式, 可以将其保存到文本文件中, 然后人工查看和分析, 或者使用工具/API自动分 ...

  10. Python+Selenium+Unittest实现PO模式web自动化框架(8)

    1.main.py模块的功能 最后就是要有一个项目入口,并且是需要加载测试用例集. # --^_^-- coding:utf-8 --^_^-- # @Remark:运行入口 "" ...