Pagini recente » Rating Rares Braescu (raresbraescu) | Istoria paginii runda/vendetta_dc4/clasament | Cod sursa (job #658664) | Cod sursa (job #263744) | Cod sursa (job #2076619)
#include <fstream>
#define nmax 300002
#include <stdlib.h>
#include <time.h>
using namespace std;
ifstream f("sdo.in");
ofstream g("sdo.out");
int v[nmax],n,k;
void interschimba(int i,int j)
{
int aux=v[i];
v[i]=v[j];
v[j]=aux;
}
int QuickSort_StOrd(int st,int dr,int poz)
{
g<<st<<' '<<dr<<' '<<poz<<'\n';
if(st==dr) return v[st];
int x1=st+rand()%(dr-st+1);
int x2=st+rand()%(dr-st+1);
int x3=st+rand()%(dr-st+1);
int pivot,punct_partitie;
if((v[x1]<=v[x2] && v[x2]<=v[x3])||(v[x3]<=v[x2] && v[x2]<=v[x1]))
pivot=v[x2];
else if((v[x1]<=v[x3] && v[x3]<=v[x2]) || (v[x2]<=v[x3] && v[x3]<=v[x1]))
pivot=v[x3];
else pivot=v[x1];
int i=st-1,j=dr+1;
while(i<j)
{
while(v[++i]<pivot);
while(v[--j]>pivot);
if(i>=j)
punct_partitie=j;
else
interschimba(i,j);
}
if(poz+st-1<=punct_partitie)
return QuickSort_StOrd(st,punct_partitie,poz);
else
return QuickSort_StOrd(punct_partitie+1,dr, poz-punct_partitie+st-1);
}
int main()
{
srand(time(NULL));
f>>n>>k;
for(int i=1;i<=n;++i)
f>>v[i];
g<<QuickSort_StOrd(1,n,k);
return 0;
}