用一个队列模拟,还有一个数组cnt记录9个优先级的任务的数量,每次找到当前最大优先级的任务然后出队,并及时更新cnt数组。

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std; struct Task
{
int pos, pri;
Task(int pos = , int pri = ):pos(pos), pri(pri) {}
}; int cnt[]; int main()
{
//freopen("in.txt", "r", stdin); int T; scanf("%d", &T);
while(T--)
{
int n, p;
scanf("%d%d", &n, &p);
memset(cnt, , sizeof(cnt));
queue<Task> Q;
for(int i = ; i < n; i++)
{
int pri;
scanf("%d", &pri);
Q.push(Task(i, pri));
cnt[pri]++;
}
while()
{
Task t = Q.front();
int m;
for(m = ; m >= t.pri; m--) if(cnt[m]) break;
cnt[m]--;
if(m > t.pri)
{
while(t.pri != m)
{
Q.pop();
Q.push(t);
t = Q.front();
}
Q.pop();
if(t.pos == p) break;
//printf("sz = %d\n", Q.size());
}
else
{
Q.pop();
if(t.pos == p) break;
}
}
printf("%d\n", n - Q.size());
} return ;
}

代码君

UVa 12100 (模拟) Printer Queue的更多相关文章

  1. 【习题 5-7 UVA - 12100】Printer Queue

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用队列和multiset就能完成模拟 [代码] #include <bits/stdc++.h> using names ...

  2. UVa 12100 Printer Queue(queue或者vector模拟队列)

    The only printer in the computer science students' union is experiencing an extremely heavy workload ...

  3. uva 12100 Printer Queue

    The only printer in the computer science students' union is experiencing an extremely heavy workload ...

  4. Printer Queue UVA - 12100

    The only printer in the computer science students' union is experiencing an extremely heavy workload ...

  5. 12100 Printer Queue(优先队列)

    12100 Printer Queue12 The only printer in the computer science students’ union is experiencing an ex ...

  6. Printer Queue

    Description The only printer in the computer science students' union is experiencing an extremely he ...

  7. POJ 3125 Printer Queue

    题目: Description The only printer in the computer science students' union is experiencing an extremel ...

  8. [刷题]算法竞赛入门经典(第2版) 5-7/UVa12100 - Printer Queue

    题意:一堆文件但只有一个打印机,按优先级与排队顺序进行打印.也就是在一个可以插队的的队列里,问你何时可以打印到.至于这个插队啊,题目说"Of course, those annoying t ...

  9. J - Printer Queue 优先队列与队列

    来源poj3125 The only printer in the computer science students' union is experiencing an extremely heav ...

随机推荐

  1. Mozilla推荐的CSS属性书写顺序及命名规则

    传说中的Mozilla推荐 /* mozilla.org Base Styles * maintained by fantasai */ /* Suggested order: * display * ...

  2. 理解Session的几种模式

    一.写在前面 我们在使用ASP.NET开发的过程中,有时会进行数据存储以实现请求前后的状态保持(HTTP是无状态保持的协议),而Session作为一种快速简单易于实现的方式被我们经常使用,当然如果出于 ...

  3. 浅谈 OneAPM 在 express 项目中的实践

    [编者按]OneAPM 运营团队,近日在 github 上发现了一篇文章,特别奉献给大家.本文作者王宇先生从2015年年初就开始使用我们的产品,也是OneAPM 的忠实用户. OneAPM 是一个优秀 ...

  4. ECMAScript5下Array的方法

    声明:ECMAScript不会兼容IE8及以下版本IE浏览器. 一.迭代方法 注:这些迭代方法不会影响数组的值. 每个方法都有两个参数: array.方法(执行函数体,当前作用域(比如this,这个可 ...

  5. HDU4782 Beautiful Soup

    成都赛里的一道坑爹码力题,突然间脑抽想做一下弥补一下当时的遗憾.当时没做出这道题一是因为当时只剩大概45分钟,对于这样的具有各种条件的题无从下手,二则是因为当时估算着已经有银牌了,所以就不挣扎了.但是 ...

  6. ZOJ3718 Diablo II(状态压缩dp)

    题意:一个人物有K(K<=7)种技能,每种技能都有bi,ci,di值,表示该技能不能点超过bi次,每点一次加ci,点满bi次有一个附加得分di.然后还有N件武器,武器本身会有能力加成,然后每个武 ...

  7. POJ 1496

    #include <iostream> #include <string> using namespace std; int fac(int num); int C(int n ...

  8. ResourceBundle使用

    一.认识国际化资源文件   这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以:          轻松地本地化或翻译成不同的语言          一次处理多个语言环境          ...

  9. lintcode :单词搜索

    题目 单词搜索 给出一个二维的字母板和一个单词,寻找字母板网格中是否存在这个单词. 单词可以由按顺序的相邻单元的字母组成,其中相邻单元指的是水平或者垂直方向相邻.每个单元中的字母最多只能使用一次. 样 ...

  10. tomcat console

    1.大家都知道,在Tomcat5及其以后的版本中,当启动tomcat之后,是看不到控制台中的manager应用的.Manager的应用还是很有好处的,可以直接在控制台上(类似于weblogic上的co ...