Kth decimal-Niu Ke

Question:
Give you a sequence of length n, find the kth decimal in the sequence.
Input description:
Link: the kth decimal-Niuke
Source: Niuke

Multiple groups of input, the first line reads an integer T to indicate that there are T groups of data. Each group of data occupies two rows, the first row is two integers n, k, indicating the length of the number sequence and k. The second line is n integers separated by spaces.
Output description:
For each group of data, output its kth decimal. Each group of data is separated by a space.
Example 1
: Input:
2
5 2
1 4 2 3 4
3 3
3 2 1
Output:
2
3

Idea: This question may have a large amount of input data, and ordinary input may time out. This question requires fast reading and the nth_element() function.
Fast queue:

#define re register
inline int read()
{
    
    
    re int x=0,f=1;
    re char ch=getchar();
    while(ch<'0'||ch>'9') {
    
    if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return x*f;
}


Usage of nth_element function :
nth_element(start,start+k,end)
makes the k-th largest number arranged on the array with subscript k.

code show as below:

#include <iostream>
#include <algorithm>
using namespace std;
#define re register
inline int read()
{
    
    
    re int x=0,f=1;
    re char ch=getchar();
    while(ch<'0'||ch>'9') {
    
    if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return x*f;
}
int main()
{
    
    
 const int maxn=5e6+5;
 int i,t,n,k,a[maxn];
 cin>>t;
 while(t--)
 {
    
    
  n=read();
        k=read();
  for(i=1;i<=n;i++)
  {
    
    
   a[i]=read();
  }
  nth_element(a,a+k,a+n+1);
  cout<<a[k+1]<<endl;
 }
 return 0;
}

Guess you like

Origin blog.csdn.net/HT24k/article/details/108368693