UVA 12100
来源:互联网 发布:域名交易 知乎 编辑:程序博客网 时间:2024/05/22 04:44
题意:有一个打印机,有一些任务在排着队打印,每个任务都有优先级。打印时,每次取出队列第一个任务,如果它的优先级不是当前队列中最高的,就会被放到队尾,否则就打印出来。输出初始队列的第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;}
0 0
- uva 12100
- uva 12100
- UVA 12100
- UVa 12100
- UVA 12100
- UVA 12100
- Uva 12100
- UVa 12100 Printer Queue
- UVA - 12100 Printer Queue
- UVa 12100 - Printer Queue
- UVa 12100 - Printer Queue
- UVa: 12100 - Printer Queue
- UVa 12100 - Printer Queue
- uva 12100 Printer Queue
- Uva - 12100 - Printer Queue
- UVa 12100 打印队列
- UVA 12100 Printer Queue
- UVa 12100 - Printer Queue
- opencv 膨胀(dilate)与腐蚀(erode)---基本 数学形态学
- 1017. Queueing at Bank (25)
- UVA 10391
- bzoj1211: [HNOI2004]树的计数
- UVA 1595
- UVA 12100
- 这只是第一篇日志,以后每天都会更新
- UVA 230
- 关于尺寸单位
- UVA 1596
- 169. Majority Element
- 235. Lowest Common Ancestor of a Binary Search Tree
- servlet API
- java基础函数