内存是计算机重要的资源之一程序运行的过程中必须对内存进行分配。经典的内存分配过程是这样进行的:1、 内存以内存单元为基本单位每个内存单元用一个固定的整数作为标识称为地址。地址从0开始连续排列地址相邻的内存单元被认为是逻辑上连续的。我们把从地址i开始的s个连续的内存单元称为首地址为i长度为s的地址片。2、 运行过程中有若干进程需要占用内存对于每个进程有一个申请时刻T需要内存单元数M及运行时间P。在运
#include
using namespace std;
struct Process{ int T, M, P; int id; bool operator<(const Process& other) const{ return T > other.T; } };
struct Memory{ int start, end; bool operator<(const Memory& other) const{ return start > other.start; } };
int main(){
int N;
cin >> N;
priority_queue
priority_queue<Memory> memory_pq;
Memory memory = {0, N - 1};
memory_pq.push(memory);
int time = pq.top().T;
while(!pq.empty()){
Process process = pq.top();
pq.pop();
if(process.T > time){
time = process.T;
}
bool flag = false;
priority_queue<Memory> temp_pq;
while(!memory_pq.empty()){
Memory temp = memory_pq.top();
memory_pq.pop();
if(temp.end - temp.start + 1 >= process.M){
int start = temp.start;
int end = temp.start + process.M - 1;
temp.start = end + 1;
process.P--;
cout << process.T << " " << process.id << " " << start << endl;
time++;
if(process.P > 0){
pq.push(process);
}
flag = true;
if(temp.start <= temp.end){
temp_pq.push(temp);
}
break;
}else{
temp_pq.push(temp);
}
}
if(!flag){
pq.push(process);
while(!temp_pq.empty()){
memory_pq.push(temp_pq.top());
temp_pq.pop();
}
continue;
}
while(!temp_pq.empty()){
memory_pq.push(temp_pq.top());
temp_pq.pop();
}
}
return 0;
原文地址: https://www.cveoy.top/t/topic/fkK1 著作权归作者所有。请勿转载和采集!