之前把借和还分开了搞(定义两个结构体),就不能列出全部情况,不好写
参考
突破,时间借还编号排序
#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=a;i<b;i++)
struct TBR
{
int no;//钥匙编号
int t;//时间
int type;//借是1,还是0
};
bool cmp(TBR a,TBR b)
{
if(a.t!=b.t) return a.t<b.t;//时间不同,时间小的在前面
else//时间相同
{
if(a.type!=b.type) return a.type<b.type;//借在前面
else return a.no<b.no; //编号小的在前面
}
}
int main()
{
freopen("in.txt","r",stdin);
int N,K;
cin>>N>>K;
vector<TBR> tb(2*K);
int no,t1,t2;
for(int i=0;i<2*K;i++)
{
cin>>no>>t1>>t2;
tb[i].no=no;
tb[i].t=t1;
tb[i].type=1;
i++;
tb[i].no=no;
tb[i].t=t2+t1;
tb[i].type=0;
}
sort(tb.begin(),tb.end(),cmp);
vector<int> keys(N+1);
_for(i,1,N+1)//钥匙初始化
{
keys[i]=i;
}
_for(i,0,2*K)
{
if(tb[i].type==1)//借
{
_for(j,1,N+1)
{
if(keys[j]==tb[i].no)
{
keys[j]=0;
}
}
}
else
{
if(tb[i].type==0)//还
{
_for(j,1,N+1)
{
if(keys[j]==0)
{
keys[j]=tb[i].no;
break;
}
}
}
}
}
_for(i,1,N+1)
{
cout<<keys[i]<<" ";
}
//fclose(stdin);
return 0;
}