• 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;
    }
    

      

     
  • 相关阅读:
    charCodeAt() 和charAt()
    去除全角空格
    string字符串js操作
    取小数的常见操作
    js取最大最小值
    js加减法运算多出很多小数点
    js设置div透明度
    setTimeout设置不起作用
    node.js 找不到 xxx 模块解决办法
    servlet 监听器和过滤器入门
  • 原文地址:https://www.cnblogs.com/tonghao/p/4689940.html
Copyright © 2020-2023  润新知