用一个队列模拟,还有一个数组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. 理解 Memory barrier

    理解 Memory barrier(内存屏障) 发布于 2014 年 04 月 21 日2014 年 05 月 15 日 作者 name5566 参考文献列表:http://en.wikipedia. ...

  2. javascript实现KMP算法(没啥实用价值,只供学习)

    简单粗暴上代码 KMP的原理我就不讲了,想转过弯儿来不容易,建议大家先学会了怎么推导出next数组规律,然后准备两张纸,大纸上写上一行你要匹配的目标字符串,并分别写出位置编号,小纸上写上一行,也写上位 ...

  3. POJ 1986 Distance Queries (最近公共祖先,tarjan)

    本题目输入格式同1984,这里的数据范围坑死我了!!!1984上的题目说边数m的范围40000,因为双向边,我开了80000+的大小,却RE.后来果断尝试下开了400000的大小,AC.题意:给出n个 ...

  4. HDU 2852 KiKi's K-Number(树状数组+二分搜索)

    题意:给出三种操作 0 e:将e放入容器中 1 e:将e从容器中删除,若不存在,则输出No Elment! 2 a k:搜索容器中比a大的第k个数,若不存在,则输出Not Find! 思路:树状数组+ ...

  5. android:scaleType属性

    android:scaleType是控制图片如何resized/moved来匹对ImageView的size. ImageView.ScaleType / android:scaleType值的意义区 ...

  6. ExtJs之Ext.query

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  7. poj 1085 Triangle War 博弈论+记忆化搜索

    思路:总共有18条边,9个三角形. 极大极小化搜索+剪枝比较慢,所以用记忆化搜索!! 用state存放当前的加边后的状态,并判断是否构成三角形,找出最优解. 代码如下: #include<ios ...

  8. python爬煎蛋妹子图

    # python3 # jiandan meizi tu import urllib import urllib.request as req import os import time import ...

  9. SSH开发实践part4:Spring整合Struts

    1 好了,前面spring与hibernate的整合开发我们基本上讲完了,现在要开始服务层的开发,也就是处理事务的action,在这里我们需要引入spring与struts的整合.也就是将action ...

  10. MongoDB驱动特性检查列表

    http://docs.mongodb.org/meta-driver/latest/legacy/feature-checklist-for-mongodb-drivers/ 1. 基本(Essen ...