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

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

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

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

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

  

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = + ;
int queue[maxn*maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n, m, T;
while (scanf("%d", &T) != EOF) {
while (T--) {
scanf("%d%d", &n, &m);
for (int i = ; i < n; i++)
scanf("%d", &queue[i]); int ans = ;
int front = , rear = n;
bool flag = false;
while (!flag) {
for (int i = front; i < rear; i++) {
if (queue[i] > queue[front]) {
if (front == m) {
m = rear; // 记录 m 转移到队尾后的位置
}
queue[rear++] = queue[front];
break;
}
if (i == rear-) {
ans++;
if (front == m) {
flag = true;
break;
}
}
}
front++;
}
printf("%d\n", ans);
}
}
return ;
}

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

  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int maxn = + ;
struct node
{
int pos;
int priority;
}; int cmp(int x, int y)
{
return x > y;
} int main()
{
int T, n, m;
int a[maxn]; while (scanf("%d", &T) != EOF) {
while (T--) {
queue<node> q;
node t;
scanf("%d%d", &n, &m);
for (int i = ; i < n; i++) {
scanf("%d", &a[i]);
t.pos = i;
t.priority = a[i];
q.push(t);
}
sort(a, a+n, cmp);
int ans = , i = ;
while () {
t = q.front();
q.pop();
if (a[i] == t.priority && m == t.pos)
break;
else if (a[i] == t.priority && m != t.pos)
ans++, i++;
else
q.push(t);
}
printf("%d\n", ans);
}
}
return ;
}

  要注意一些细节:

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

  while (!q.empty())

    q.pop();

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

hdu 1972.Printer Queue 解题报告的更多相关文章

  1. HDU 4303 Hourai Jeweled 解题报告

    HDU 4303 Hourai Jeweled 解题报告 评测地址: http://acm.hdu.edu.cn/showproblem.php?pid=4303 评测地址: https://xoj. ...

  2. hdu 2544 最短路 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目意思:给出 n 个路口和 m 条路,每一条路需要 c 分钟走过.问从路口 1 到路口 n 需 ...

  3. 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...

  4. ACM 杭电HDU 2084 数塔 [解题报告]

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  5. hdu 1014.Uniform Generator 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014 题目意思:给出 STEP 和 MOD,然后根据这个公式:seed(x+1) = [seed(x) ...

  6. hdu 1098 Lowest Bit 解题报告

    题目链接:http://code.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=2&problemid=22 ...

  7. hdu 1232 畅通工程 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 并查集入门题.最近在学并查集,它无非包括三个操作:make_set(x).union_set(x ...

  8. hdu 1050 Moving Tables 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 这道题目隔了很久才做出来的.一开始把判断走廊有重叠的算法都想错了.以为重叠只要满足,下一次mov ...

  9. hdu 1113 Word Amalgamation 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 题意:输入一个字典,然后再输入若干单词(每行中,1 <= 单词数 <= 100,并且 ...

随机推荐

  1. Easyui中使用jquery或js动态添加元素时出现的样式失效的解决方法

    Easyui中使用jquery或js动态添加元素时出现的样式失效的解决方法 2014-03-27 11:44:46|  分类: Easy UI|举报|字号 订阅     可以使用$.parser.pa ...

  2. 从Google开源RE2库学习到的C++测试方案

    最近因为科研需求,一直在研究Google的开源RE2库(正则表达式识别库),库源码体积庞大,用C++写的,对于我这个以前专供Java的人来说真的是一件很痛苦的事,每天只能啃一点点.今天研究了下里面用到 ...

  3. pip高级使用技巧以及搭建自己的pypi服务器

    =========================  pip 访问非官方pypi源, 以及代理的设置=========================在Windows下安装某些Python的C ext ...

  4. 给select添加自定义值和选项

    添加选项: document.getElementById("id_select").options.add(new Option("name", " ...

  5. 360随身WIFI程序单文件绿色版及网卡驱动(附使用感受)

    大家好,我是Colin,今天刚收到传说中的360WIFI,拿到手后马上就进行了测试.就做工而言,19.9的价格算是比较公道的,网卡很小,做工还可以,带磨砂质感,而且还提供了一个耳机插头,可以当挂件一样 ...

  6. zookeeper集群配置与启动——实战

    1,准备: A:三台linxu服务器: 10.112.29.177 10.112.29.172 10.112.29.174 命令 hostname 得到每台机器的 hostname vm-10-112 ...

  7. [Storm] 内部消息缓存

    这篇文件翻译自 http://www.michael-noll.com/blog/2013/06/21/understanding-storm-internal-message-buffers/ 当进 ...

  8. Thank you for your resubmission. Performance - 2.3.10 We noticed that your app or its metadata includes irrelevant third-party platform information. Specifically, Android logo is mentioned in the

    被拒很多次,各种修改,最后发现是提交的时候,含有安卓的图标!欲哭无泪呀! Thank you for your resubmission. Performance - 2.3.10 We notice ...

  9. Magic Number(Levenshtein distance算法)

    Magic Number Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  10. Windows如何使用jstack跟踪异常代码

    维护服务器时,会出现java进程在CPU.内存.硬盘上总是出现异常情况. 如何找到是哪些代码出现这些异常呢? 本文使用jstack来实现这个需求 工具/原料   java jstack Process ...