NBUT-2014暑期集训专题练习1 -> 二分法F - F
来源:互联网 发布:完美告白知乎 编辑:程序博客网 时间:2024/06/06 21:44
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.
— 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".
这题比较水,二分,时间复杂度O(NLOGN)
#include<stdio.h>
#include<string.h>
int a[50010],b[50010];
int main()
{
int n1,n2;
while(~scanf("%d",&n1))
{
int i;
for(int i=0;i<n1;i++)
scanf("%d",&a[i]);
scanf("%d",&n2);
for(int j=0;j<n2;j++)
scanf("%d",&b[j]);
bool f=false;
for(i=0;i<n1;i++)
{
int l=0,r=n2-1,mid;
while(l<=r)
{
mid=(l+r)>>1;
if(a[i]+b[mid]==10000)
{
printf("YES\n");
f=true;
break;
}
else if(a[i]+b[mid]>10000)//数太大,b递减,往后
l=mid+1;
else
r=mid-1;
}
if(f)
break;
}
if(!f)
printf("NO\n");
}
return 0;
}
0 0
- NBUT-2014暑期集训专题练习1 -> 二分法F - F
- NBUT-2014暑期集训专题练习1 -> 二分法 A题
- NBUT-2014暑期集训专题练习1 -> 二分法E - E
- NBUT-2014暑期集训专题练习1 -> 二分法 J - J
- NBUT-2014暑期集训专题练习1 -> 二分法 H - H
- NBUT-2014暑期集训专题练习1 -> 二分法B - B
- NBUT-2014暑期集训专题练习1 -> 二分法L - L
- NBUT-2014暑期集训专题练习1 -> 二分法 N - N
- 2015年ALPC暑期专题练习I (计算几何) F Pick-up sticks
- nbut线段树专题F - A Simple Problem with Integers
- 【dp专题1】F
- 练习1-f
- 暑假集训——贪心专题——F题
- 【字符串入门专题1】F
- 二分法二 F
- 2013秋13级预备队集训练习1 F - Machined Surfaces
- hdu2629 Identity Card (2016xynu暑期集训检测 -----F题)
- [kuangbin带你飞]专题四 最短路练习 -F
- NBUT-2014暑期集训专题练习1 -> 二分法E - E
- NBUT-2014暑期集训专题练习1 -> 二分法 J - J
- NBUT-2014暑期集训专题练习1 -> 二分法 H - H
- NBUT-2014暑期集训专题练习1 -> 二分法B - B
- NBUT-2014暑期集训专题练习1 -> 二分法L - L
- NBUT-2014暑期集训专题练习1 -> 二分法F - F
- nbut线段树专题E - Just a Hook
- nbut线段树专题F - A Simple Problem with Integers
- nbut线段树专题I - Balanced Lineup
- <vim>VIM各种tips
- nbut线段树专题C - Mayor\'s posters
- nbut线段树专题W - Frequent values
- nbut线段树专题M - Buy Tickets
- nbut线段树专题 J - Atlantis