And was given a number (dichotomy)

And was given a number

Jun garlic gives several integers, asking whether there is a pair of numbers and equal to a given number.

Input format
total of three lines:
the first line is an integer of n (0 <n \ le 100,000 ) n (0 <n≤100,000), expressed nn integers.

The second line is nn integer. It is an integer ranging from between 00 to 2 \ ^ Times 10 82 × 10
. 8
between.

The third line is an integer m (0 \ Le m \ 2 ^ {30} Le) m (0≤m≤2
30
), and expressed the need.

Output format
if present and p, an output two integers, small front, after a large, separated by a single space of several mm. If the number of the plurality of conditions are satisfied, the number of the smaller number of selected smaller. If the number can not be found to meet the requirements of the output line "No".

Extra spaces at the end of each row, the answer does not affect the validity of the output

Sample input

4

2 5 1 4

6

Sample Output

1 5

Description ideas

First, I would like to mark array, the presence of f [i] = 1 The number; does not exist, f [i] = 0;
then only needs to determine the front to back f [mi] is equal to 1, equal outputs when traversing the these two numbers, ranging continue to traverse.

(Simple face)

but! ! ! I think that the data is only 10,000, but this is just the number of sets of data, and the data size of a staggering 2 * 10 ^ 8, so I can not open large arrays it! ! !
So the idea was pass out.

Then remembered that just the idea of ​​redundant data space, wanted to try to use vector, STL is a good thing, after the discovery after some push_back () and iterator operations, time overrun! ! !

Finally ( positive solution ), write dichotomy coming!

Attach AC Code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 100000+5
using namespace std;
//@date: 2020-02-07 15:07:33
//@state:Yes

int num[N];
int n,m;

int f(int x)
{
    int l=0,r=n,mid;
    //l,r,mid,左右边界和中间点
    while(l<=r)
    {
        mid=l+(r-l)/2;
        if(num[mid]==x)return mid;
        if(num[mid]<x)l=mid+1;
        if(num[mid]>x)r=mid-1;
    }
    return 0;
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&num[i]);
    scanf("%d",&m);
    sort(num,num+n);
    for(int i=0;num[i]<=m/2;i++)
    {//遍历比m/2小的数字
        int t=f(m-num[i]);//寻找是否存在这个数
        if(t&&t!=i)
        {
            printf("%d %d",num[i],m-num[i]);
            return 0;
        }
    }
    printf("No");
    return 0;
}
Published 67 original articles · won praise 42 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_26235879/article/details/105357758