Cod sursa(job #2672912)

Utilizator cristi_macoveiMacovei Cristian cristi_macovei Data 15 noiembrie 2020 14:16:10
Problema Carnati Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <iostream>
#include <fstream>
#include <algorithm>

const int NMAX = 2e3;

class Client {
public:
  int time;
  int price;

  Client() = default;

  bool operator < (const Client& other) const {
    return this->time < other.time;
  }

  friend std::istream& operator >> (std::istream& is, Client& inst) {
    is >> inst.time >> inst.price;
    return is;
  }
};

int n, cost;

int dp[1 + NMAX][1 + NMAX];

Client clients[1 + NMAX];

void read() {
  std::ifstream in("carnati.in");

  in >> n >> cost;

  for (int i = 1; i <= n; ++i)
    in >> clients[i];

  std::sort(clients + 1, clients + n + 1);

  in.close(); // Gata citirea
}

int main() {
  read();

  int last;

  for (int i = 1; i <= n; ++i) {
    last = 0;
    for (int j = 1; j <= n; ++j) {
      if (clients[i].price <= clients[j].price) {
        dp[i][j] = std::max(dp[i][last] + clients[i].price - cost * (clients[j].time - clients[last].time), clients[i].price - cost);
        last = j;
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j)
      std::cout << dp[i][j] << ' ';
    std::cout << '\n';
  }

  int sol = dp[1][1];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j)
      sol = std::max(sol, dp[i][j]);
  }

  std::ofstream out("carnati.out");
  out << sol << '\n';

  out.close();
  return 0;
}