zoukankan      html  css  js  c++  java
  • POJ2366(HASH法)

                                                                              Sacrament of the sum
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 1628   Accepted: 756

    Description

    — The Brother of mine, the Head of Monastic Order wants to know tomorrow about the results long-term researches. He wants to see neither more nor less than the Summering Machine! Even moreover, he wants our Machine — only a machine — to demonstrate its comprehension of the Sacrament of the Sum as deeply as it is possible. He wants our Machine to find two numbers that give the sum equal to the Sacred Number 10 000. 
    — Tsh-sh-sh! This is madness that borders on blasphemy! How can the Machine calculate the Sacred Number? Twenty seven years we work on it, but we've could teach it to tell if the sum of two introduced numbers greater or lower than 10 000. Can an ordinary mortal find two numbers that there sum will be equal to 10 000? 
    — But we'll have to do it with the help of our Machine, even if it is not capable. Otherwise we'll have... let's say, big problems, if it is possible to call boiling oil like this. However, I have an idea. Do you remember, last week we've entered two numbers -7 and 13 into the Machine, and it answered that their sum is lower than 10 000. I don't know how to check this, but nothing's left for us than to believe to the fruit of our work. Let's enter now a greater number than -7 and start up the Machine again. We'll do like this again and again until we find a number that being added to 13 will give us 10 000. The only thing we are to do is to prepare an ascending list of numbers. 
    — I don't believe in this... Let's start with the sum that is obviously greater than the Sacred Number and we'll decrease one of the summand. So we have more chances to avoid boilin... big problems. 

    Haven't come to an agreement, the Brothers went away to their cells. By next day everyone of them has prepared a list of numbers that, to his opinion, could save them... Can both of the lists save them together? 
    Your program should decide, if it is possible to choose from two lists of integers such two numbers that their sum would be equal to 10 000.

    Input

    You are given both of these lists one by one. Format of each of these lists is as follows: in the first line of the list the quantity of numbers Ni of the i-th list is written. Further there is an i-th list of numbers each number in its line (Ni lines).The following conditions are satisfied: 1 <= Ni <= 50 000, each element of the lists lays in the range from -32768 to 32767. The first list is ascending and the second one is descending.

    Output

    You should write "YES" to the standard output if it is possible to choose from the two lists of integers such two numbers that their sum would be equal to 10 000. Otherwise you should write "NO".

    Sample Input

    4
    -175
    19
    19
    10424
    3
    8951
    -424
    -788
    

    Sample Output

    YES

    Hint

    This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
     
    表示刚听斌神讲了下HASH法,虽然以前用这种方法做过,但还没怎么在脑海里形成系统的方法,这道题跟HDU-2141有些像,所以我没有写二分方法,虽然二分肯定能过,根据题目给的数据,可知只要开一个10万大小的数组,是可以用HASH法判断的,HASH最好的地方在于,不用排序,也不用开其他数组来存元素,读完马上拿来用就可以了,。。至于判断是不是相加等于一万,则用一点小小的技巧,即判断50000+a=60000-b,放到HASH法里,即可很方便的得出结果
    因为BOOL类型只占一个字节,所以,把HASH数组定义为布尔类型比较节省内存,这样也遍开启更大的数组,虽然本题不需要
    PS.这个题目的HASH法是我参考了别人的博客才写的,原先也是用的二分法,后来用HASH法重写了。
     
     

    #include <cstdio>
    #include <cstring>
    bool hash[100000];

    int main()
    {
    int a,b;
    int n,m;
    while (scanf("%d",&n)==1)
    {
        memset(hash,0,sizeof(hash));
        bool ans=false; 
        while (n--)
       {
         scanf("%d",&a);
         hash[50000+a]=1;
       }
       scanf("%d",&m);
       while (m--)
      {
        scanf("%d",&b);
        if (hash[60000-b])
        {
           ans=true;
        }
      }
     if (ans) printf("YES\n");
     else printf("NO\n");
    }
    return 0;
    }

     
     
  • 相关阅读:
    Dubbo与Eureka
    对称加密与非对称加密
    [转] SpringBoot2.0集成WebSocket,实现后台向前端推送信息
    [转] Druid简介(Spring Boot + Mybatis + Druid数据源【自己定制】)
    [转] rsync+inotify实现文件实时同步
    [转] windows server 几大实时同步软件比较
    [转] Springboot 整合RabbitMq ,用心看完这一篇就够了
    [转] Windows环境下部署RabbitMQ
    [转] 分布式缓存 Redis 集群搭建
    [转] 吞吐量(TPS)、QPS、并发数、响应时间(RT)概念
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2871635.html
Copyright © 2011-2022 走看看