Cod sursa(job #2697403)

Utilizator LucaMihaiLM10Luca Ilie LucaMihaiLM10 Data 18 ianuarie 2021 15:28:17
Problema Carnati Scor 10
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <stdio.h>
#define MAX_N 2000

struct produs {
    int t, p;
};
struct produs v[MAX_N];

int cmp( struct produs a, struct produs b ) {
    if ( a.t < b.t )
        return -1;
    else if ( a.t > b.t )
        return 1;
    return 0;
}
void sort( int begin, int end ) {
    int b, e;
    struct produs p, aux;
    b = begin;
    e = end;
    p = v[(begin + end) / 2];
    while ( cmp( v[b], p ) )
        b++;
    while ( cmp( p, v[e] ) )
        e--;
    while( b < e ) {
        aux = v[b];
        v[b] = v[e];
        v[e] = aux;
        do
            b++;
        while ( cmp( v[b], p ) );
        do
            e--;
        while ( cmp( p, v[e] ) );
    }
    if ( begin < e )
        sort( begin, e );
    if ( e + 1 < end )
        sort( e + 1, end );
}

int main() {
    FILE *fin, *fout;
    int n, c, maxProfit, profit, pret, i, j;
    fin = fopen( "carnati.in", "r" );
    fscanf( fin, "%d%d", &n, &c );
    for ( i = 0; i < n; i++ )
        fscanf( fin, "%d%d", &v[i].t, &v[i].p );
    fclose( fin );

    sort( 0, n - 1 );

    maxProfit = 0;
    for ( i = 0; i < n; i++ ) {
        pret = v[i].p;
        profit = -c;
        if ( v[0].p >= pret )
            profit += pret;
        for ( j = 1; j < n; j++ ) {
            profit -= c * (v[j].t - v[j - 1].t - 1);
            if ( profit < 0 )
                profit = 0;
            profit -= c;
            if ( v[j].p >= pret )
                profit += pret;
            if ( profit > maxProfit )
                maxProfit = profit;
        }
    }

    fout = fopen( "carnati.out", "w" );
    fprintf( fout, "%d", maxProfit );
    fclose( fout );
    return 0;
}