zoukankan      html  css  js  c++  java
  • 【BZOJ 1216】 1216: [HNOI2003]操作系统 (模拟+优先队列)

    1216: [HNOI2003]操作系统

    Description

    写一个程序来模拟操作系统的进程调度。假设该系统只有一个CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的。其中运行优先级用自然数表示,数字越大,则优先级越高。如果一个进程到达的时候CPU是空闲的,则它会一直占用CPU直到该进程结束。除非在这个过程中,有一个比它优先级高的进程要运行。在这种情况下,这个新的(优先级更高的)进程会占用CPU,而老的只有等待。如果一个进程到达时,CPU正在处理一个比它优先级高或优先级相同的进程,则这个(新到达的)进程必须等待。一旦CPU空闲,如果此时有进程在等待,则选择优先级最高的先运行。如果有多个优先级最高的进程,则选择到达时间最早的。

    Input

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

    Output

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

    Sample Input

    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

    Sample Output

    1 6
    3 19
    5 30
    6 32
    8 34
    4 35
    7 40
    2 42

    HINT

    Source

    【分析】

      硬模拟、优先队列优化。

      要细心一点,一个进程可以先进行一点,然后被占用,然后再把剩下的完成,就是说可以分阶段完成同一个进程。

      其他就没什么好说的了。

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<queue>
     7 using namespace std;
     8 #define Maxn 15010
     9 
    10 struct node
    11 {
    12     int id,st,w,rk;
    13     friend bool operator < (node x,node y)
    14     {
    15         return (x.rk==y.rk)?(x.st>y.st):x.rk<y.rk;
    16     }
    17 };
    18 
    19 priority_queue<node > q;
    20 
    21 int op[Maxn*100],opp[Maxn*100],pl;
    22 
    23 int mymax(int x,int y) {return x>y?x:y;}
    24 
    25 int main()
    26 {
    27     node nw,now;
    28     int t=0;
    29     while(!q.empty()) q.pop();
    30     pl=0;
    31     while(scanf("%d%d%d%d",&nw.id,&nw.st,&nw.w,&nw.rk)!=EOF)
    32     {
    33         if(q.empty())
    34         {
    35             q.push(nw);
    36         }
    37         else
    38         {
    39             now=q.top();
    40             while(mymax(t,now.st)+now.w<=nw.st)
    41             {
    42                 t=mymax(t,now.st)+now.w;
    43                 op[++pl]=now.id;opp[pl]=t;
    44                 q.pop();
    45                 if(q.empty()) break;
    46                 now=q.top();
    47             }
    48             if(!q.empty())
    49             {
    50                 q.pop();
    51                 now.w-=nw.st-mymax(t,now.st);
    52                 q.push(now);
    53             }
    54             q.push(nw);
    55         }
    56         t=mymax(t,nw.st);
    57     }
    58     while(!q.empty())
    59     {
    60         node now=q.top();q.pop();
    61         t=mymax(t,now.st)+now.w;
    62         op[++pl]=now.id;opp[pl]=t;
    63     }
    64     for(int i=1;i<=pl;i++)
    65     {
    66         printf("%d %d
    ",op[i],opp[i]);
    67     }
    68     return 0;
    69 }
    View Code

    2017-01-14 09:34:28

  • 相关阅读:
    POJ 2503 Babelfish
    POJ 1182 食物链
    POJ 2352 Stars
    POJ 2528 Mayor's posters
    POJ 1328 Radar Installation
    POJ 1017 Packets
    jQuery实现登录提示
    【1-5】jQuery对象和DOM对象
    【1-4】jQuery代码风格-导航栏
    【3】买苹果
  • 原文地址:https://www.cnblogs.com/Konjakmoyu/p/6284667.html
Copyright © 2011-2022 走看看