您的位置:首页 > 产品设计 > UI/UE

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: