首页 > 代码库 > UVa12100,Printer Queue

UVa12100,Printer Queue

水题,1A过的

数据才100,o(n^3)都能过,感觉用优先队列来做挺麻烦的,直接暴力就可以了,模拟的队列,没用stl

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <queue>#define maxn 100+5using namespace std;int mid[maxn],v[maxn],q[maxn*maxn],ans;int n,m,id;int init(){    memset(mid,0,sizeof(mid));    memset(v,0,sizeof(v));    memset(q,0,sizeof(q));    cin>>n>>m;    id=0;ans=0;    for (int i=0;i<n;i++){        mid[i]=i;        cin>>v[i];    }}int find_max(){    int max=0;    for (int i=0;i<n;i++)        max=v[i]>max?v[i]:max;    return max;}int work(){    int head,tail,max;    head=0;tail=n-1;    for (int i=0;i<n;i++)        q[i]=i;    while (head<tail){        int t=find_max();        if (q[head]==m&&t==v[m]){            break;        }else if (v[q[head]]==t) {            v[q[head]]=0;            head++;            ans++;        }else {            tail++;            q[tail]=q[head];            head++;        }    }}int main(){    int T;    cin>>T;    while (T-->0){        init();        work();        cout<<ans+1<<endl;    }}
View Code