习题5-7 打印队列(Printer Queue, UVa12100)
1.adding and removing jobs from the queue is instantaneous.
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cctype>
#define CLOSE() ios::sync_with_stdio(false)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
const int maxn = 1e5 + 5;
using LL = long long;
using UI = unsigned int;
using namespace std;
//------------------------------------------------------------------------------------------//
struct A {
int id, pri;
bool operator<(const A &rhs) {
return pri > rhs.pri;
}
}a[105];
int main() {
int T;
//IN(); OUT();
scanf("%d", &T);
int n, tgt;
while (T--) {
scanf("%d%d", &n, &tgt);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i].pri);
a[i].id = i;
}
queue<A> q;
int t = 0;
for (int i = 0; i < n; i++) q.push(a[i]);
sort(a, a + n);
int pos; for (pos = 0; a[pos].id != tgt; pos++);
int cnt = 0;
while (1) {
A x = q.front();//返回引用
q.pop(); //printf("x.id = %d\n", x.id);
if (q.empty()) { t++; break; }
if (x.pri != a[cnt].pri) {
q.push(x);
}
else {
t++;
cnt++;//printf("pop(%d)\n", x.id);
if (x.id == tgt) break;
}
}
printf("%d\n", t);
}
return 0;
}
/*
deque iterator not dereferencable :
当队列为空时,进行front(),也会产生这种错误,
所以,在进行front时,首先判断是否empty(),如果emtpy返回一个错误的值,就可以了。
*/
习题5-7 打印队列(Printer Queue, UVa12100)
最新推荐文章于 2023-10-05 13:10:08 发布