zoukankan      html  css  js  c++  java
  • P2278 [HNOI2003]操作系统 优先队列模拟

      

    题目描述

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

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

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

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

    输入输出格式

    输入格式:

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

    输出格式:

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

    输入输出样例

    输入样例#1: 复制
    1 1 5 3 
    2 10 5 1 
    3 12 7 2 
    4 20 2 3 
    5 21 9 4 
    6 22 2 4 
    7 23 5 2 
    8 24 2 4 
    
    输出样例#1: 复制
    1 6
    3 19
    5 30
    6 32
    8 34
    4 35
    7 40
    2 42


    好恶心一模拟题 一开始好不容易做完 发现每个任务可以分时间完成

    debug无能
    #include<bits/stdc++.h>
    using namespace std;
    //input by bxd
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);--i)
    #define RI(n) scanf("%d",&(n))
    #define RII(n,m) scanf("%d%d",&n,&m)
    #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define RS(s) scanf("%s",s);
    #define ll long long
    #define pb push_back
    #define REP(i,N)  for(int i=0;i<(N);i++)
    #define CLR(A,v)  memset(A,v,sizeof A)
    //////////////////////////////////
    #define inf 0x3f3f3f3f
    const int N=200000+5;
    
    struct node
    {
        int id,s,d,rank1;
        bool operator< (const node & b)const
        {
            return rank1<b.rank1||rank1==b.rank1&&s>b.s;
        }
    }s[N];
    
    
    int main()
    {
        priority_queue<node>q;
    
        node temp;
        int cnt=0;
        while(scanf("%d%d%d%d",&temp.id,&temp.s,&temp.d,&temp.rank1)==4)
        {
            s[++cnt]=temp;
        }
    
        int cur=1;
        int ti=s[1].s;
        while(cur<=cnt)
        {
    
            while(q.empty())
                q.push(s[cur]);
    
            ti=max(ti,q.top().s);
            while(max(ti,q.top().s)+q.top().d>s[cur+1].s&&cur+1<=cnt)
            {
                q.push(s[++cur]);
            }
            ti=max(ti,q.top().s);
            ti+=q.top().d;
            printf("%d %d
    ",q.top().id,ti);
            q.pop();
            if(q.empty())cur++;
        }
        while(!q.empty())
        {
            ti=max(ti,q.top().s);
            ti+=q.top().d;
            printf("%d %d
    ",q.top().id,ti);
            q.pop();
        }
        return 0;
    }
    View Code

    参考了大佬 学不来

    #include<bits/stdc++.h>
    using namespace std;
    //input by bxd
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);--i)
    #define RI(n) scanf("%d",&(n))
    #define RII(n,m) scanf("%d%d",&n,&m)
    #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define RS(s) scanf("%s",s);
    #define ll long long
    #define pb push_back
    #define REP(i,N)  for(int i=0;i<(N);i++)
    #define CLR(A,v)  memset(A,v,sizeof A)
    //////////////////////////////////
    #define inf 0x3f3f3f3f
    const int N=2000000+5;
    
    struct node
    {
        int id,s,d,rank1;
        bool operator< (const node & b)const
        {
            return rank1<b.rank1||rank1==b.rank1&&s>b.s;
        }
    }s[N];
    
    
    int main()
    {
        priority_queue<node>q;
    
        node temp;
        int cnt=0;
        while(scanf("%d%d%d%d",&temp.id,&temp.s,&temp.d,&temp.rank1)==4)
        {
            s[++cnt]=temp;
        }
    
        int rest=cnt;
        s[++cnt].s=inf;
        int cur=1;
        int ti;
        while(rest!=0)
        {
            if(q.empty())q.push(s[cur]),ti=s[cur].s,cur++;
            node temp=q.top();q.pop();
            int lasttime=ti;
            ti=min( s[cur].s,lasttime+temp.d );
            if(ti==lasttime+temp.d)
                printf("%d %d
    ",temp.id,ti),rest--;
            else temp.d-=ti-lasttime,q.push(temp);
            if(ti==s[cur].s)q.push(s[cur]),cur++;
        }
        return 0;
    }
    View Code













  • 相关阅读:
    Java 读取某个目录下所有文件、文件夹
    poi根据excel模板导出Excel
    UML关系(泛化,实现,依赖,关联(聚合,组合))
    使用Git下载Hadoop的到本地Eclipse开发环境
    maven command to create your application
    Roo中的@Version
    oracle 给用户赋表空间
    hiberante 二级缓存设置
    procedure
    Oracle综合数据库管理命令集
  • 原文地址:https://www.cnblogs.com/bxd123/p/10804788.html
Copyright © 2011-2022 走看看