zoukankan      html  css  js  c++  java
  • 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:
    ",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
    ");
    			else
    			    printf("NO
    ");
    		}
    	}
    	return 0;
    }
    

      

     
  • 相关阅读:
    文本框只能填数字
    Win7 IIS 配置错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的
    C#中小数点后保留两位小数,四舍五入的函数及使用方法(转)
    数据库中where与having区别
    NET Runtime version 2.0.50727.42
    C盘文件过大,C盘空间莫名丢失,pagefile.sys文件
    VS2008水晶报表变两页(重装系统后)
    win8 IIS配置
    从程序员到项目经理(5):程序员加油站 -- 懂电脑更要懂人脑
    从程序员到项目经理(4):程序员加油站 -- 不是人人都懂的学习要点
  • 原文地址:https://www.cnblogs.com/tonghao/p/4689940.html
Copyright © 2011-2022 走看看