Cod sursa(job #2673543)

Utilizator cristi_macoveiMacovei Cristian cristi_macovei Data 17 noiembrie 2020 10:22:48
Problema Carnati Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <memory.h>

const int NMAX = 2e3;
const int TMAX = 15e2;

class Client {
public:
  int time;
  int price;

  Client() = default;

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

  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, ans = 0;

Client clients[1 + NMAX];

int profit[1 + TMAX];


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

  in >> n >> cost;

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

  in.close();
}

int max_subarray() {
  int best = 0, sum = 0;

  for (int i = 1; i <= TMAX; ++i) {
    if (sum < 0)
      sum = profit[i];
    else
      sum += profit[i];

    if (sum > best)
      best = sum;
  }

  return best;
}

int main() {
  read();

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

  for (int i = 1; i <= n; ++i) {
    memset(profit, 0, sizeof(profit));

    int fixed_price = clients[i].price;

    for (int j = 1; j <= n; ++j) {
      if (clients[j].price >= fixed_price)
        profit[clients[j].time] += fixed_price;
    }

    for (int k = 1; k <= TMAX; ++k)
      profit[k] -= cost;

    ans = std::max(ans, max_subarray());
  }

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

  out.close();
  return 0;
}