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. 自动化运维工具之ansible

    自动化运维工具之ansible   一,ansible简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fab ...

  2. 对N个数组进行操作。先把这N个一维数组合并成一个2为数组;然后进行操作

    using System;using System.Collections.Generic;using System.Linq;using System.Collections;using Syste ...

  3. js调用.net后台事件,和后台调用前台等方法以及js调用服务器控件的方法

    http://blog.csdn.net/deepwishly/article/details/6670942  ajaxPro.dll基础教程(前台调用后台方法,后台调用前台方法) 1. javaS ...

  4. WPF内嵌代码和后台代码简单混合使用

    下面实例展示了WPF内嵌代码和后台代码混合使用,一个简单基础的实例: xaml文件: <Window x:Class="WPF内嵌代码和后台代码混合使用.MainWindow" ...

  5. 2的N次方

    /**编程精确计算2的N次方.(N是介于100和1000之间的整数)*//*问题代码:#include<stdio.h>#include<math.h>int main(){ ...

  6. 基于TCP的NAT子网穿透实验

    不得不说,在国内IP紧缺的现状下,NAT发挥了无比巨大的作用:它以把IP和端口重新分配的方式,满足了广大人民群众上网的强烈需求.但是对于个人服务器以及在内网中基于网络的嵌入式设备,却是个比较尴尬的事情 ...

  7. debug(fmt,args...)调试

    1.定义宏(debug.h) #ifndef __DEBUG__H #define __DEBUG__H #include <stdio.h> #ifdef DEBUG #define d ...

  8. reverse string | leetcode

    思路:在原来的字符串后面添加上strlen-1个字符,返回 class Solution { public: string reverseString(string s) { unsigned int ...

  9. SqlServer 允许保存对数据库中表结构的修改

    1.Tools-->optisons-->Designers 如下截图

  10. 【关于php】Appserv中关于DW配置站点问题

    用DW运行的话,还要配置下站点.或者你直接在浏览器地址栏上输入:http://localhost:8080/p5-1.php  或者是http://localhost/p5-1.php dreamwe ...