Cod sursa(job #1849401)

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

const int kMaxDim = 50005;
const int kMaxTime = 1005;

class Reader {
	public:
		Reader(const std::string& filename) :
			m_stream(filename),
			m_pos(kBufferSize - 1),
			m_buffer(new char[kBufferSize]) {
			Next();
		}

		template<typename T>
		Reader& operator>>(T& value) {
			value = 0;
			while (Current() < '0' || Current() > '9')
				Next();
			while (Current() >= '0' && Current() <= '9') {
				value = value * 10 + Current() - '0';
				Next();
			}
			return *this;
		}

	private:
		const int kBufferSize = (1 << 18);

		char Current() {
			return m_buffer[m_pos];
		}

		void Next() {
			if (!(++m_pos != kBufferSize)) {
				m_stream.read(m_buffer.get(), kBufferSize);
				m_pos = 0;
			}
		}

		std::ifstream m_stream;
		int m_pos;
		std::unique_ptr<char[]> m_buffer;
};

struct Net {
	Net(int _fish = 0, int _time = 0) :
		fish(_fish),
		time(_time) {
	}

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

	int fish, time;
};

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

std::priority_queue< int > heap;

int main() {
	Reader 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);

	for (int i = 0, sum = 0; i < netCount; ++i) {
		heap.push(-nets[i].fish);
		sum += nets[i].fish;

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

		precalcMaximumFishCount[nets[i].time] = sum;
	}

	int maxTime = nets[netCount - 1].time;
	for (int i = 1; i <= maxTime; ++i)
		precalcMaximumFishCount[i] = std::max(precalcMaximumFishCount[i], precalcMaximumFishCount[i - 1]);

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

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

	outputFile.close();

	return 0;
}

//Trust me, I'm the Doctor!