hdu 1972.Printer Queue 解题报告
题目链接: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 解题报告的更多相关文章
- HDU 4303 Hourai Jeweled 解题报告
HDU 4303 Hourai Jeweled 解题报告 评测地址: http://acm.hdu.edu.cn/showproblem.php?pid=4303 评测地址: https://xoj. ...
- hdu 2544 最短路 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目意思:给出 n 个路口和 m 条路,每一条路需要 c 分钟走过.问从路口 1 到路口 n 需 ...
- 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...
- ACM 杭电HDU 2084 数塔 [解题报告]
数塔 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- hdu 1014.Uniform Generator 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014 题目意思:给出 STEP 和 MOD,然后根据这个公式:seed(x+1) = [seed(x) ...
- hdu 1098 Lowest Bit 解题报告
题目链接:http://code.hdu.edu.cn/game/entry/problem/show.php?chapterid=1§ionid=2&problemid=22 ...
- hdu 1232 畅通工程 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 并查集入门题.最近在学并查集,它无非包括三个操作:make_set(x).union_set(x ...
- hdu 1050 Moving Tables 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 这道题目隔了很久才做出来的.一开始把判断走廊有重叠的算法都想错了.以为重叠只要满足,下一次mov ...
- hdu 1113 Word Amalgamation 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 题意:输入一个字典,然后再输入若干单词(每行中,1 <= 单词数 <= 100,并且 ...
随机推荐
- Unity3D
一.安装配置 1.下载页面:http://unity3d.com/get-unity 分个人版本和专业版,个人版免费,但少了很多功能(主要是渲染相关,具体请看官方说明), 专业版是每月 75$ 或一次 ...
- svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted (2014-08-1 ...
- godaddy空间的sql server数据库没办法insert中文
原来的代码: use string007 from sysobjects where id = object_id('K_V_TEST') and type = 'U') drop table K_V ...
- Mac Pro 软件收藏
记录下Pro Mac中安装过的软件: 编号 软件名 功能说明 1 QQ 2 微信 3 搜狗输入法 4 Chrome 浏览器 Chrome 及其 插件“个性化设置”备份 5 360云盘 ...
- ios 微博登录 21338错误 sso package or sign error
os中,是你项目的bundle id 和 连接的微博开发账户里面的bundle id 不一致造成的. 特别是,你把你注册的appKey放到你下载的官方的demo里面测试,肯定会出现这种情况, 所以,你 ...
- BZOJ3282——Tree
1.题目大意:动态树问题,点修改,链查询xor和 裸题一道.. #include <stack> #include <cstdio> #include <cstdlib& ...
- [BZOJ1833][ZJOI2010]count 数字计数
[BZOJ1833][ZJOI2010]count 数字计数 试题描述 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. 输入 输入文件中仅包含一行两个整数a ...
- C#高级编程笔记 Day 3, 2016年9月 8日 抽象类
1.虚方法:把一个基类函数声明为 virtual,就可以在任何派生类中重写该函数. 2.在Java 中所有函数都是虚拟的,但是在C# 中,C# 要求在派生类的函数重写另一个函数时,要使用 overri ...
- 跟着百度学PHP[4]-OOP面对对象编程-1-什么是面对对象编程
该阶段学习参考:http://www.cnblogs.com/xiaochaohuashengmi/archive/2010/09/10/1823042.html 面向对象编程(Object Orie ...
- 500 OOPS: vsftpd: both local and anonymous access
配置ftp服务器,有如下报错 C:\netos74\bin>ftp 10.20.100.252Connected to 10.20.100.252.500 OOPS: vsftpd: both ...