Cod sursa(job #1013546)

Utilizator varga13VarGaz13 varga13 Data 21 octombrie 2013 09:39:09
Problema Statistici de ordine Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 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;
}

inline int part(int st, int dr)
{
    int i=st+1;
    int j=dr+1;

    int pivot = a[(st+dr)/2];

    while(true)
    {
        do
        {
            ++i;
        }while(i<pivot);
        do
        {
            --j;
        }while(j>pivot);
    }
}

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--;
        }
    }
    afiseaza();
    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]<<" ";
    }

 //   g<<a[m-1]<<'\n';
   // g<<ms;
    return 0;
}