`
hellojyj
  • 浏览: 58948 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

HDU 2141 Can you find it?

    博客分类:
  • ACM
阅读更多

原题传送门: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): 10636    Accepted Submission(s): 2786


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
 

 

Author

 

wangye
 

 

Source

 

 

 

 

Recommend

 

威士忌   |   We have carefully selected several similar problems for you:  2199 2899 2289 1597 1551
 
分析:这道题目一共有三个序列集L N M 很显然直接暴力三个for肯定超时,就算两个for for 然后第三个用二分,这也是超时的。那么能用一个for和一个二分解决吗,显然是可以的。首先我们班L,N两个序列集合并为一个。定义一个新的 数组用来存储所有L,N集中和的情况,就是for for(A[n++] = L[i]+N[i]),然后再用遍历A集合,每一步在M集合中二分搜索是否有符合x-A[i]的值(利用这个公式就可以转换下了C = X-(A+B)),如果有就YES 木有就NO;
如果你这么天真地想,恭喜你你又得超时了。既然二分可以提高搜索速度,我们为什么不把A拿来二分搜索呢?A中的元素最多会有25W个啊!!如果二分就是10^3级别的。。搜索次数,大大减小时间。如果拿M来二分,你只能从500到20左右。
OJ也证明了这一点,代码如下:
//搜索 HDU2141
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAXN 510
int AL[MAXN];
int AN[MAXN];
int A[MAXN*MAXN];
int AM[MAXN];

int L,M,N,x,s,k,temp,n;
bool res;
bool binSearch(int target,int len){
    int low = 0;
    int high = len - 1;
    int mid;
    while(low<high)
    {
        mid = (low+high)/2;
        if(A[mid] == target)return true;
        else if(target>A[mid]) low = mid+1;
        else high = mid;
    }
    return false;

}
int main()
{
    k = 1;

    while(scanf("%d%d%d",&L,&N,&M)==3)
    {
        n = 0;
        res = false;
        for(int i=0;i<L;i++)
            scanf("%d",AL+i);
        for(int i=0;i<N;i++)
            scanf("%d",AN+i);
        for(int i=0;i<M;i++)
            scanf("%d",AM+i);
        for(int i=0;i<L;i++){
            for(int j=0;j<N;j++){
                 A[n++]= AL[i]+AN[j];
            }
        }
        sort(A,A+n);
        sort(AM,AM+M);
        scanf("%d",&s);
        printf("Case %d:\n",k++);
        while(s--){
            scanf("%d",&x);
            for(int i=0;i<M;i++){
                res = binSearch(x-AM[i],n);
                if(res){
                    printf("YES\n");
                    break;
                }
            }
            if(!res){
                printf("NO\n");
            }
        }
    }
    return 0;
}
 
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics