zoj 3230Solving the Problems//優先隊列

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std;
#define N 100005
class node
{
public:
	int a,b;
};
bool operator< (const node &a,const node &b)
{
	if(a.b<b.b ) return true;
	return false;
}
node st[N];
int n,m,p;
int cmp(const void *a,const void *b)
{
	node *c=(node *)a;
	node *d=(node *)b;
	if(c->a >d->a )return 1;
	return 0;
}
void solve()
{
	qsort(st,n,sizeof(st[0]),cmp);
	priority_queue<node> q;
	while(!q.empty ()) q.pop ();
	int cnt=0,j=0;
	for(int i=0;i<m;i++)
	{
		while(j<n&&st[j].a <=p)
		{
			q.push (st[j]);
			j++;
		}
		if(!q.empty ())
		{
			p+=q.top ().b ;q.pop ();
		}
	}
	printf("%d\n",p);
}
int main()
{
	while(scanf("%d%d%d",&n,&m,&p)!=EOF)
	{
		for(int i=0;i<n;i++)
			scanf("%d%d",&st[i].a,&st[i].b );
		solve();
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章