UVA 12100
2016-02-11 00:01
190 查看
题意:有一个打印机,有一些任务在排着队打印,每个任务都有优先级。打印时,每次取出队列第一个任务,如果它的优先级不是当前队列中最高的,就会被放到队尾,否则就打印出来。输出初始队列的第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;
}
思路:用队列模拟一下。建一个<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;
}
相关文章推荐
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes
- A.537 - Artificial Intelligence?