Cod sursa(job #3326409)

Utilizator horatiu.avramAvram Popa Cristian Horatiu horatiu.avram Data 28 noiembrie 2025 18:52:09
Problema Statistici de ordine Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include<fstream>

using namespace std;

const int MAXN=3e6;

ifstream fin("sdo.in");
ofstream fout("sdo.out");

int v[MAXN];

int quickselect(int n,int pos) {
    int aux,s,e,ip,pivot,start,end;
    start=0;
    end=n-1;
    while(start<end) {
        ip=(end+start)/2;
        pivot=v[ip];
        s=start;
        while(v[s]<pivot) {
            s++;
        }
        e=end;
        while(v[e]>pivot) {
            e--;
        }
        while(s<e) {
            aux=v[s];
            v[s]=v[e];
            v[e]=aux;
            s++;
            while(v[s]<pivot) {
                s++;
            }
            e--;
            while(v[e]>pivot) {
                e--;
            }
        }
        if(pos<=e) {
            end=e;
        } else {
            start=e+1;
        }
    }
    return v[start];
}

int main() {
    int n,k;
    fin>>n>>k;
    for(int i=0; i<n; i++) {
        fin>>v[i];
    }
    fout<<quickselect(n,k-1);
    return 0;
}