UVA12100 Printer Queue 【双端队列】
2017-01-15 17:52
316 查看
题目链接:https://vjudge.net/problem/UVA-12100
题意:
有 n 个任务,每个任务都有一个优先级,优先级越高则越急。每次从队列首部取出一个任务J。按照如下思路打印:
如果队列里有比 J 更急的任务,则直接吧 J 放到队列尾部。否则进行打印,打印需要1分钟。(放到队列尾部不消耗时间,打印之后扔掉)
给你一个打印队列,求某个任务的打印时间(包括这个任务本身所用的打印时间)。
题解:
这是我的第一道双端队列题= =本来想直接用数组手切,结果发现太麻烦,数组大小也不好算。于是我想到了双端队列= =。
上网自行脑补双端队列有关知识。
然后就水过去了。
附代码:
#include <deque>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int size = 105;
struct _node{
int x;
bool isfd; // 表示是否是需要查询的点
}a[size];
int findmx(deque<_node> test) {
int mn = -(1 << 26);
while(!test.empty()) {
mn = max(mn, test.back().x);
test.pop_back();
}
return mn;
}
int main() {
int test;
scanf("%d", &test);
while( test -- ) {
deque<_node> dq;
int n, fd;
scanf("%d %d", &n, &fd);
for ( int i = 0; i < n; i++ ){
if(i == fd) a[i].isfd = 1;
else a[i].isfd = 0;
scanf("%d", &a[i].x);
dq.push_back(a[i]);
}
int ans = 0;
while( !dq.empty() ) {
int mx = findmx(dq);
_node vthis = dq.front();
if(vthis.x == mx) {
ans ++;
dq.pop_front();
if(vthis.isfd == 1) {
printf("%d\n", ans);
break;
}
} else {
dq.push_back(vthis);
dq.pop_front();
}
}
}
return 0;
}
题意:
有 n 个任务,每个任务都有一个优先级,优先级越高则越急。每次从队列首部取出一个任务J。按照如下思路打印:
如果队列里有比 J 更急的任务,则直接吧 J 放到队列尾部。否则进行打印,打印需要1分钟。(放到队列尾部不消耗时间,打印之后扔掉)
给你一个打印队列,求某个任务的打印时间(包括这个任务本身所用的打印时间)。
题解:
这是我的第一道双端队列题= =本来想直接用数组手切,结果发现太麻烦,数组大小也不好算。于是我想到了双端队列= =。
上网自行脑补双端队列有关知识。
然后就水过去了。
附代码:
#include <deque>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int size = 105;
struct _node{
int x;
bool isfd; // 表示是否是需要查询的点
}a[size];
int findmx(deque<_node> test) {
int mn = -(1 << 26);
while(!test.empty()) {
mn = max(mn, test.back().x);
test.pop_back();
}
return mn;
}
int main() {
int test;
scanf("%d", &test);
while( test -- ) {
deque<_node> dq;
int n, fd;
scanf("%d %d", &n, &fd);
for ( int i = 0; i < n; i++ ){
if(i == fd) a[i].isfd = 1;
else a[i].isfd = 0;
scanf("%d", &a[i].x);
dq.push_back(a[i]);
}
int ans = 0;
while( !dq.empty() ) {
int mx = findmx(dq);
_node vthis = dq.front();
if(vthis.x == mx) {
ans ++;
dq.pop_front();
if(vthis.isfd == 1) {
printf("%d\n", ans);
break;
}
} else {
dq.push_back(vthis);
dq.pop_front();
}
}
}
return 0;
}
相关文章推荐
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
- 关于“打印队列 Printer Queue, ACM/ICPC NWERC 2006, UVa 12100)"的讨论
- UVA ~ 12100 ~ Printer Queue (队列 + 优先队列)
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
- 5-7 UVA 12100 Printer Queue打印队列
- uva 12100 Printer Queue 习题5-7 打印队列 (STL deque练习)
- uva12100 Printer Queue (队列模拟, 据说是优先队列,错了)
- UVA 12100 Printer Queue(队列,优先队列)
- UVALive3638 UVA12100 POJ3125 HDU1972 Printer Queue【队列+模拟】
- UVa 12100 - Printer Queue【队列和优先队列】
- UVALive3638 UVA12100 POJ3125 HDU1972 Printer Queue(优先队列+队列)
- uva 12100 Printer Queue(打印队列)模拟
- Uva 12100 - Printer Queue
- UVa12100 - Printer Queue
- UVA - 12100-Printer Queue
- UVa12100 - Printer Queue
- 【习题 5-7 UVA - 12100】Printer Queue
- UVA - 12100 Printer Queue
- UVA12100 - Printer Queue