Cod sursa(job #1849422)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 17 ianuarie 2017 15:35:03
Problema Peste Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
#include <memory>
#include <cstdio>

const int kMaxDim = (1 << 16);
const int kMaxTime = (1 << 10);

struct Net {
	int fish, time;
};

inline bool Comp(const Net& a, const Net& b) {
	return (a.time == b.time ? a.fish < b.fish : a.time < b.time);
}

int netCount, maximumNets, totalTime;
int precalcMaximumFishCount[kMaxTime];
long long dp[kMaxDim];
Net nets[kMaxDim];

std::priority_queue< int, std::vector< int >, std::greater< int > > heap;

int main() {
	std::ifstream inputFile("peste.in");
	std::ofstream outputFile("peste.out");

	inputFile >> netCount >> maximumNets >> totalTime;
	for (int i = 0; i < netCount; ++i)
		inputFile >> nets[i].fish >> nets[i].time;

	std::sort(nets, nets + netCount, Comp);

	for (int i = 1, j = 0, sum = 0; i <= 1000; ++i) {
		for (; j < netCount && nets[j].time <= i; ++j) {
			heap.push(nets[j].fish);
			sum += nets[j].fish;
		}

		while ((int)heap.size() > maximumNets) {
			sum -= heap.top();
			heap.pop();
		}

		precalcMaximumFishCount[i] = sum;
	}

	for (int i = 1; i <= totalTime; ++i)
		for (int j = 1; j <= std::min(i, 1000); ++j)
			dp[i] = std::max(dp[i], dp[i - j] + precalcMaximumFishCount[j]);

	outputFile << dp[totalTime] << '\n';

	return 0;
}

//Trust me, I'm the Doctor!