zoukankan      html  css  js  c++  java
  • HDU 2141 Can you find it? (二分法)

    Can you find it?

    Time Limit:3000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

     

     

    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
     

     
    Analysis
      像这种直接做找不到算法的题,一般都是用枚举、搜索这种“暴力”算法。
       这一题就是把 A+B 所有情况都存下来,对每一个 X ,把 C 移过来,令 X2 = X - C ,在 A+B 的数组中二分查找 X2 的值,如果找到一样的,输出YES,全部找不到,输出NO。
     
    TIPs
    • 在二分查找之前,一定要对 A+B 数组进行排序
    • 二分的退出条件:可以用for循环循环100次,可以达到 2100。如果那时找不到,则一定没有解了。

              也可以在循环中:

              while( front <= back )

              {

                     ......

               if( F[mid] > Z ) back = mid - 1;

               if(F[mid] < Z ) front = mid + 1;

               ......

               ......

              }

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <algorithm>
    
    using namespace std;
    
    int ab[500*500+1];
    int a[501],b[501],c[501];
    
    int main()
    {
        int na,nb,nc,nx,i,j,kkk=0;
        int x;
    
        while(~scanf("%d%d%d",&na,&nb,&nc))
        {
    
            for(i=0;i<na;i++)
                scanf("%d",&a[i]);
            for(i=0;i<nb;i++)
                scanf("%d",&b[i]);
            for(i=0;i<nc;i++)
                scanf("%d",&c[i]);
    
            int k=0,s,m,t;
            for(i=0;i<na;i++)
                for(j=0;j<nb;j++)
                    ab[k++] = a[i] + b[j];
            sort(ab,ab+na*nb);
    
            scanf("%d",&nx);
            int flag = 0;
            printf("Case %d:
    ",++kkk);
            for(i=0;i<nx;i++)
            {
                scanf("%d",&x);
                for(j=0;j<nc;j++)
                {
                    int x2;
                    x2 = x - c[j];
                    flag = 0;
                    s = 0;
                    t = na * nb - 1;
                    m = (s + t)/2;
                    if(ab[m] == x2) flag = 1;
                    while(s <= t)
                    {
                        if(ab[m] > x2)
                            t = m-1;
                        else s = m+1;
                        m = (s + t)/2;
                        //printf("s = %d t = %d
    ",s,t);
                        if(ab[m] == x2)
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if(flag)
                    {
                        printf("YES
    ");
                        break;
                    }
                    else continue;
                }
                if(!flag) printf("NO
    ");
            }
        }
        return 0;
    }

          

     
  • 相关阅读:
    N的阶乘:高精度
    蓝桥杯历届试题 连号区间数:枚举(含样例解释)
    最大公共子串:DP
    IncDec序列:差分+贪心
    [ACM] hdu 1465 不容易系列之一(错排复习)
    写给现在,写给未来
    [ACM] hdu 2082 找单词 (母函数)
    [ACM] poj 1146 ID Codes(字符串的下一个排列)
    [ACM] hdu 2149 Public Sale (巴什博奕)
    [ACM] hdu 1846 Brave Game (巴什博奕)
  • 原文地址:https://www.cnblogs.com/GY8023/p/4684439.html
Copyright © 2011-2022 走看看