zoukankan      html  css  js  c++  java
  • P2278 操作系统

    P2278 操作系统

    题目描述

    写一个程序来模拟操作系统的进程调度。假设该系统只有一个CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的。其中运行优先级用自然数表示,数字越大,则优先级越高。

    如果一个进程到达的时候CPU是空闲的,则它会一直占用CPU直到该进程结束。除非在这个过程中,有一个比它优先级高的进程要运行。在这种情况下,这个新的(优先级更高的)进程会占用CPU,而老的只有等待。

    如果一个进程到达时,CPU正在处理一个比它优先级高或优先级相同的进程,则这个(新到达的)进程必须等待。

    一旦CPU空闲,如果此时有进程在等待,则选择优先级最高的先运行。如果有多个优先级最高的进程,则选择到达时间最早的。

    输入输出格式

    输入格式:

    输入包含若干行,每一行有四个自然数(均不超过10^8),分别是进程号,到达时间,执行时间和优先级。不同进程有不同的编号,不会有两个相同优先级的进程同时到达。输入数据已经按到达时间从小到大排序。输入数据保证在任何时候,等待队列中的进程不超过15000个。

    输出格式:

    按照进程结束的时间输出每个进程的进程号和结束时间。

    模拟,优先队列

    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <queue>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    #define res register int
    
    struct node{
    	int num,st,len,val;
    	bool operator<(const node &n2) const {
    		return val<n2.val || val==n2.val&st>n2.st;//´óµÄ»áÅÅÔÚÇ°Ãæ 
    	}
    };
    priority_queue<node> q;
    int tim,n;
    node c;
    
    inline int read()
    {
    	int x=0,f=1;char ch;
    	while(!isdigit(ch=getchar())) if(ch=='-') f=-1;
    	while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    	return f*x;
    }
    
    int main()
    {
    	while(scanf("%d",&c.num)!=EOF)
    	{
    		c.st=read(); c.len=read(); c.val=read();
    		while(q.size() && q.top().len+tim <= c.st)
    		{
    			node tmp=q.top(); q.pop();
    			printf("%d %d
    ",tmp.num,tmp.len+tim);
    			tim+=tmp.len;
    		}
    		if(q.size())
    		{
    			node t=q.top(); q.pop();
    			t.len=t.len+tim-c.st;
    			q.push(t);
    		}
    		tim=c.st;
    		q.push(c);
    	}
    	while(q.size())
    	{
    		node t=q.top(); q.pop();
    		tim+=t.len;
    		printf("%d %d
    ",t.num,tim);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Java反编译代码分析(一)
    Java信号量Semaphore
    Ubuntu SVN安装&使用&命令
    Android -- Dialog动画
    Android -- EventBus使用
    Android -- queryIntentActivities
    解决:fatal: authentication failed for https
    MySQL表名大小写敏感导致的问题
    Publish to a Linux Production Environment
    layer.js 弹窗组件API文档
  • 原文地址:https://www.cnblogs.com/wmq12138/p/10507368.html
Copyright © 2011-2022 走看看