题目链接: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. PHP简单漂亮的分页类

    本文介绍一款原生的PHP分页类,分页样式有点类似bootstrap. <?php /* * ********************************************* * @类名 ...

  2. gulp进阶构建项目由浅入深

    gulp进阶构建项目由浅入深 阅读目录 gulp基本安装和使用 gulp API介绍 Gulp.src(globs[,options]) gulp.dest(path[,options]) gulp. ...

  3. mongodb_修改器($inc/$set/$unset/$push/$pop/upsert......)

    主从复制:http://blog.csdn.net/drifterj/article/details/7833883 对于文档的更新除替换外,针对某个或多个文档只需要部分更新可使用原子的更新修改器,能 ...

  4. PHP基础 之 数组(一)

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. maven之window安装

    1.下载:apache-maven-3.3.9-bin.zip 2.解压下载的maven文件到任意指定文件夹 3.配置maven 右键“我的电脑” -> "属性" 在打开的属 ...

  6. Android开发App工程结构搭建

    本文算是一篇漫谈,谈一谈关于android开发中工程初始化的时候如何在初期我们就能搭建一个好的架构.      关于android架构,因为手机的限制,目前我觉得也确实没什么大谈特谈的,但是从开发的角 ...

  7. 创建第一个JBPM6项目并且运行自带的helloword例子(JBPM6学习之三)

    1. 打开Eclipse,右键New JBPM Project 项目,在项目名称里面填写一个项目名字,如“TestJbpm6”,然后下一步,知道Finish完成(里面会使用我们配置的运行环境). 2. ...

  8. webpack的最简单应用,只使用js与css的打包

    首先进行全局安装webpack npm install -g webpack cmd跳转到项目的文件夹,安装webpack npm install --save-dev webpack 接着需要pac ...

  9. AJAX 页面数据传递

    $.ajax({ //一个Ajax过程 type: "post", //以post方式与后台沟通 url: "personstockajax.php", //与 ...

  10. NDK学习一: 环境搭建Eclipse篇

    下载NDK 国内的一个镜像站点 下载速度比较快 http://wear.techbrood.com/tools/sdk/ndk/#Installing 网上搭建环境的方案有很多 1. Eclipse ...