(Second Provincial Game of the 12th Blue Bridge) I Load Balancing (Priority Queue + Thinking)

Analysis: This question examines the priority queue. We can store pair-type data in the priority queue. The first dimension in the pair is the termination time of the task, and the second dimension is the computing power occupied by the task. Since the pair type is sorted according to the first dimension, and sorted according to the termination time from early to late, we do not need to sort by handwritten structure, we can directly use greater.

Let me talk about how to deal with this problem. We first create a p array, p[i] records the current computing power of the i-th computer . If we currently traverse to a task, this task is to be assigned to the t- th computer. For computers, we first traverse the priority queue of the t-th computer, release all tasks in the priority queue whose termination time is before the current time, and add the computing power occupied by the task in the process of releasing the task . When the priority queue terminates After all tasks with time less than or equal to the current time are released, at this time p[t] really represents the computing power of the t-th computer. At this time, the relationship between p[t] and d is judged. If p[t]<d, then Directly output -1, if p[t]>=d, output p[t]-d, add p[t]-d, and add the task to the priority queue of the t-th computer, the termination of the task Time is a+c-1, and computing power is d .

Here is the code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>PII;
const int N=200030;
priority_queue<PII,vector<PII>,greater<PII> >q[N];
ll p[N];//p[i]表示第i个计算机的算力
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		scanf("%lld",&p[i]); 
	for(int i=1;i<=m;i++)
	{
		ll a,b,c,d;
		scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
		while(!q[b].empty()&&q[b].top().first<a)
		{
			p[b]+=q[b].top().second;//把无效任务的占用的算力释放 
			q[b].pop();//释放无效任务 
		}
		if(p[b]>=d)
		{
			p[b]-=d;
			printf("%lld\n",p[b]);
			q[b].push({a+c-1,d});//把当前任务加入第b个计算机的优先队列 
		}
		else
			puts("-1");
	}	
	return 0;
}

Guess you like

Origin blog.csdn.net/AC__dream/article/details/124049001