Cod sursa(job #1293170)

Utilizator cella.florescuCella Florescu cella.florescu Data 15 decembrie 2014 15:23:45
Problema Orase Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <stdio.h>
#include <stdlib.h>
int ind[50000];

struct strada{
  int d, l;
}v[50000], aux;

int dist(i, j){
  return v[i].l+v[j].l+v[j].d-v[i].d;
}

void myqsort(int begin, int end){
  int b=begin, e=end, pivot=v[begin+rand()%(end-begin+1)].d;
  while(b<=e){
    while(v[b].d<pivot)
      ++b;
    while(v[e].d>pivot)
      --e;
    if(b<=e){
      aux=v[b]; v[b]=v[e]; v[e]=aux;
      ++b; --e;
    }
  }
  if(begin<e)
    myqsort(begin, e);
  if(b<end)
    myqsort(b, end);
}

int main()
{
    FILE *fin, *fout;
    int m, n, i, max;
    fin=fopen("orase.in", "r");
    fscanf(fin, "%d%d", &m, &n);
    for(i=0; i<n; i++)
      fscanf(fin, "%d%d", &v[i].d, &v[i].l);
    fclose(fin);
    myqsort(0, n-1);
    max=-1;
    for(i=1; i<n; i++){
      ind[i]=dist(ind[i-1], i)<dist(i-1, i)?i-1:ind[i-1];
      if(max<dist(ind[i], i))
        max=dist(ind[i], i);
    }
    fout=fopen("orase.out", "w");
    fprintf(fout, "%d\n", max);
    fclose(fout);
    return 0;
}