您的位置:首页 > 其它

CCF认证 2017-09 公共钥匙盒

2018-02-07 11:28 363 查看
把所有拿钥匙还钥匙的操作和时间都保存在一个优先队列里,按时间先后排序

再用一个数组模拟钥匙盒,按顺序把队列里的操作过一遍,最后输出钥匙盒情况

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
struct node
{
int id;
int sta;
int t;
bool operator < (const node &u) const
{
if(t>u.t) return true;
if(t<u.t) return false;
if(sta>u.sta) return true;
if(sta<u.sta) return false;
if(id>u.id) return true;
return false;
}
};
priority_queue<node> q;
int a[1001];
int main()
{
int n,k,w,s,c;
scanf("%d%d",&n,&k);
node x;
while(k--)
{
scanf("%d%d%d",&w,&s,&c);
x.id=w;
x.sta=1;
x.t=s;
q.push(x);
x.sta=0;
x.t=s+c;
q.push(x);
}
for(int i=0;i<=n;i++)
a[i]=i;
while(!q.empty())
{
x=q.top();
q.pop();
if(x.sta==0)
{
for(int i=0;i<=n;i++)
if(a[i]==-1)
{
a[i]=x.id;
break;
}
}
else
{
for(int i=0;i<=n;i++)
if(a[i]==x.id)
{
a[i]=-1;
break;
}
}
}
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a
);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: