zoukankan      html  css  js  c++  java
  • HDU2141 Can you find it?(搜索,二分)

    题目链接

    分析:

    一看这题还以为是暴搜呢。果断TLE。

    第二次将三个数组合并,二分查找,MLE。

    最后将两个两个数组合并,二分查找 AC。。。不容易啊。

    AC代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXN 510
    
    int l, m, n;
    
    int num1[MAXN], num2[MAXN], num3[MAXN], num4[MAXN*MAXN];
    
    int cmp(const void *a, const void *b){
        return *(int *)a - *(int *)b;
    }
    
    int main(){
        int flag, cnt=0, i, j, x, ans, s, ic;
    
        while(scanf("%d %d %d", &l, &n, &m) == 3){
            cnt++;
    
            for(i=0; i<l; i++) scanf("%d", &num1[i]);
            for(i=0; i<n; i++) scanf("%d", &num2[i]);
            for(i=0; i<m; i++) scanf("%d", &num3[i]);
    
            qsort(num1, l, sizeof(int), cmp);
    
            int *p = num4;
            for(i=0; i<n; i++)
                for(j=0; j<m; j++)
                    *p++ = num2[i] + num3[j];
    
            qsort(num4, n*m, sizeof(int), cmp);
    
            scanf("%d", &s);
            printf("Case %d:\n", cnt);
            for(ic=0; ic<s; ic++){
                flag = 0;
                scanf("%d", &x);
                for(i=0; i<l && !flag; i++){
                    ans = x - num1[i];
                    int low=0, mid, high=n*m-1;
                    while(low <= high){
                        mid = (low+high)/2;
                        if(num4[mid] == ans) {flag = 1; break;}
                        else if(num4[mid] < ans) low = mid+1;
                        else high = mid-1;
                    }
                }
                if(flag) printf("YES\n");
                else printf("NO\n");
            }
        }
    
    
        return 0;
    }
  • 相关阅读:
    js正则表达式——数字校验
    php curl cookie 读写
    Java遍历所有网卡打印对应IP
    mysql 查看数据库大小
    执行mvn 报错 source-1.5 中不支持 diamond运算符
    php开学之环境搭建
    ubuntu漂亮主题
    CleanAop使用笔记
    python学习笔记(五)
    python学习笔记(四)
  • 原文地址:https://www.cnblogs.com/tanhehe/p/2932440.html
Copyright © 2011-2022 走看看