首页 > 其他分享 >负载均衡

负载均衡

时间:2023-02-26 11:55:36浏览次数:25  
标签:PII 负载 结束 计算机 int typedef 任务 均衡

题解:

首先根据题意可知,每台计算机时互相独立的,因此下面的讲解只针对一台计算机

每个任务的开始时间保证是递增的,对于任务a,再i时刻开始,a之前开始的任务如果结束时间小于等于i,则表示这些任务已经结束了,

在进行a之前,它们已经将算力还给计算机了,如果之前开始的任务的结束时间还大于i,就证明之前的任务没有结束,还不能将算力归还给计算机,

可以用堆,也就是c++中的优先队列,小根堆,类型是PII,first存每个任务的结束时间,second存每个任务需要都的算力

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define x first
#define y second
typedef pair<int,int> PII;
const int N=2e5+10;

int n,m;
int s[N];
priority_queue<PII,vector<PII>,greater<PII> > heap[N];

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%d",&s[i]);
    
    while(m--)
    {
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        while(heap[b].size()&&heap[b].top().x<=a)
        {
            s[b]+=heap[b].top().y;
            heap[b].pop();
        }
        if(s[b]<d) puts("-1");
        else{
            heap[b].push({a+c,d});
            s[b]-=d;
            printf("%d\n",s[b]);
        }
    }
    return 0;
}

 

标签:PII,负载,结束,计算机,int,typedef,任务,均衡
From: https://www.cnblogs.com/tolter/p/17156398.html

相关文章