Sacrament ofthe sum
— The Brother of mine, the Head of Monastic Order wants to know tomorrow aboutthe results long-term researches. He wants to see neither more nor less thanthe Summering Machine! Even moreover, he wants our Machine — only a machine — to demonstrate its comprehension ofthe Sacrament ofthe Sum as deeply asitis possible. He wants our Machine to find two numbers that give the sum equaltothe Sacred Number 10000.
— Tsh-sh-sh! This is madness that borders on blasphemy! How can the Machine calculate the Sacred Number? Twenty seven years we work onit, but we've could teach ittotellifthe sum of two introduced numbers greater or lower than 10000. Can an ordinary mortal find two numbers that there sum will be equalto10000?
— But we'll have to do itwiththe help of our Machine, even ifitisnot capable. Otherwise we'll have... let's say, big problems, ifitis possible to call boiling oil like this. However, I have an idea. Do you remember, last week we've entered two numbers -7and13intothe Machine, andit answered that their sum is lower than 10000. I don't know how to check this, but nothing's left for us than to believe tothe fruit of our work. Let's enter now a greater number than -7and start up the Machine again. We'll do like this again and again until we find a numberthat being added to13 will give us 10000. The only thing we are to do isto prepare an ascending listof numbers.
— I don't believe in this... Let's start withthe sum thatis obviously greater thanthe Sacred Number and we'll decrease one ofthe 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 listof numbers that, to his opinion, could save them... Can both ofthe lists save them together?
Your program should decide, ifitis possible to choose from two lists of integers such two numbers that their sum would be equalto10000.
Input
You are given both of these lists one by one. Format of each of these lists isas follows: inthefirst line ofthelistthe quantity of numbers Ni ofthe i-th listis written. Further there is an i-th listof numbers each numberinits line (Ni lines).The following conditions are satisfied: 1 <= Ni <= 50000, each element ofthe lists lays inthe range from -32768to32767. The firstlistis ascending andthesecond one is descending.
Output
You should write"YES"tothe standard output ifitis possible to choose fromthe two lists of integers such two numbers that their sum would be equalto10000. Otherwise you should write"NO".
Sample Input
4
-17519191042438951
-424
-788
Sample Output
YES
Hint
This problem has huge input data,use scanf() instead of cin toread data to avoid time limit exceed.
Source
Ural State University Internal Contest October'2000 Junior Session
题意:
给你两组数让你判断是个否有 在两组分别取一个数相加为10000
解:
直接而二分
#include<cstdio>#include<iostream>#include<algorithm>usingnamespacestd;
int num1[50005],num2[50005];
int main()
{
int n,m,i,j;
while(cin>>n)
{
for(i=0;i<n;i++)
scanf("%d",num1+i);
cin>>m;
for(i=0;i<m;i++)
scanf("%d",num2+i);
sort(num1,num1+n);
int f=0;
for(i=0;i<m;i++)
{
int x=10000-num2[i];
int low=0, high=n-1;
while(low<=high)
{
int mid=(low+high)/2;
if(num1[mid]>x)
high=mid-1;
elseif(num1[mid]<x)
low=mid+1;
else
{
puts("YES");
f=1;
break;
}
}
if(f) break;
}
if(!f)
puts("NO");
}
return0;
}