码迷,mamicode.com
首页 > 其他好文 > 详细

hdoj2141 Can you find it?

时间:2015-07-31 10:47:57      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:二分查找   hdoj2141   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): 17531    Accepted Submission(s): 4432


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

原题目:hdoj2141
个人思路:
三组数据,先把前两组相加并排序,然后拿所取出的值减去第三组的值,并与前两组的和进行比较即可!具体代码分析:
我写了两种,stl和二分:
s t l:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[250050],b[250050],c[250050],d[250050];
int main()
{
	int A,B,C,i,j;
	 int w=1;
	while(scanf("%d%d%d",&A,&B,&C)!=EOF)
	{
		int l=0;
		for(i=0;i<A;i++)
		 scanf("%d",&a[i]);
		for(i=0;i<B;i++)
		 scanf("%d",&b[i]);
		for(i=0;i<C;i++)
		 scanf("%d",&c[i]);
		   for(i=0;i<A;i++)
		     for(j=0;j<B;j++)
		       d[l++]=a[i]+b[j];
		    sort(d,d+l);//前两组相加并排序
		    sort(c,c+C);
		 int n,m;
		 printf("Case %d:\n",w++);
		 scanf("%d",&n);
		 while(n--)
		 	int y,t;
		 	int temp=0;
		 	scanf("%d",&m);
		 	for(i=0;i<C;i++)
		 	 {
		 	 	y=m-c[i];
		 	 	t=lower_bound(d,d+l,y)-d;//stl中找到大于或等于y的地址值,再减去首地址即得到数组所在下标
		 	 	if(y==d[t])// /看该下标的值是否与y相等
		 	 	{
		 	 		temp=1;
		 	 		break;
				}
			  }
			  if(temp==1)
			  printf("YES\n");
			  else
			  printf("NO\n");
		 }
	}
	return 0;
}
二分查找:
#include<iostream>
#define MAX 251000
#include<algorithm>
using namespace std;
int L[MAX],N[MAX],M[MAX],a[MAX];
int main()
{
    int l,n,m,cas=1;
    while(cin>>l>>n>>m)
    {
        int i,j,t,k;
        for(i=0;i<l;++i)
            cin>>L[i];
         for(j=0;j<n;++j)
            cin>>N[j];
        for(t=0;t<m;++t)
            cin>>M[t];
        k=0;
        for(i=0;i<l;i++)
            for(j=0;j<n;++j)
                a[k++]=L[i]+N[j];
//            cout<<"            "<<k<<endl; 
        sort(a,a+k);
        sort(M,M+m);

        cout<<"Case "<<cas++<<":"<<endl;
        int s,x,y,temp;
        cin>>s;
        while(s--)
        {
            temp=0;
            cin>>x;
            for(i=0;i<m;++i)
            {
                y=x-M[i];
                int left,right,mid;
                left=0;
                 right=k-1;
                if(a[k-1]<y)
                    continue; 
                while(left<=right)
                {
//                    cout<<left<<"              "<<right<<endl;
                    mid=(left+right)/2;
                    if(a[mid]==y)
                    {
                        temp=1;
                        break;
                    }
                    if(a[mid]>y)
                        right=mid-1;
                    else
                        left=mid+1;
                }

            }
            if(temp)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
    }
    return 0;
}







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

hdoj2141 Can you find it?

标签:二分查找   hdoj2141   can you find   it   

原文地址:http://blog.csdn.net/zhangxiaoxiang123/article/details/47164635

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!