Cod sursa(job #1849430)

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

using namespace std;

ifstream fin("peste.in");
ofstream fout("peste.out");

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

char buf[BMAX];

int buffer;

void read(int &ans)
{
	ans = 0;
	while (buf[buffer]<'0' || buf[buffer]>'9')
	{
		buffer++;
		if (buffer == BMAX - 1)
		{
			fin.get(buf, BMAX, EOF);
			buffer = 0;
		}
	}
	while (buf[buffer] >= '0'&&buf[buffer] <= '9')
	{
		ans = ans * 10 + buf[buffer++] - '0';
		if (buffer == BMAX - 1)
		{
			fin.get(buf, BMAX, EOF);
			buffer = 0;
		}
	}
}

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 precalcMaximumFishCount[kMaxTime];
long long dp[kMaxDim];
Net nets[kMaxDim];

int main() {
	int netCount, maximumNets, totalTime;
	priority_queue< int, vector< int >, greater< int > > heap;
	fin.get(buf, BMAX, EOF);

	read(netCount); read(maximumNets); read(totalTime);
	for (int i = 0; i < netCount; ++i) {
		read(nets[i].fish);
		read(nets[i].time);
	}

	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 <= min(i, 1000); ++j)
			dp[i] = max(dp[i], dp[i - j] + precalcMaximumFishCount[j]);

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

	return 0;
}

//Trust me, I'm the Doctor!