• HDU2141【hash】


    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

    分析:

    用10^4打表  用10^2枚举

    我的lower_bound一直挂  只能手写二分  不过还好

    代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int maxn = 505;
     8 int a[maxn], b[maxn], c[maxn], d[maxn * maxn];
     9 
    10 int cnt;
    11 bool check(int num) {
    12     int low = 0; int high = cnt - 1;
    13     while(low <= high) {
    14         int mid = ( low + high) >> 1;
    15         if(d[mid] >= num) {
    16             high = mid - 1;
    17         } else {
    18             low = mid + 1;
    19         }
    20     }
    21     if(d[high + 1] == num) return true;
    22     return false;
    23 }
    24 
    25 int main() {
    26     int l, n, m;
    27     int t = 1;
    28     while(EOF != scanf("%d %d %d",&l, &n, &m) ) {
    29         for(int i = 0; i < l; i++) scanf("%d",&a[i]);
    30         for(int i = 0; i < n; i++) scanf("%d",&b[i]);
    31         for(int i = 0; i < m; i++) scanf("%d",&c[i]);
    32         cnt = 0;
    33         for(int i = 0; i < l; i++) {
    34             for(int j = 0; j < n; j++) {
    35                 d[cnt++] = a[i] + b[j];
    36             }
    37         }
    38         sort(d, d + cnt);
    39         int s;
    40         int num;
    41         printf("Case %d:
    ", t++);
    42         scanf("%d",&s);
    43         while(s--) {
    44             scanf("%d",&num);
    45             bool flag = false;
    46             for(int i = 0; i < m; i++) {
    47                 if(check(num - c[i]) ) {
    48                     flag = true;
    49                     break;
    50                 }
    51             }
    52             if(flag) puts("YES"); 
    53             else puts("NO");
    54         }
    55     }
    56 }
    View Code
  • 相关阅读:
    2017年5月19日13:58:17 问题记录
    2017年5月17日20:14:29 rabbitmq 消费 异常信息无法处理 导致轮询
    2017年5月12日15:10:46 rabbitmq不支持非阻塞调用服务器
    2017年5月11日17:43:06 rabbitmq 消费者队列
    2017年5月10日16:11:28 定义所有问题
    MyBatis Plus 只插入只有自增id字段的表
    Centos 7 关闭报警音
    关于git项目切换远程提交路径和提交仓库
    IDEA通过git回滚到某个提交节点或某个版本
    IDEA使用@Autowired注解报错解决方案
  • 原文地址:https://www.cnblogs.com/zhanzhao/p/4063532.html
Copyright © 2020-2023  润新知