HDU 2141~ ??Can you find it? 还是二分法~~??????

Can you find it?

Time Limit : 10000/3000ms (Java/Other)   Memory Limit : 32768/10000K (Java/Other)

Total Submission(s) : 274   Accepted Submission(s) : 87

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

纳闷儿的是我注释掉的明明可以减轻程序运行的负担~但实际上确实Wrong answer~注释掉就ac了~~~

AC-code:

Sample Output

Case 1:
NO
YES
NO
#include<stdio.h>
#include<algorithm>
using namespace std;
int A[505],B[505],C[505],num[500*500*500+10];
int main()
{
	int l,m,n,i,j,le,k,r,x,s,mid,p=1;
	while(~scanf("%d%d%d",&l,&m,&n))
	{
		for(i=0;i<l;i++)
			scanf("%d",&A[i]);
		for(i=0;i<m;i++)
			scanf("%d",&B[i]);
		for(i=0;i<n;i++)
			scanf("%d",&C[i]);

		printf("Case %d:\n",p++);
		scanf("%d",&s);
		int len=0;
		for(i=0;i<l;i++)
			for(j=0;j<m;j++)
				num[len++]=A[i]+B[j];
		sort(num,num+len);
		sort(C,C+n);
		while(s--)
		{
			k=0;
			scanf("%d",&x);
//			if(x<num[0]+C[0]||x>num[len-1]+C[n-1])
//			{
//				printf("NO\n");
//				continue;
//			}
			for(i=0;i<n;i++)
			{
				le=0;r=len-1;
				while(le<=r)
				{
					mid=(le+r)/2;
					if(x-num[mid]==C[i])
					{
						k=1;
						break;
					}
					else if(x-num[mid]<C[i])
						r=mid-1;
					else
						le=mid+1;
				}
				if(k)
					break;
			}
			if(k)
				printf("YES\n");
			else
				printf("NO\n");
		}
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-29 23:28:19

HDU 2141~ ??Can you find it? 还是二分法~~??????的相关文章

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

HDU 2141 Can you find it? (二分法)

Can you find it? Time Limit:3000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u 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

hdu 2141 Can you find it?

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2141 Can you find it? 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 formu

hdu 2141 (二分)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 11503    Accepted Submission(s): 3021 Problem Description Give you three seque

HDU 2141(二分&amp;三分 _B题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 ----------------------------------------------------------------------------------- 题意:三个数组,找到每个数组中加和为M的值的组数. 思路:首先将后两个数组加和 O(N^2),排序 O(NlogN),然后利用二分求解. 代码: #include<cstdio> #include<cstring>

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 Can you find it?(二分)

题目链接:点我点我 题意:给出L个A,N个B,M个C,然后S个X,求能否找出解使得Ai+Bj+Ck = X.成立 题解:TLE了一晚上.这道题其实就是个暴力二分,前把三组数中任意两组数先合并,只不过最后的时候转变一下思想,不要直接去求使X成立的条件, 而是反过去把X当作条件(但是有些大神直接set+set的find()函数过了,当我没说,(捂脸逃....) 1.巧妙暴力二分 1 #include <cstdio> 2 #include <algorithm> 3 using nam

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): 16411    Accepted Submission(s): 4172 Problem Description Give you three sequences of numbers A, B, C, then we give you a numbe

HDU 1950 Bridging signals (LIS,二分法,O(nlogn))

题意: 给一个数字序列,要求找到LIS,输出其长度. 思路: 扫一遍+二分,复杂度O(nlogn),空间复杂度O(n). 具体方法:增加一个数组,用d[i]表示长度为 i 的递增子序列的最后一个元素,且该元素总是保持当前最小.初始化d[1]=A[i],当前LIS的长度len=1.从 2 to n,若A[i]>d[len],则d[++len]=A[i],否则,在数组d中找到A[i]应该插入的位置,代替掉那个第一个比它大的数字,比如d[k]<A[i]<=d[k+1],直接将A[i]代替掉d[