Cod sursa(job #1013547)

Utilizator varga13VarGaz13 varga13 Data 21 octombrie 2013 09:43:52
Problema Statistici de ordine Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#define MAX_INT 1919999
#define MIN_INT -1

using namespace std;

ifstream f("sdo.in");
ofstream g("sdo.out");
bool global;
int a[3000000];int i,j,m,n,ms=MAX_INT,md=MAX_INT;

inline void afiseaza()
{
    for(int i=0;i<n;++i)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
}

inline void swap(int i, int j)
{
    int t = a[i];
    a[i] = a[j];
    a[j] = t;
   // cout<<"swapped"<<" "<<a[i]<<" "<<a[j]<<endl;
}

void sort(int st, int dr)
{
    if(global)
    {
    i = st;
    j = dr;
    int pivot = a[st + (rand() % (dr-st))];

    while (i <= j) {
        while (a[i] < pivot)
            i++;
        while (a[j] > pivot)
            j--;
        if (i <= j) {
            swap(i,j);
            i++;
            j--;
        }
    }
    ms=(dr-st<ms)?dr-st:ms;
    if(ms==1&&m<=dr&&m>=dr)
    {
        g<<a[st];
        global=false;
    }
    else
    {
       if (st < j)
        sort(st, j);
       if(i < dr)
        sort(i, dr);
    }
    }
}

int main()
{
    f>>n>>m;
    global=true;
    for(int i=0;i<n;i++)
    {
        f>>a[i];
    }

    sort(0,n-1);

    for(int i=0;i<n;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}