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 ;
}
 

HDU 2143 Can you find it?(基础二分)的更多相关文章

  1. HDU 2254 奥运(矩阵高速幂+二分等比序列求和)

    HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意:  中问题不解释. 分析:  依据floyd的算法,矩阵的k次方表示这个矩阵走了k步.  所以k ...

  2. hdu 2143 数组合并 二分

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

  3. hdu.. 基础二分的精度问题

    #include<stdio.h>#include<iostream>using namespace std;double f(double x){ return 8*x*x* ...

  4. HDU 2236:无题II(二分搜索+二分匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2236 题意:中文题意. 思路:先找出最大和最小值,然后二分差值,对于每一个差值从下界开始枚举判断能不能二分匹配. ...

  5. HDU 4282 A very hard mathematic problem 二分

    A very hard mathematic problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sh ...

  6. hdu 1839 Delay Constrained Maximum Capacity Path 二分/最短路

    Delay Constrained Maximum Capacity Path Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu. ...

  7. HDU 5954 - Do not pour out - [积分+二分][2016ACM/ICPC亚洲区沈阳站 Problem G]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5954 Problem DescriptionYou have got a cylindrical cu ...

  8. HDU 2141 Can you find it? (二分)

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

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

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

随机推荐

  1. 代理模式:利用JDK原生动态实现AOP

    代理模式:利用JDK原生动态实现AOP http://www.cnblogs.com/qiuyong/p/6412870.html 1.概述 含义:控制对对象的访问. 作用:详细控制某个(某类)某对象 ...

  2. vue 脚手架(二,项目依赖说明 package.json)

    本文以转移至本人的个人博客,请多多关注! 本文以转移至本人的个人博客,请多多关注! 本文以转移至本人的个人博客,请多多关注! 本文以转移至本人的个人博客,请多多关注! 继续上一篇, 上一篇讲了 vue ...

  3. thinkphp3.2笔记(4)模板函数的使用 foreach标签

    一  模板函数的使用 1.代码 效果: 函数会按照从左到右的顺序依次调用.如果你觉得这样写起来比较麻烦,也可以直接这样写:{:substr(strtoupper(md5($name)),0,3)} 默 ...

  4. Manacher练习

    看这篇博客学了下Manacher, 讲的很好, 但他的板子写错了.. https://www.cnblogs.com/Lyush/p/3221503.html 练习1 hdu 3068最长回文 板子题 ...

  5. Linux命令详解-printf

    printf命令格式化并输出结果到标准输出. 1.命令格式: printf (选项) (参数) 2.命令功能: echo会将输入的字符串送往标准输出.输出的字符串间以空白字符隔开,并在最后加上换行号. ...

  6. 排序算法总结(基于Java实现)

    前言 下面会讲到一些简单的排序算法(均基于java实现),并给出实现和效率分析. 使用的基类如下: 注意:抽象函数应为public的,我就不改代码了 public abstract class Sor ...

  7. AppCrawler自动化遍历使用详解(版本2.1.0 )

    AppCrawle是自动遍历的app爬虫工具,最大的特点是灵活性,实现:对整个APP的所有可点击元素进行遍历点击.   优点: 1.支持android和iOS, 支持真机和模拟器 2.可通过配置来设定 ...

  8. python批量给云主机配置安全组

    python批量给云主机配置安全组 用公有云的思路去思考去实现一个安全稳定.可伸缩和经济的业务构架,云运维是有别与传统运维的,比如说了解公有云的都知道安全组的概念,安全组跟防火墙功能很相似,那我的机器 ...

  9. BZOJ1555 KD之死

    如果没有必选的限制条件,就是水题了... 只要按照w + t排序就可以了,然后搞个堆来维护 于是有了限制条件,还是水题... 到了必选的时候强制选上,不加入堆中即可. /*************** ...

  10. 快速切题 sgu134.Centroid 树形dp

    134. Centroid time limit per test: 0.25 sec. memory limit per test: 4096 KB You are given an undirec ...