Cod sursa(job #2049107)

Utilizator shantih1Alex S Hill shantih1 Data 26 octombrie 2017 21:01:23
Problema Carnati Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <iostream>
#include <fstream>
#include <deque>
#include <algorithm>

using namespace std;
ifstream fin("carnati.in");
ofstream fout("carnati.out");

int cate, i, j, cpeh, mx, pr, co, tr, ti, mx2, rez;

struct  om
{   int t, p;   } v[2005];
deque <int> vq, nq, tq, iq;

int max ()
{
	int mx = 0;
	int a = 0, b = 0;
	int tr2 = tr, n = 1, mx2 = -1;
	for (j = (int)vq.size()-1; j >= 0; j--)
	{
		if (vq[j] >= pr)	
		{
			nq[nq.size()-1]++;
			a = nq[nq.size()-1]*pr - (tr-iq[j]+1)*cpeh;
			if (a < 0)	a = 0;
		}
		else 
		{
			nq[j]++;
			a = nq[j]*vq[j] - (tr-iq[j]+1)*cpeh;
		}
		b = nq[j]*vq[j] - (tr-iq[j]+1)*cpeh;
		if (b < 0)	b = 0;
		
		if (a > mx2 && a >= 0) 
		{
			mx2 = a;
			tr2 = tq[j];
			n = nq[nq.size()-1];
		}
		a = max(a, max(b, 0));
		if (a > mx)	mx = a;
	}
	
	iq[iq.size()-1] = tr2;
	nq[nq.size()-1] = n;
	return mx;
}

bool comp (om x, om y)
{
	return (x.t < y.t);
}

int main () {
	
	fin >> cate >> cpeh;
	for (i = 1; i <= cate; i++)
		fin >> v[i].t >> v[i].p;
	
	sort (v+1, v+cate+1, comp);
	
	rez = v[1].p-cpeh;
	vq.push_back(v[1].p);
	tq.push_back(v[1].t);
	nq.push_back(1);
	iq.push_back(v[1].t);
	for (i = 2; i <= cate; i++)
	{
		pr = v[i].p; 
		tr = v[i].t;
		
		vq.push_back(pr);
		tq.push_back(tr);
		nq.push_back(0);
		iq.push_back(tr);
		
		mx = max();
		mx2 = pr-cpeh;
		if (mx2 > mx)
		{
			mx = mx2;
			while (vq.size() > 1)
			{
				vq.pop_front();
				nq.pop_front();
				tq.pop_front();
				iq.pop_front();
			}
			nq[0] = 1; 
			iq[0] = tq[0];
		}
		
		if (mx > rez)    rez = mx;
	}
	
	if (rez > 0) fout << rez << "\n";
	else fout << 0 << "\n";
}