UVA 12100
題意:有一個打印機,有一些任務在排著隊打印,每個任務都有優先級。打印時,每次取出隊列第一個任務,如果它的優先級不是當前隊列中最高的,就會被放到隊尾,否則就打印出來。輸出初始隊列的第m個任務的打印時間,每次打印花費單位1的時間。
思路:用隊列模擬一下。建一個<num,pos>的隊列q,num表示優先級,pos表示位置。然后再按照優先級建一個優先隊列pq。
每次先把q的隊首拿出來
(1)不是當前優先級最高的,num<pq.top(),再把出隊的這個元素push回去。
(2)是當前優先級最高的,num==pq.top()
如果pos?==?m?,輸出結束。
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <stack> #include <map> #include <set> #include <vector> #include <sstream> #include <queue> #include <utility> using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++) #define Rrep(i,j,k) for (int i=j;i>=k;i--)int T; int n,m,temp;struct node {int num,pos;node(int x = 0,int y = 0){num = x;pos = y;} }; int main() {cin>>T;while(T--){queue<node> q;priority_queue<int> pq;cin>>n>>m;rep(i,0,n-1){scanf("%d",&temp);q.push(node(temp,i));pq.push(temp);}int t;node x;int ans = 0;while(1){x = q.front();t = pq.top();q.pop();if ( t == x.num ){pq.pop();ans++;if ( x.pos == m ){cout<<ans<<endl;break;}}else q.push(x);}}return 0; }
總結
- 上一篇: js给img的src赋值
- 下一篇: 第28课:彻底解密Spark Sort-