New count game

topic

Suantoujun is playing a game with his friends. Due to his wit, this game was judged by Mr. Huantou.

First, Mr. Garantou will give each of them a number, and each person's number is different. In each subsequent round, Mr. Suantou will give a number, and the person whose number does not exceed its maximum number should report his number. If no one's number is lower than the number given by Mr. Suantou, then the person with the smallest number will report his number. Everyone can repeat the registration number.

Mr. Garantou will report the number of each round in sequence according to a list, and his friends want to know what the number should be reported for each round. Can you help them?

input format

There are 3 lines of input data. 
In the first line, there are two integers n, m (1 n 10 5 , 1 m 10 5 ≤n≤105, 1≤m≤105), which respectively represent the number of friends who participated in the game, and the number of friends in the game. number of rounds.

The second line has n integers  a i ( 1 a i 10 8 ) ai (1≤ai≤108), which represent the numbers of each of the friends.

The third line is mm integers  qi ( 1 ≤ qi ≤ 10 8 ) qi ( 1≤qi≤108 ) , which represent the numbers given by Mr. Garlic in each round.

output format

A total of one line is output, and mm integers are output, indicating the number reported in each round. There is a space between every two integers and no space after the last integer.

sample input

5 5
1 5 10 15 20
3 6 12 18 24

output

1 5 10 15 20

  Please use scanf and printf for input and output. Undoubtedly, you need a sorting algorithm - here you are free to use any method to sort the data: you can choose to implement a common sorting algorithm yourself (such as bubble sort or merge sort), and you also You can choose to use qsort or std::sort, here we do not limit. You also need a more efficient search algorithm - you can solve this problem with the halved search algorithm (also called the binary search algorithm) that you learned earlier.

#include<iostream>
#include<algorithm>
#define MAX 100000+10
using std::sort;
int finder(int array[],int low,int high,int target)//二分查找
{
    while(low<=high)
        {
            int mid=(low+high)/2;
            if(array[mid]>target)
                high=mid-1;
            else 
            low=mid+1;
        }
            return high;
}
intmain ()
{
    int n,m;
     int a[MAX]; // friend number 
    int q[MAX]; // the number 
    scanf( " %d %d " ,&n,& m);
     for ( int i= 0 ;i <n;i++) // The number of each friend 
    {
        scanf("%d",&a[i]);
    }
    sort(a,a + n);
     for ( int j= 0 ;j<m;j++) // The number given by Mr. Garlic every round 
    {
        scanf("%d",&q[j]);
    }
    for ( int k= 0 ;k<m;k++) // output module 
    {
         int c= q[k];
         if (c>=a[n- 1 ]) // when the given number is greater than the maximum number 
        {
            printf("%d",a[n-1]); 
        }
        else  if (c<=a[ 0 ]) // When the given number is less than the minimum number 
        {
            printf("%d",a[0]);
        }
        else  // When the number does not exceed its maximum number 
        {
            printf("%d",a[finder(a,0,n,c)]);
        }
        if(k!=m-1) printf(" ");
    }
    return 0;
}

 

 

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324780638&siteId=291194637