Can you find it?

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

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
题意:给你三组数,每组数的个数分别为L N M再给你S个数x判断能否从这三组数中分别找到一个数使Ai+Bj+Ck = X.
题解:暴力肯定超时,所以要用二分,先将三组数中任意两组存入数组str[]中,然后再对每个x进行遍历
1、测试数据太弱,而且这样的一组测试数据本来就是个坑,好多不正确的情况都能得出正确答案
2、输出格式好坑,本来我以为是连续输出NO YES NO一直错没想到竟然是输一个数出一个结果
3、一定要注意给str[]数组排序(二分查找的条件是有序数列)
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAX 1100
bool cmp(int a,int b)
{
return a<b;
}
int a[MAX];
int b[MAX];
int c[MAX];
int d,str[300000];
int main()
{
int n,m,j,i;
int k=1,l;
while(scanf("%d%d%d",&l,&n,&m)!=EOF)
{
for(i=0;i<l;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<m;i++)
scanf("%d",&c[i]);
int len=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
str[len++]=b[i]+c[j];
}
}
sort(str,str+len,cmp);
int ok;
printf("Case %d:\n",k++);
int s;
scanf("%d",&s);
for(i=0;i<s;i++)
{
scanf("%d",&d);
ok=0;
for(j=0;j<l;j++)
{
int left = 0,right = len,mid = 0;
int goal = d - a[j];
while(right >= left)
{
mid = (right + left) >> 1;
if(str[mid] < goal)
left = mid + 1;
else if(str[mid] > goal)
right = mid - 1;
else
{
ok=1;
break;
}
}
if(ok)
break;
}
if(ok)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}

  

 

hdoj 2141 Can you find it?【二分查找+暴力】的更多相关文章

  1. 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

    题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...

  2. what the fuck!(二分查找 / 暴力模拟)

    what the fuck! Description 现在有一家公司有nnn个员工(nnn为奇数),他们的工资发放是基本工资+提成,现在这家公司计划再招一批人.要写一篇招聘启事,但是对于这个招聘启事中 ...

  3. 二分查找 HDOJ 2141 Can you find it?

    题目传送门 /* 题意:给出一个数,问是否有ai + bj + ck == x 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x */ ...

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

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

  5. hdu 2141 Can you find it?(二分查找变例)

    Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo ...

  6. Can you find it? HDU - 2141 (二分查找)

    Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...

  7. hdu 2141:Can you find it?(数据结构,二分查找)

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

  8. HDU 2141 Can you find it?【二分查找是否存在ai+bj+ck=x】

    Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...

  9. 二分查找+数学 HDOJ 4342 History repeat itself

    题目传送门 题意:计算从1开始到第n个非完全平方数的开方和 分析:设第n个非完全平方数的值为a,x * x < a < (x+1) * (x+1),而且易得(tmp = sqrt (a) ...

随机推荐

  1. CoreAnimation1-图层树、寄宿图以及图层几何学

    (一)图层的树状结构 Core Animation其实是一个令人误解的命名.你可能认为它只是用来做动画的,但实际上它是从一个叫做Layer Kit这么一个不怎么和动画有关的名字演变而来,所以做动画这只 ...

  2. mysql 表操作

    创建表 简单的方式 CREATE TABLE person ( number INT(11), name VARCHAR(255), birthday DATE ); 或者是 CREATE TABLE ...

  3. Codevs 4768 跳石头 NOIP2015 DAY2 T1

    4768 跳石头 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 一年一度的"跳石头"比赛又要开始了! ...

  4. java.net.ServerSocket和java.net.Socket

    个人博客地址:http://www.cnblogs.com/wdfwolf3/ java.net.ServerSocket 1.构造函数 a.ServerSocket() 创建一个无连接的server ...

  5. PHP 编译问题PEAR package PHP_Archive not installed的解决

    php 的编译时需要依赖pear package ,目前的问题错误"PEAR package PHP_Archive not installed",已经明显报出这个问题. 因此编译 ...

  6. android入门到熟练(二)----活动

    1.活动创建对于每一个后端java类(继承至Activity或者ActionBarActivity)代码都有一个方法需要被重写[onCreate], 在此方法中可以加载界面资源文件或者绑定元素事件. ...

  7. Google HTML/CSS/JS代码风格指南

    JS版本参见:http://www.zhangxinxu.com/wordpress/2012/07/google-html-css-javascript-style-guides/ HTML/CSS ...

  8. DOM中的范围 createRange()

    学习<JavaScript 高级程序设计> 12章dom范围的笔记 dom2级在Document类型中定义了 createRange()方法: 创建range对象很简单 var range ...

  9. 子网/ip/子网掩码

    IP地址由网络地址和主机地址组成 而现在IP由“子网掩码”通过子网网络地 址细分出 A,B,C类更小的网络.这种方式 实际上就是将原来的A类,B类,C类等分类 中的的主机地址部分用作子网地址,可以 将 ...

  10. Cinder-1 TinderBox

    Cinder:http://libcinder.org/,当前版本是0.8.5,代码托管位置:https://github.com/cinder/Cinder.git 下载Cinder之后,其目录结构 ...