一个模拟问题

Problem

Jesse is building his own operating system and now faces the task of building the process scheduling and the memory management feature. He has laid down the rules of how he is going to do it. It’s as follows:

  1. If a process needs to be executed and memory is available, the process is given the required amount of memory.
  2. If a process needs to be executed and memory is not available, Jesse will wait until a few processes are completed which will free up enough memory and then he will assign it to the process.
  3. Once a process is assigned some memory, it can’t be removed from the memory until it’s completed.
  4. The processes should be executed in the given order. A process j can’t be allocated memory before process i, if i<j .

Jesse is busy with other stuff and needs your help in implementing this. Can you help him do this?

Assume that the time taken to allocate memory to a process is 0 .

Input Format

The first line contains two integers n and m , where n is the number of processes and m is the amount of memory available initially. Then n lines follow, each line contains two integers dur and mem where dur is the time needed for the current process to complete and mem is the amount of memory it needs.

Constraints

  • 1n105
  • 1m109
  • 1duri106
  • 1memin

Output Format

Print in a single line, the total time taken to execute all the given processes.

Sample Input

5 20
5 10
6 11
4 8
2 9
3 10

Sample Output

14

Solution

Tip

  • 优先队列
  • 重定义操作符
  • 数据类型 long long

Code

#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;

struct comp{
    bool operator()(vector<long long> x, vector<long long> y){
        return x[0] > y[0];
    }
};

int os(){
    int n, m;
    cin >> n >> m;
    queue<vector<int>> rec;
    for(int i = 0; i < n; i++){
        int dur, mem;
        cin >> dur >> mem;
        vector<int> temp;
        temp.push_back(dur);
        temp.push_back(mem);
        rec.push(temp);
    }

    priority_queue<vector<long long>, vector<vector<long long>>, comp> q;
    int mem_left = m;
    long long time = 0;
    while(!rec.empty()){
        while(!rec.empty() && rec.front()[1] <= mem_left){
            vector<long long> temp = rec.front();
            rec.pop();

            temp[0] += time;
            mem_left -= temp[1];

            q.push(temp);
        }
        while(!q.empty()){
            if(!rec.empty() && rec.front()[1] <= mem_left){
                break;
            }
            vector<long long> temp = q.top();
            q.pop();

            time = temp[0];
            mem_left += temp[1];            
        }
    }

    while(!q.empty()){
        time = q.top()[0];
        q.pop();
    }

    return time;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值