Cod sursa(job #2698413)

Utilizator Ana_22Ana Petcu Ana_22 Data 21 ianuarie 2021 23:41:39
Problema Carnati Scor 100
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <stdio.h>
#include <stdlib.h>
#define NMAX 2000

int t[NMAX], p[NMAX];

void myqsort( int begin, int end ) {
  int aux, b = begin, e = end,
    pivot = t[(begin+end)/2];
  while( t[b] < pivot)
    b++;
  while( t[e] > pivot)
    e--;
  while( b < e ) {
    aux = t[b];
    t[b] = t[e];
    t[e] = aux;
    aux = p[b];
    p[b] = p[e];
    p[e] = aux;
    do
      b++;
    while( t[b] < pivot );
    do
      e--;
    while( t[e] > pivot );
  }
  if( begin < e )
    myqsort( begin, e );
  if( e + 1 < end )
    myqsort( e + 1, end );
}

int main() {
    FILE *fin, *fout;
    int n, c, i, rez, prof, j;
    fin = fopen( "carnati.in", "r" );
    fscanf( fin, "%d%d", &n, &c );
    for( i = 0; i < n; i++ )
      fscanf( fin, "%d%d", &t[i], &p[i] );
    fclose( fin );
    myqsort( 0, n - 1 );//sortare dupa timp
    rez = -1000000;
    for( i = 0; i < n; i++ ) {//pt fiec pret p[i] fac ssm
      prof = ( p[0] >= p[i] ? p[i] : 0 ) - c;//pt profitul rezultat
      rez = prof > rez ? prof : rez;//din fiecare posibila vanzare
      for( j = 1; j < n; j++ ) {//la pretul p[i]
        prof -= ( t[j] - t[j-1] - 1 ) * c;
        if( prof < 0 )
          prof = 0;
        prof += ( p[j] >= p[i] ? p[i] : 0 ) - c;
        rez = prof > rez ? prof : rez;
      }
    }
    fout = fopen( "carnati.out", "w" );
    fprintf( fout, "%d\n", rez );
    fclose( fout );
    return 0;
}