Cod sursa(job #1755774)

Utilizator preda.andreiPreda Andrei preda.andrei Data 11 septembrie 2016 00:42:23
Problema Carnati Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

#define NMAX 2001

struct Client
{
    int timp;
    int pret;
};

vector<Client> clienti;

int main()
{
    ifstream fin("carnati.in");
    ofstream fout("carnati.out");

    int n, salariu;
    fin >> n >> salariu;

    clienti.push_back({0, 0});

    for (int i = 1; i <= n; ++i) {
        int t, p;
        fin >> t >> p;
        clienti.push_back({t, p});
    }

    auto comp = [] (Client c1, Client c2) -> bool { return c1.timp < c2.timp; };
    sort(clienti.begin(), clienti.end(), comp);

    clienti[0].timp = clienti[1].timp - 1;

    int profitMax = 0;
    for (int i = 1; i <= n; ++i) {
        int profit = 0;
        for (int j = 1; j <= n; ++j) {
            if (profit == 0)
                profit -= salariu;
            else profit -= salariu * (clienti[j].timp - clienti[j - 1].timp);
            profit += (clienti[j].pret >= clienti[i].pret) ? clienti[i].pret : 0;

            if (profit <= 0) {
                if (clienti[j].pret >= clienti[i].pret)
                    profit = clienti[i].pret - salariu;
                else profit = 0;
            }

            // cout << clienti[j].timp << " " << profit << "\n";
            profitMax = max(profitMax, profit);
        }
    }

    fout << profitMax;
    return 0;
}