Cod sursa(job #2669880)

Utilizator teodorescunicolasteodorescu nicolas alexandru teodorescunicolas Data 8 noiembrie 2020 12:46:09
Problema Statistici de ordine Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <stdio.h>

int v[3000000];
FILE *fin, *fout;
int readInt() {
    int ch, res = 0, semn = 1;
    while ( isspace( ch = fgetc( fin ) ) );
    if ( ch == '-' ) {
        semn = -1;
        ch = fgetc( fin );
    }
    do
        res = 10 * res + ch - '0';
    while ( isdigit( ch = fgetc( fin ) ) );
    return semn * res;
}

void sort( int begin, int end, int a ) {
    int aux, b = begin, e = end, pivot = v[(begin + end) / 2];
    while ( v[b] < pivot )
        b++;
    while ( v[e] > pivot )
        e--;
    while ( b < e ) {
        aux = v[b];
        v[b] = v[e];
        v[e] = aux;
        do
            b++;
        while ( v[b] < pivot );

        do
            e--;
        while ( v[e] > pivot );
    }
    if ( begin < e && a <= e )
        sort( begin, e, a );
    else if ( e + 1 < end )
        sort( e + 1, end, a );
}

int main()
{
    int n, i, a;
    fin = fopen( "sdo.in", "r" );
    fout = fopen( "sdo.out", "w" );
    fscanf( fin, "%d%d", &n, &a );
    for ( i = 0; i < n; i++ ) {
        v[i] = readInt();
    }
    a--;
    sort( 0, n - 1, a );
    fprintf( fout, "%d", v[a] );
    fclose( fin );
    fclose( fout );
    return 0;
}