zoj 3317 Murder in Restaurant

本文介绍了一个租房分配算法,该算法通过排序租客的入住时间来优化房间分配,确保每个租客尽可能入住已有房间,若无可用房间则分配新的房间。文章包含完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* 最近老是犯一些超级低级的错误 比如说看错题。。。 注意output里的这一句 "The i-th integer indicates the room number of the i-th renter" 这就暗示要对renter进行排序 如果你把实例中的renter顺序打乱后 输出的结果也是相应的打乱的结果 那估计就能AC了 */ #define LOCAL #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<iomanip> #include<string> #include<algorithm> #include<ctime> #include<stack> #include<queue> #include<vector> using namespace std; class ROOM {public:int come,leave;}; class RENTER {public:int num,come,leave;}; bool cmp(const RENTER &a,const RENTER &b) {if(a.come<b.come) return true;else return false;} int main() { #ifdef LOCAL freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int nrenter,nroom,a,b,i,j,full,find,k,ans[105]; ROOM room[105];RENTER renter[105]; while(cin>>nrenter>>nroom,nrenter) { for(i=0;i<nrenter;i++)//读入renter {cin>>renter[i].come>>renter[i].leave;renter[i].num=i;}//记录编号 sort(renter,renter+nrenter,cmp);//根据到来的日期进行排序 room[0].come=renter[0].come; //第一位renter入住 room[0].leave=renter[0].leave; full=1; //full记录的是已经有人入住的房间数 ans[renter[0].num]=1; //记录第一位renter的房间号 for(i=1;i<nrenter;i++)//对以后的各个renter进行房间分配 { find=0;//记录能不能在已经有人入住的房间里找到可住房 for(j=0;j<full;j++)//对有人入住的各个房间进行遍历 { if(renter[i].come>=room[j].leave)//可住 { room[j].come=renter[i].come; room[j].leave=renter[i].leave; ans[renter[i].num]=j+1; find=1; break; } } if(!find)//没有在有人入住的房间里找到可住房 { if(full<nroom)//如果还有空余房间就分配 { room[full].come=renter[i].come; room[full].leave=renter[i].leave; full++; ans[renter[i].num]=full; } else//否则无法入住 ans[renter[i].num]=0; } } for(i=0;i<nrenter;i++)//输出结果 cout<<ans[i]<<endl; } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值