题目链接: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. sql sever 字符串函数

    SQL Server之字符串函数   以下所有例子均Studnet表为例:  计算字符串长度len()用来计算字符串的长度 select sname ,len(sname) from student ...

  2. HDU 2014

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> typedef float ElementType; void Select_Sort ...

  3. android 读取SQLite android could not open the database in read/write mode错误

    由于AndroidManifest.xml文件中uses-permission没有设置权限问题 <uses-permission android:name="android.permi ...

  4. Cotex-M3内核LPC17xx系列时钟及其配置方法

    一.背景: 最近正在接手一个项目,核心芯片既是LPC17XX系列MCU,内核为ARM的Cotex-M3内核. 想要玩转一个MCU,就一定得搞定其时钟! 时钟对MCU而言,就好比人类的心脏.由其给AHB ...

  5. Logistic 回归(sigmoid函数,手机的评价,梯度上升,批处理梯度,随机梯度,从疝气病症预测病马的死亡率

    (手机的颜色,大小,用户体验来加权统计总体的值)极大似然估计MLE 1.Logistic回归 Logistic regression (逻辑回归),是一种分类方法,用于二分类问题(即输出只有两种).如 ...

  6. Ubuntu 14 如何解压 .zip、.rar 文件?

    .zip 和 .rar 是Windows下常用的压缩文件,在Ubuntu中如何解压? [解压.zip文件] Ubuntu中貌似已经安装了unzip软件,解压命令如下: unzip ./FileName ...

  7. 如何使用Android中hide的类和方法进行开发?

    1.获取Android源码并进行编译. 2.编译完毕后,取出out\target\common\obj\JAVA_LIBRARIES\framework_intermediates路径下的classe ...

  8. div动态显示iframe内容

    在div里隐藏不了iframe <div id="popupmenu" style="position:relative; display:none; z-inde ...

  9. java使用split分隔,需要注意的点

    String severName = "10.6.62.244"; System.out.println(severName.split(".").length ...

  10. CSU 1120 病毒(DP)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 解题报告:dp,用一个串去更新另一个串,递推方程是: if(b[i] > a ...