zoukankan      html  css  js  c++  java
  • [ZHOJ1131]Find K Min

    题目大意:
      给你一个数列,求其中第K大的数。

    思路:
      类似于快速排序的思想,每次可以确定出当前的的x在数组中的位置。
      然后根据位置选择该往左找还是往右找。

     1 #pragma GCC optimize(3)
     2 #include<cstdio>
     3 #include<cctype>
     4 #include<bits/move.h>
     5 inline int getint() {
     6     register char ch;
     7     while(!isdigit(ch=getchar()));
     8     register int x=ch^'0';
     9     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    10     return x;
    11 }
    12 const int N=10000001;
    13 int a[N];
    14 int find(const int &l,const int &r,const int &k) {
    15     int i=l,j=r;
    16     const int x=a[(l+r)>>1];
    17     do {
    18         while(a[i]<x) i++;
    19         while(a[j]>x) j--;
    20         if(i<=j) std::swap(a[i++],a[j--]);
    21     } while(i<=j);
    22     if(l<=k&&k<=j) return find(l,j,k);
    23     if(i<=k&&k<=r) return find(i,r,k);
    24     return x;
    25 }
    26 int main() {
    27     int n=getint(),k=getint();
    28     for(register int i=1;i<=n;i++) {
    29         a[i]=getint();
    30     }
    31     printf("%d
    ",find(1,n,k));
    32     return 0;
    33 }
  • 相关阅读:
    sprintf的用法
    sscanf
    Decode the tape
    poj 1579 Function Run Fun
    Where's Waldorf?
    uva Andy's First Dictionary
    UVA Hangman Judge
    UVa Automatic Editing
    界面设计规范
    web标准下的web开发流程思考
  • 原文地址:https://www.cnblogs.com/skylee03/p/7655791.html
Copyright © 2011-2022 走看看