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. HDU 5536 Chip Factory 字典树

    Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  2. delphi TPopupMenu.Popup

      procedure TPopupMenu.Popup(X, Y: Integer);     这个点是相对桌面的而不是窗体的   GetCursorPos是鼠标的位置 鼠标动这个点就不一样   v ...

  3. mybatis 关联映射传递多参数方法

    <resultMap type="com.secoo.my.bean.OrderItemComment" id="orderItemResult"> ...

  4. MHA自动Failover过程解析(updated) 转

    允许转载, 转载时请以超链接形式标明文章原始出处和网站信息 http://www.mysqlsystems.com/2012/03/figure-out-process-of-autofailover ...

  5. 面向对象设计原则OO

    面向对象设计原则是OOPS(Object-Oriented Programming System,面向对象的程序设计系统)编程的核心,但大多数Java程序员追逐像Singleton.Decorator ...

  6. PhpCMS标签:专题模块special标签

    专题模块 专题模块PC标签调用说明 模块名:special 模块提供的可用操作 操作名 说明 lists 专题列表 content_list 专题信息列表 hits 专题信息点击排序 下面对所有的操作 ...

  7. 电商ERP如何接入智选物流平台?

    智选物流是综合地址库+时效库+逻辑库(成本.订单属性.仓库)选择出最优快递,通过多家快递网点地址库精准数据,点对点的运输时间,各种行业包裹的所有快递价格逻辑,不同产品类型.支付方式等分析,实现不同仓库 ...

  8. Android进阶笔记05:View、SurfaceView 和GLSurfaceView 的关系和区别

    1.  Android游戏当中主要的除了控制类外就是显示类View.SurfaceView是从View基类中派生出来的显示类.android游戏开发中常用的三种视图是:    (1) view.Sur ...

  9. (转载)MatLab绘图

    转载自:http://www.cnblogs.com/hxsyl/archive/2012/10/10/2718380.html 转载自:http://www.cnblogs.com/jeromebl ...

  10. agile学习

    https://www.flickr.com/photos/codingthearchitecture/sets/