Cod sursa(job #1549488)

Utilizator lflorin29Florin Laiu lflorin29 Data 12 decembrie 2015 13:30:29
Problema Statistici de ordine Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3 * 1e6 + 7;

int N, K;
int A[MAXN];

int sdo(int st, int fn) {
    int pivot = A[st + rand() % (fn - st + 1)];
    int actst = st, actfn = fn;
    while(true) {
          while(A[actst] < pivot)
               ++actst;
          while(A[actfn] > pivot)
               --actfn;
          if(actst < actfn)
             swap(A[actst], A[actfn]);
          else
              return actfn;
    }
    return 0;
}

void solve(int st, int fn, int step) {
    if(st == fn)
        return;
    int num = sdo(st, fn), cnt = num - st + 1;
    if(cnt >= step)
        solve(st, num, step);
    else
        solve(num + 1, fn, step - cnt);
}

void Read() {
    srand(time(0));
    ifstream fin("sdo.in");
    fin >> N >> K;
    for(int i = 1; i <= N; ++i)
        fin >> A[i];
}

int main() {
    Read();
    solve(1, N, K);
    ofstream("sdo.out") << A[K];
    return 0;
}