【二分查找】[POJ2366]Sacrament of the sum

10 篇文章 0 订阅
2 篇文章 0 订阅

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.

题目大意

从第一次给出的N个数中和第二次给出的M个数中任意选出两个数,如果它们的和为10000,输出YES,否则输出NO。

分析

这道题是一道典型的二分查找例题,读入两组数据A,B,排序其中的一组(代码给出的是排序的第二组B),然后循环求出需要配对的数x=10000-A[i],在经过排序的B组数据中二分搜索即可。
说得有点乱,详细看代码。

源代码

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=50001;
int n,m,a[N],b[N];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
        scanf("%d",&b[i]);//读入
    sort(b+1,b+m+1);//排序
    int l,r,mid;
    for(int i=1;i<=n;i++){
        int x=10000-a[i];//求出a[i]应该匹配的数
        l=1;r=m;
        while(l<=r){//二分查找
            mid=(l+r)/2;
            if(b[mid]<x) l=mid+1;//找到的数比应该匹配的数小
            else if(b[mid]==x){puts("YES");return 0;}//找到,退出程序
            else r=mid-1;//找到的数比应该匹配的数大
        }
    }
    puts("NO");//没有找到
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值