Can you find it?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 42520    Accepted Submission(s): 10315

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
 
Author
wangye
 
Source
 
Recommend
威士忌   |   We have carefully selected several similar problems for you:  2899 2289 1597 1551 2298 
 
今天从基础开始学起,以为二分挺容易的,但是忽视了题目需要考虑时间复杂度,会不会爆int,等等。
存不存在ai,bi,ci加起来是x。
如果直接枚举那也太简单了吧,我就想着,大循环a,小循环b,小小循环对c二分,发现这样也超时!!!
然后,我就大循环a,小循环对b二分,小小循环对c二分,结果这样的思路是完全错的!!!
然后看看题解,要先把a+b的所有可能都存起来放到sum里,再大循环c,对sum二分。开始还觉得这个思路不是和我一开始的思路差不多吗?仔细一想,我大循环a,小循环b,一方面会有很多重复的a+b,重复带入算,费时,另一方面,每输入一个x,又要大循环啊,小循环b的,很费时。
#include <iostream>
#include <stack>
#include <string.h>
#include <stdio.h>
#include<queue>
#include<algorithm>
#define ll long long
using namespace std;
int a[];
int b[];
int c[];
int sum[];
int main()
{
int L,N,M;
int k=;
while(~scanf("%d %d %d",&L,&N,&M))
{
k++;
for(int i=;i<=L;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<=N;i++)
{
scanf("%d",&b[i]);
}
for(int i=;i<=M;i++)
{
scanf("%d",&c[i]);
}
int n,x;
printf("Case %d:\n",k);
scanf("%d",&n);
int p=; for(int i=;i<=L;i++)
{
for(int j=;j<=N;j++)
{
sum[p++]=a[i]+b[j];
}
}
sort(sum+,sum+p);
sort(c+,c++M);
while(n--)
{
scanf("%d",&x);
bool f=;
for(int i=;i<=M;i++)
{
if(sum[]>x-c[i])//不能写成sum[1]+c[1]>x,因为可能会爆int
break;
int le=;int ri=p-;
while(le<=ri)
{
int mid=(le+ri)/;
if(sum[mid]==x-c[i])
{
f=;
break;
}
else if(sum[mid]>x-c[i])
{
ri=mid-;
}
else if(sum[mid]<x-c[i])
le=mid+;
}
if(f)
break;
}
if(f)
printf("YES\n");
else
printf("NO\n");
}
}
return ;
}
 

最新文章

  1. Filter体现职责链模式
  2. css重新认识(2)
  3. [51NOD1095] Anigram单词(map)
  4. windows下查看所有进程以及pid
  5. 65. Valid Number
  6. Leetcode Python Solution(continue update)
  7. Jquery remove()和empty()
  8. 汽车总线obd模拟器,obd仿真器,ecu模拟器,obd开发测试工具,可以模拟ecu中的obd协议 MRD-5050
  9. 【 D3.js 入门系列 --- 4 】 怎样使用scale(比例)
  10. OOA/OOD/OOP(转)
  11. 7 Ways to earn money on programming(转)
  12. 从c#基础到java基础的学习的感悟
  13. [转载] RED-BLACK(红黑)树的实现TreeMap源码阅读
  14. jQuery 效果 – 淡入淡出
  15. 《Professional JavaScript for Web Developers》day02
  16. cookie 和 session区别
  17. Hadoop记录-HDFS配额Quota
  18. day5 用户交互 input用法
  19. HBase的Shell命令和JavaAPI
  20. NavicatForOracle无法连接数据库,报错ORA-28547

热门文章

  1. centos 升级python26到python27
  2. hdu 1004 Let the Balloon Rise strcmp、map、trie树
  3. Android ViewGroup等容器控件的使用
  4. org.springframework.transaction 包改成 spring-tx
  5. Mybatis出现参数错误Parameter &#39;cateCode_search&#39; not found. Available parameters are [3, 2, 1, 0, param1, param2, 5, param3, 4, param4, param5, param6]
  6. linux三尖刀
  7. hadoo异常——org.apache.hadoop.security.UserGroupInformation: PriviledgedActionException
  8. 在vue中使用scss的配置
  9. [转载]Java抽象类和接口的学习
  10. jQuery实现鼠标划过展示大图的方法