• hdu 1972.Printer Queue 解题报告


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1972

    题目意思:需要模拟打印机打印。打印机里面有一些 job,每个job被赋予1~9的其中一个值,越大表示优先级越高,越早被打印。job这个队列是会向前推进的,如果排在最前面的job优先级最高,那么才打印,否则就把这个job放到队列最后。问给出 m 这个位置的job要经过多长时间才被打印。 规定每次打印时间为一分钟,移动 job到队列最后不占时间。

      练开优先队列就继续吧~~~不过这题不是咯。

      仅仅用到队列来做。不过感觉用数组模拟队列更简单。

      (1)数组版(一步一步模拟)

      

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 
     6 const int maxn = 100 + 5;
     7 int queue[maxn*maxn];
     8 
     9 int main()
    10 {
    11     #ifndef ONLINE_JUDGE
    12         freopen("in.txt", "r", stdin);
    13     #endif // ONLINE_JUDGE
    14     int n, m, T;
    15     while (scanf("%d", &T) != EOF) {
    16         while (T--) {
    17             scanf("%d%d", &n, &m);
    18             for (int i = 0; i < n; i++)
    19                 scanf("%d", &queue[i]);
    20 
    21             int ans = 0;
    22             int front = 0, rear = n;
    23             bool flag = false;
    24             while (!flag) {
    25                 for (int i = front; i < rear; i++) {
    26                     if (queue[i] > queue[front]) {
    27                         if (front == m) {
    28                             m = rear;     // 记录 m 转移到队尾后的位置
    29                         }
    30                         queue[rear++] = queue[front];
    31                         break;
    32                     }
    33                     if (i == rear-1) {
    34                         ans++;
    35                         if (front == m) {
    36                             flag = true;
    37                             break;
    38                         }
    39                     }
    40                 }
    41                 front++;
    42             }
    43             printf("%d
    ", ans);
    44         }
    45     }
    46     return 0;
    47 }
    View Code

          (2)数据结构 queue 版,需要用一个辅助数组来保存优先级。

      

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <queue>
     6 using namespace std;
     7 
     8 const int maxn = 100 + 10;
     9 struct node
    10 {
    11     int pos;
    12     int priority;
    13 };
    14 
    15 int cmp(int x, int y)
    16 {
    17     return x > y;
    18 }
    19 
    20 int main()
    21 {
    22     int T, n, m;
    23     int a[maxn];
    24 
    25     while (scanf("%d", &T) != EOF) {
    26         while (T--) {
    27             queue<node> q;
    28             node t;
    29             scanf("%d%d", &n, &m);
    30             for (int i = 0; i < n; i++) {
    31                 scanf("%d", &a[i]);
    32                 t.pos = i;
    33                 t.priority = a[i];
    34                 q.push(t);
    35             }
    36             sort(a, a+n, cmp);
    37             int ans = 1, i = 0;
    38             while (1) {
    39                 t = q.front();
    40                 q.pop();
    41                 if (a[i] == t.priority && m == t.pos)
    42                     break;
    43                 else if (a[i] == t.priority && m != t.pos)
    44                     ans++, i++;
    45                 else
    46                     q.push(t);
    47             }
    48             printf("%d
    ", ans);
    49         }
    50     }
    51     return 0;
    52 }
    View Code

      要注意一些细节:

      如果 queue<node>q 声明在main 里,则不需要最后的

      while (!q.empty())  

        q.pop();

      声明在 main 外 就需要这两行,否则会TLE。

  • 相关阅读:
    python基础day3-今日内容,2019-6-25
    python基础day3-视频下载,2019-6-25
    append,extend,insert的区别
    DOM基础之获取元素
    p1553数组反转
    python函数
    python文件处理
    day02 python基础之列表,元祖,字典
    day01 python基础
    Python绘图Turtle库详解
  • 原文地址:https://www.cnblogs.com/windysai/p/4294611.html
Copyright © 2020-2023  润新知