题目链接: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. 11个很棒的 jQuery 图表库

    如果你曾经使用过任何类型的数据,你应该知道阅读一排排数据的痛苦.通过所有这些数据弄清楚他们的意思是非常不容易的.可视化对于解决这个问题起到了重要的作用.可视化降低了数据阅读的难度,帮助决策者获得可操作 ...

  2. 用GL画出人物的移动路径

    注意:用Debug画的线会存在穿透问题 没啥好解释的,直接看代码: using UnityEngine; using System.Collections; using System.Collecti ...

  3. MySQL配置文件my.cnf中文详解附mysql性能优化方法分享

    Mysql参数优化对于新手来讲,是比较难懂的东西,其实这个参数优化,是个很复杂的东西,对于不同的网站,及其在线量,访问量,帖子数量,网络情况,以及机器硬件配置都有关系,优化不可能一次性完成,需要不断的 ...

  4. 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“System.ServiceModel.Activation.HttpModule”。

    ********************************* 思路壹(也是网络上铺天盖地的通俗解决方案) 原因: 这是因为先安装了 .NET Framework , 随后启用了 .NET Fra ...

  5. js网页中调用本地应用程序

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="Con ...

  6. 分析Masonry

    一. 继承关系 1.MASConstraint (abstract) MASViewContraint MASComposisionConstraint 2. UIView NSLayoutConst ...

  7. web安全及防护

    本文将对web方面的安全问题以及相应的防护方法进行一个简单的介绍. SQL注入(SQL Injection) 原理:就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺 ...

  8. Internet与www的关系

    Internet是把分布于世界各地不同结构的计算机网络用各种传输介质相互连接起来的网络. 因此,被称为网络的网络.Internet提供的主要服务有万维网(WWW.)文件传输(FTP.)电子邮件(E-m ...

  9. qt-4.8.5 显示图片居中笔记

    已经太久没有写过qt的程序了,所以导致的后果就是一个很简单的程序写了老半天还没写完整. 今天想实现的功能在原来软件的基础上显示他的版本. 因为想在该界面显示一个logo,一开始在pc机上跑发现图片一直 ...

  10. mysql的安装以及基本操作

    一.在Linux 下安装MySQL ubuntu 下可以直接使用apt-get . centos 下yum源有没有就不知道了. 1. sudo apt-get install mysql-server ...