Time Limit:3000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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
 
解题思路:题目大意:给出3串整数A,B,C及整数X,如果存在一组数Ai,Bj,Ck,使得Ai+Bj+Ck=X成立,则输出“YES”,否则输出“NO”。
数据类型使用int即可。首先看到题目想到的是暴力法破解,3个for循环,时间复杂度为O(n3)。当n达到一定规模时此方法不可取。还容易想到二分检索,效率比较高。思路:对后两个序列求和,结果排序去重。时间复杂度为O(n2)。对第一个序列枚举,二分检索和序列,时间为nlgn。因此总的时间复杂度为O(n2,方案可行。
 
收获感想:由于昨天上课学习到二分查找的应用,还比较熟悉,其中数组去重浪费了一点时间,由于保存两个数组的和时创建了一个新的数组,如果在创建一个数组来保存去重以后的结果就没有必要了,直接利用原来的数组,由于有序,只要比较前后两个数,从第一个数开始,计数器第一次出现的位置,再往后遍历,直到新的数出现,将其往前移,覆盖掉重复的值,计数器加1。
 
 
 
 
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int A[],B[],C[],temp[];
int L,N,M,S,i;int d=;
while(cin>>L>>N>>M)
{ if(L<||L>||N<||N>||M<||M>) return -;
//输入
for(i=;i<L;i++) cin>>A[i];
for(i=;i<N;i++) cin>>B[i];
for(i=;i<M;i++) cin>>C[i];
cin>>S;
if(S<||S>) return -; //求后两组的和保存到temp中
int tp=;
for(i=;i<N;i++)
for(int t=;t<M;t++)
{
temp[tp]=B[i]+C[t];
tp++;
}
sort(temp,temp+M*N);
//数组去重
int tp1=;
for(i=;i<M*N;i++)
{
if(temp[i]==temp[i-]) continue;
else {tp1++;temp[tp1]=temp[i];}
} cout<<"Case "<<d<<":"<<endl;
while(S--){
int flag=;
int X;
cin>>X;
for(i=;i<L;i++){
int lo=,hi=tp1;
int mi;
while(lo<=hi)
{
mi=((hi-lo)>>)+lo;//lo和hi比较大的时候相加可能爆int
if((A[i]+temp[mi])==X) {flag=; break;}
else if(A[i]+temp[mi]<X) lo=mi+;
else hi=mi-;
}
}
if(!flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl; }
d++;
}
return ;
}

CSU-ACM2016暑假集训训练1-二分搜索 A - Can you find it?的更多相关文章

  1. 2016huasacm暑假集训训练五 H - Coins

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/H 题意:A有一大堆的硬币,他觉得太重了,想花掉硬币去坐的士:的士司机可以不找零,但 ...

  2. 2016huasacm暑假集训训练五 J - Max Sum

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...

  3. 2016huasacm暑假集训训练五 G - 湫湫系列故事——减肥记I

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/G 这是一个01背包的模板题 AC代码: #include<stdio.h&g ...

  4. 2016huasacm暑假集训训练五 F - Monkey Banana Problem

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/F 题意:求至上而下一条路径的所经过的值得和最大值,这题比赛时就出了 但当时看不懂题 ...

  5. 2016huasacm暑假集训训练五 E - What Is Your Grade?

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/E 题意:给做出的题目个数,5个的100分,4个的前n/2的同学95,后n/2的90 ...

  6. 2016huasacm暑假集训训练五 C-Common Subsequence

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/C 题意:这是一道求字符串的公共子串的最大长度的题目,用dp动态方程即可 if(a[ ...

  7. 2016huasacm暑假集训训练四 DP_B

    题目链接:http://acm.hust.edu.cn/vjudge/contest/125308#problem/M 题意:有N件物品和一个容量为V的背包.第i件物品的费用是体积c[i],价值是w[ ...

  8. 2016huasacm暑假集训训练四 数论_B

    题目链接:http://acm.hust.edu.cn/vjudge/contest/125308#problem/G 题意:求有多少x(1<=x<=n),使得gcd(x,n)>=m ...

  9. 2016huasacm暑假集训训练四 数论_A

    题目链接:http://acm.hust.edu.cn/vjudge/contest/125308#problem/F 题意:狼捉兔子,兔子躲在n个洞中一个,这n个洞围成一个圈,狼会从第0号洞开始,搜 ...

  10. 2016huasacm暑假集训训练四 _排列

    题目链接:http://acm.hust.edu.cn/vjudge/contest/125308#problem/D 这题要求错误的方式有多少种,就是一个错排公式,记得公式就行            ...

随机推荐

  1. Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度

    Problem D. Dinner ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1003 ...

  2. (字符串的模式匹配4.7.18)POJ 2406 Power Strings(求一个字符串的最小重复串)

    注意,在IDE运行时,可能会因为开的数组太大而报错,这时我们可以把数组开小一点来进行调试....提交的时候把数组的大小改成1000005即可.... #include <iostream> ...

  3. 【转】Dancing Links精确覆盖问题

    原文链接:http://sqybi.com/works/dlxcn/ (只转载过来一部分,全文请看原文,感觉讲得很好~)正文    精确覆盖问题    解决精确覆盖问题    舞蹈步骤    效率分析 ...

  4. iOS开发——语法篇&swift经典语法总结

    swift经典语法总结 1:函数 1.1 func funcNmae()->(){} 这样就定义了一个函数,它的参数为空,返回值为空,如果有参数和返回值直接写在两个括号里就可以了 1.2 参数需 ...

  5. qt里标识操作系统的宏

    qt文件里的 windows的宏是Q_WS_WIN32/*   The window system, must be one of: (Q_WS_x) MACX     - Mac OS X      ...

  6. Linux 学习笔记 Linux环境变量初稿

    set命令会显示为某个特定进程设置的所有环境变量,但不包括一些没有设置值的默认环境变量 可以通过等号来给环境变量赋值,值可以是数值或字符串 如果要给变量赋一个含有空格的字符串值,必需用单引号来界定字符 ...

  7. QTP 中对象操作

    1.创建对象(将浏览器设置为一个对象) set oIE=createobject("internetexplorer.application") 2.将浏览器设置为可见的 oIE. ...

  8. SqlServer2008 之 应用积累

    1.断开数据库连接,在原有查询窗口(断开数据库连接的未关闭查询窗口),对现在所连数据库进行操作,结果是对已断开数据库的误操作. 正确操作:重新连接数据库后,应关闭原有查询窗口,新建查询窗口后再执行操作 ...

  9. struts2.1笔记05:struts2开发环境的搭建

    1.找到开发Struts应用需要使用到的jar文件. 首先我们要在myEclipse中新建一个Web Project,我们这里命名为"struts2".然后我们就要使用jar文件, ...

  10. ng中用$http接后台接口的异步坑

    最近笔者在一个项目中用ng去接后台的接口.因为前后端都是新手,前端的不懂后台,且没有经验:后端的不懂前端,也没有经验,然后接口bug百出,文档写得乱.一个接口,后台改了三次,我也是寸步难行. 首先来看 ...