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. Web App 讲义教程

    http://www.csdn.net/tag/web%E5%BA%94%E7%94%A8

  2. Android开发学习之 定制界面风格

    统一的用户界面是可以使得应用程序更友好.要做到用户界面的统一,我们就必须用到风格(style)和主题(theme).OPhone系统提供了很多系统默认的风格和主题,但是很多情况下,这些不能满足我们的需 ...

  3. NSRange,判断字符串的各种操作~

    今天写的都比较简单,偶尔偷一下懒,猪真的很懒啊~   - (void)viewDidLoad { [super viewDidLoad]; //抽取指定范围的字符串 NSString *string1 ...

  4. onClick,onServerClick,onClientClick

    <asp:button id=button1 runat=server test=button1 onclick=button1_onclick/> <input type=butt ...

  5. mybatis0210 mybatis和ehcache缓存框架整合

    .1mybatis和ehcache缓存框架整合 一般不用mybatis来管理缓存而是用其他缓存框架在管理缓存,因为其他缓存框架管理缓存会更加高效,因为别人专业做缓存的而mybatis专业做sql语句的 ...

  6. visualvm连接服务器jvm进行监控

    本地环境,mac: 服务器环境ubuntu: 直接运行jstatd命令,提示 Could not create remote object access denied ("java.util ...

  7. Spark on Yarn遇到的问题及解决思路

    原文:http://www.aboutyun.com/thread-9425-1-1.html 问题导读1.Connection Refused可能原因是什么?2.如何判断内存溢出,该如何解决?扩展: ...

  8. Subsequence

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  9. Adobe Edge Animate –修改Edge Commons Spotlight功能,使之能支持播放中国网站视频

    Adobe Edge Animate –修改Edge Commons Spotlight功能,使之能支持播放中国网站视频 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权 ...

  10. 关于Override在JDK1.5和JDK1.6上子类实现接口中方法使用@Override注解编译错误.

    遇到这个问题说来也怪.新开了一个path的工作空间用来打patch.该Eclipse的默认全局的编译版本是1.6.但是唯独其中的一个插件项目的版本是1.5(可能是唯一的一个,不确定,不知道为什么会是这 ...