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;
}
再用一个数组模拟钥匙盒,按顺序把队列里的操作过一遍,最后输出钥匙盒情况
#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;
}
相关文章推荐
- CSP - 2017 -09 - 2 公共钥匙盒
- CCF认证 2017-09 通信网络
- CCF认证 2017-09 除法
- CCF认证 2017-09 打酱油
- CCF认证-20170902-公共钥匙盒
- CCF认证 2017-09 JSON查询
- 4000 PAT 甲级2017_12_09_B_Final_Grading_25_2
- 美团2017校招-最长公共连续子串
- 2017 09 02 小结
- CCF认证 2017-03 地铁修建
- 公共钥匙盒
- CCF认证 2017-12 行车路线
- CCF认证 2014-09 字符串匹配
- 2017 09 11指针复习
- 2017校园招聘编程题——两个字符串中找到最大公共字符串
- 201709-2 公共钥匙盒 ccf
- ccf/公共钥匙盒
- ccf 2017 09 4 通信网络
- 每日练习2017-11-09
- CCF认证 2014-09 最优配餐