hdoj 2141 Can you find it?【二分查找+暴力】

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

  

时间: 2024-11-13 10:31:35

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

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

题目传送门 1 /* 2 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 5e2 + 10; 10 const int MAXM = 1e6 + 10; 11 const int INF = 0

HDU 2141 Can you find it? 二分查找

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 21485    Accepted Submission(s): 5446 Problem Description Give you three sequences of numbers A, B, C, then we give you a number

hdu-2141(二分查找+暴力)

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 descr

二分查找 HDOJ 2141 Can you find it?

题目传送门 1 /* 2 题意:给出一个数,问是否有ai + bj + ck == x 3 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAX

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. 1 /* 2 3 x^2+6*x-7==y 4 输入y 求x 精确度为10^-5 5 0=<x<=10000 6 7 */ 8 #include <iostream> 9 #include <cstdio> 10 using namespace std; 11 int main (void) 1

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

题目传送门 题意:计算从1开始到第n个非完全平方数的开方和 分析:设第n个非完全平方数的值为a,x * x < a < (x+1) * (x+1),而且易得(tmp = sqrt (a) ) == x,a之前的非完全平方数的个数为a - tmp,所以可以二分查找a - tmp == n的a,然后模拟一下能计算出前a个数的开方和 收获:二分查找是个好方法 代码: /************************************************ * Author :Running

离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers

题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前花开的数量,放张图易理解: 还有一种做法用尺取法的思想,对暴力方法优化,对询问点排序后再扫描一遍,花开+1,花谢-1.详细看代码. 收获:一题收获很多:1. 降低复杂度可以用二分 2. 线段计数问题可以在端点标记1和-1 3. 离散化+线段树 终于会了:) (听说数据很水?) 代码1:离散化+线段树

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

二分查找

递归版(在区间[x, y)中找v的位置) 1 //递归版二分查找 2 int bsearch(int * A, int x, int y, int v) 3 { 4 5 if(v<a[x] || v>a[y-1]) return -1; 6 int m = x + (y-x)/2; //此处能不能用int m = (x+y)/2,需要仔细考虑(暂时想不到原因) 7 if(A[m]==v) return m; 8 else if(A[m]>v) return bsearch(A, x, m