Cod sursa(job #2467131)

Utilizator LeVladzCiuperceanu Vlad LeVladz Data 3 octombrie 2019 19:10:26
Problema Statistici de ordine Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <cstdio>
#include <stdlib.h>

using namespace std;

FILE *fin = fopen("sdo.in", "r");
FILE *fout = fopen("sdo.out", "w");

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

int n,k,i,v[3000005];

void sorteaza(int st, int dr)
{
    if (st < dr)
    {
        int i = 0; int j = -1;
        int stst = st; int drdr = dr;
        int x = st+rand()%(dr-st+1);
        //swap(v[st], v[x]);
        int aux = v[st]; v[st] = v[x]; v[x] = aux;
        while (stst != drdr)
        {
            if (v[stst] > v[drdr])
            {
                //swap(v[stst], v[drdr]);
                int aux = v[stst]; v[stst] = v[drdr]; v[drdr] = aux;
                aux = i;
                i = -j;
                j = -aux;
            }
            stst += i;
            drdr += j;
        }
        int p = stst;
        sorteaza(st, p-1);
        sorteaza(p+1, dr);
    }
}

int main()
{
    //fin >> n >> k;
    fscanf(fin, "%d", &n); fscanf(fin, "%d", &k);
    for (i=1; i<=n; i++)
    {
        //fin >> v[i];
        fscanf(fin, "%d", &v[i]);
    }
    sorteaza(1, n);
    //fout << v[k];
    fprintf(fout, "%d", v[k]);
    return 0;
}