zoukankan      html  css  js  c++  java
  • 第k小数(桶排序)

    https://ac.nowcoder.com/acm/contest/5773/A

    给一个数列,求第k小的数。

    用桶排序,每次遇到一个数加1,从小到大扫一遍,每遇到数列里的数计数加一,等于k时输出。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=5e6;
    inline int read(){
        int x = 0, f = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9'){
            if (ch == '-')
                f = -1;
            ch = getchar();
        }
        while(ch >= '0' && ch <= '9'){
            x = (x<<1) + (x<<3) + (ch^48);
            ch = getchar();
        }
        return x * f;
    }
    int s[maxn+5];
    int main(){
        int m,n,k,t;
        t=read();
        for(int i=0;i<t;i++){
            n=read();
            k=read();
            fill(s,s+maxn,0);
            for(int j=0;j<n;j++){
                m=read();
                s[m]++;
            }
            int cnt=0;
            for(int o=1;o<=maxn;o++){
                while(s[o]){
                    s[o]--;cnt++;
                    if(cnt==k){cout<<o<<endl;break;}
                }
            }
        }
        return 0;

     

  • 相关阅读:
    STL容器[26]
    SHELL[01]
    SHELL[04]
    SHELL[02]I/O重定向
    STL容器[39]
    stl.set用法总结
    STL容器[33]
    STL容器[29]
    hdu acm1071
    hdu acm 2673
  • 原文地址:https://www.cnblogs.com/mohari/p/12961032.html
Copyright © 2011-2022 走看看