P2107 小Z的AK计划(反悔贪心,优先队列)

又是一道反悔贪心,怎么还是不会写呢!!哎

x , 假设最后停在了x号机房,路费固定

那么一定是从小到大选了最小的代价的机房

但是这样不好实现,需要反悔的贪心了

x , A K , T 当我们到达x节点时,假设之前所有机房都去AK了,花费时间是T

T m , [ 1 , x ] , A K 如果T小于等于m,那么在[1,x]号机房中,这样AK一定是最优的

T m , [ 1 , x ] 如果T大于m,那么在[1,x]号机房中选择一些最大代价的机房弹出

A K 1 , 那么AK数减去1,但返还花费的时间

使 x , A K 使得在能走到x机房的基础上,AK的题还最多

我们用优先队列来实现

#include <bits/stdc++.h>
using namespace std;
const int maxn=3e5+10;
typedef long long ll;
ll n,m;
struct p{
	ll x,t;
	bool operator < (const p&tmp )	const{
		return x<tmp.x;
	}
}a[maxn];
priority_queue<ll>q;
int main()
{
	cin >> n >> m;
	for(int i=1;i<=n;i++)	cin >> a[i].x >> a[i].t;
	ll ans=0,last=0,temp=0;
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++)
	{
		m-=( a[i].x-last+a[i].t );//一路全都AC 
		temp++;
		q.push( a[i].t ); 
		while( m<0&&!q.empty() )//只要不能AK就一直弹出来 
		{
			ll now=q.top(); q.pop();
			temp--,m+=now;//AC数减少,但时间增加	
		}	
		if( m<0)	break;//走都走不到
		last=a[i].x;
		ans=max(ans,temp); 
	} 
	cout<<ans;
}

猜你喜欢

转载自blog.csdn.net/jziwjxjd/article/details/107482690