UVA12100 Printer Queue 【双端队列】

来源:互联网 发布:淘宝新店引流量 编辑:程序博客网 时间:2024/05/22 06:16

题目链接: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;}


0 0