首页 > ACM题库 > HDU-杭电 > hdu 2141 Can you find it?-分治-[解题报告]C++
2013
12-29

hdu 2141 Can you find it?-分治-[解题报告]C++

Can you find it?

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10

样例输出:

Case 1:
NO
YES
NO

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2141

直接三重循环肯定超时了啊,O(n^3)是不允许的,现在把前两个数组加起来的每种情况保存,再用X-C[i]的值去做二分搜索,这样可以把复杂度降低到O(n^2+nlogn)就可以AC了,不过做二分搜索的时候一定要注意边界情况,不然会WA很多次,尼玛的!!

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream> 
using namespace std;
int a[505],b[505],c[505];
int sum[505*505];
int l,n,m,k;
int flag;
void binary(int x)
{
	int left,right,mid;
	left=0,right=k-1;
	while(left<=right)
	{
		mid=(left+right)>>1;
		if(sum[mid]>x)
			right=mid-1;
		else if(sum[mid]<x)
			left=mid+1;
		else
			{flag=1;return ;}
	}
	return ;
}

int main()
{
	int i,j,q,x,cnt=1;
	while(cin>>l>>n>>m)
	{
		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]);
		k=0;
		for(i=0;i<l;i++)
			for(j=0;j<n;j++)
			{
				sum[k++]=a[i]+b[j];
			}
		sort(sum,sum+k);
		scanf("%d",&q);
		printf("Case %d:\n",cnt++);
		while(q--)
		{
			scanf("%d",&x);
		    flag=0;
			for(i=0;i<m;i++)
			{
				binary(x-c[i]);
				if(flag){
					printf("YES\n");
					break;
				}		
			}
			if(!flag)
				printf("NO\n");
		}
	}
	return 0;
}

解题转自:http://blog.csdn.net/xiaozhuaixifu/article/details/9067273


  1. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.

  2. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.

  3. for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
    dp = dp [j-1] + 1;
    if(s1.charAt(i-1) == s3.charAt(i+j-1))
    dp = dp[i-1] + 1;
    if(s2.charAt(j-1) == s3.charAt(i+j-1))
    dp = Math.max(dp [j - 1] + 1, dp );
    }
    }
    这里的代码似乎有点问题? dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false

  4. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c