Cod sursa(job #2221101)

Utilizator MoldooooooooMoldoveanu Stefan Moldoooooooo Data 13 iulie 2018 10:15:24
Problema Statistici de ordine Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <cstdio>
using namespace std;
int N, K, i, st, dr, stmax, drmax;
int long List[3000005], aux;
char p;
int main()
{
    freopen("sdo.in", "r", stdin);
    freopen("sdo.out", "w", stdout);
    scanf("%d%d", &N, &K);
    for(i=1; i<=N; i++) scanf("%ld", &List[i]);
    p='d';
    st=stmax=1; dr=drmax=N;
    while(st!=K){
        if(st>K){
            st=stmax;
            dr--;
            drmax=dr;
        }
        if(dr<K){
            dr=drmax;
            st++;
            stmax=st;
        }
        while(st!=dr){
            if(List[st]>List[dr]){
                aux=List[st];
                List[st]=List[dr];
                List[dr]=aux;
                if(p=='d')p='s';
                else p='d';
            }
            if(p=='d')st++;
            else dr--;
        }
    }
    printf("%ld", List[K]);
    return 0;
}