CCF 201709-2 公共钥匙盒
作者:互联网
问题分析这个题可以划分为两个过程,每个过程对应一个时间点,处理好数据以后加入优先队列中,然后遍历队列,模拟取钥匙和还钥匙的过程。
提交后100分的C++程序如下
// 201709-2 公共钥匙盒
#include<cstdio>
#include<queue>
using namespace std;
struct node{
int time; //取还钥匙的时间点
int num; //钥匙编号
char op; //操作
bool operator < (const node &a) const //重载“<”,按时间设定优先级
{
if(time != a.time){
return time > a.time;
}else if(op != a.op){
return op < a.op;
}else{
return num > a.num;
}
}
};
const int maxn = 1010;
int a[maxn];
int main()
{
priority_queue<node> q;
node t;
int n, k, w, s, c;
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i ++)
{
a[i] = i;
}
for(int i = 0; i < k; i ++)
{
scanf("%d%d%d", &w, &s, &c);
t.num = w;
t.time = s;
t.op = 'G';
q.push(t);
t.time = s + c;
t.op = 'R';
q.push(t);
}
while(!q.empty()) //遍历队列
{
t = q.top();
q.pop();
if(t.op == 'G'){ //模拟取钥匙
for(int i = 1; i <= n; i ++)
{
if(a[i] == t.num){
a[i] = 0;
break;
}
}
}else{ //模拟还钥匙
for(int i = 1; i <= n; i ++)
{
if(a[i] == 0){
a[i] = t.num;
break;
}
}
}
}
for(int i = 1; i < n; i ++)
{
printf("%d ", a[i]);
}
printf("%d\n", a[n]);
return 0;
}
标签:node,201709,const,int,num,钥匙,CCF,op 来源: https://blog.csdn.net/weixin_43832005/article/details/100083402