Cod sursa(job #438384)

Utilizator VladTVlad Tudose VladT Data 10 aprilie 2010 18:26:45
Problema Gutui Scor 100
Compilator cpp Status done
Runda teme_upb Marime 1.13 kb
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

typedef struct{
	long weight;
	long height;
	long lifetime;
}quince;

bool compare (quince i,quince j) 
{ 
	return (i.height>j.height); 
}

long Quinces()
{
	long N,H,U,total_weight=0,sum=0,last;
	int i;
	FILE *in=fopen("gutui.in","r");
	fscanf(in,"%ld %ld %ld\n",&N,&H,&U);
	vector<quince> q(N);
	priority_queue<long> pq;

	for(i=0;i<N;i++)
	{
		
		fscanf(in,"%ld %ld\n",&q[i].height,&q[i].weight);
		q[i].lifetime=(H-q[i].height)/U+1;
		total_weight+=q[i].weight;
	}
	fclose(in);
	if(!U)
	      return total_weight;
	sort(q.begin(),q.end(),compare);
	last=q.back().lifetime;
	while((q.size() or pq.size())and last)
	{
		if(pq.size())
		{
			sum+=pq.top();
			pq.pop();
			last--;
		}
		else
		{
		    last=q.back().lifetime;
        }
        while(q.size() and q.back().lifetime>=last)
		{
			pq.push(q.back().weight);
			q.pop_back();
		}                       
	}
	return sum;
}

int main ()
{
	FILE *out=fopen("gutui.out","w");
	fprintf(out,"%ld",Quinces());
	fclose(out);  
	return 0;
}