Cod sursa(job #1512219)

Utilizator MarghescuGabriel Marghescu Marghescu Data 27 octombrie 2015 19:53:01
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
using namespace std;
ifstream f("sdo.in");
ofstream g("sdo.out");
int v[3000000];
int main()
{
      int n,i,left,right,aux,b,e,k,pivot;
      f>>n>>k;
      for(i=0; i<n; i++)
        f>>v[i];
      k--;
      left=0;
      right=n-1;
      while(left<right)
      {
        pivot=v[(left+right)/2];
        b=left;
        e=right;
        while(b<=e)
        {
          while(v[b]<pivot)
              b++;
          while(v[e]>pivot)
              e--;

          if(b<=e)
          {
              aux=v[b];
              v[b]=v[e];
              v[e]=aux;
              b++;
              e--;
          }
        }
        if(k<=e)
           right=e;
        else
          if(k>=b)
              left=b;
          else
              left=right=k;

      }
      g<<v[k];
      return 0;
}