您的位置:首页 > 其它

uva12100解题报告

2017-05-22 11:01 387 查看
水题留念

一个队列模拟进出操作,一个优先队列保存优先级,模拟过程输出结果

Time 0ms

#include<cstdio>
#include<queue>
#include<cstring>
#include<sstream>
using namespace std;

const int maxn=100+10;
typedef pair<int,int> P;

struct cmp
{
bool operator () (const P &p1,const P &p2)const{
return p1.first<p2.first;
}
};

int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
queue<P> q1;
priority_queue<P,vector<P>,cmp > q2;
int ver[15];
memset(ver,0,sizeof(ver));
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
ver[x]++;
P p=make_pair(x,i);
q1.push(p);
}
for(int i=1;i<=9;i++)
if(ver[i]>0) { P p=make_pair(i,ver[i]); q2.push(p); }
int ans=0;
while(1)
{
P p=q1.front(); q1.pop();
if(p.first!=q2.top().first) q1.push(p);
else{
if(p.second==m) { ans++; break; }
else {
ans++; P p1=q2.top(); q2.pop();
if(p1.second>1){
p1.second--;
q2.push(p1);
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: