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. ASP.NET WEB API 2: HTTP MESSAGE LIFECYLE

    https://www.asp.net/media/4071077/aspnet-web-api-poster.pdf 1.You can host Web API inside IIS or ins ...

  2. [SpringBoot] - 了解什么是SpringBoot,使用SpringBoot的配置文件

    首先明白Spring是什么,Spring是Java开发的一个框架,为了方便简化Java开发. 什么是注解(注解式开发)? Spring的常用注解有哪些? 假如用SpringBoot构建一个网站程序,应 ...

  3. eclipse中导入maven项目:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.Maven

    org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter) 解决方法为:更新eclipse中的maven插件 1.help ...

  4. http cookie的domain使用

    问题描述 最近遇到了一个因cookie domain设置不正确导致公司自研的分布式session组件无法生效的问题. 公司自研的这套分布式session组件依赖于设置在cookie中的sessionI ...

  5. domain---Node.js 异步异常的处理与domain模块解析

    var domain = require('domain'); app.use(function (req, res, next) { var reqDomain = domain.create(); ...

  6. 使用方法拦截机制在不修改原逻辑基础上为 spring MVC 工程添加 Redis 缓存

    首先,相关文件:链接: https://pan.baidu.com/s/1H-D2M4RfXWnKzNLmsbqiQQ 密码: 5dzk 文件说明: redis-2.4.5-win32-win64.z ...

  7. 阿里云服务器购买 发布web项目全过程

    http://blog.csdn.net/liona_koukou/article/details/50496946

  8. 控制反转(IOC)模式

    控制反转(Inversion of Control):提倡实现松耦合层.组件和类的设计原则,颠倒程序的控制流程.IoC使用分离执行特定问题处理代码的概念: IoC意味着将你设计好的对象交给容器控制,而 ...

  9. jquery+html5制作超酷的圆盘时钟表

    自己封装的一个用HTML5+jQuery写的时钟表 代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...

  10. MySQL根据表字段生成C#Model语句

    USE INFORMATION_SCHEMA;SELECT CONCAT('/// <summary>\r\n/// ',COLUMN_COMMENT,'\r\n/// </summ ...