Description
Murder in closet happened again in a small restaurant and Conan went there to collect evidence with Kogoro. After they reached the restaurant, they got a list of renters that lived in this restaurant recently. Yet the list was so long that they couldn’t get the useful information from it. As an assistant of Conan, you must have been ready to help him to get the information on which rooms the renters have lived in.
Input
There are no more than 20 test cases. The first line of each test case contains two integers n and m, indicating the number of renters and the rooms of the restaurant (0 < n, m <= 100). The i-th line of the next n lines contains two integers t1 and t2, the day when they wanted to check in and to leave (0 < t1 < t2 <= 1000).
Each renter rends exactly one room and their check-in days are distinct. Each time a renter came, the owner would give him/her an available room with minimum room number if there were still empty rooms. Otherwise the renter would leave at once and never come back again. Note that rooms are always returned in morning and rented in afternoon. The input is ended with two zeroes.
Output
For each test case, output n lines of integers. The i-th integer indicates the room number of the i-th renter. The rooms are numbered from 1. If someone didn’t live in this restaurant, output 0 in the corresponding line.
Sample Input
2 5 1 3 2 4 4 2 1 5 2 3 3 5 4 5 0 0
Sample Output
1 2 1 2 2 0
说下题目大意:给出来住宿的人数n,旅馆有m间房,给出n个人的入住的时间和离开的时间,问这n个人住的是哪间房子,输出房号,没有入住的输出0.
这个题目主要是数据可能没有排序,所以要先排序,然后模拟就好了。
题目推荐:2颗星
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct point
{
int to,from;//入住时间 和离开时间
int num,room;
};
point room[101];
point temp[101];
int n,m;
int cmp(point a,point b)
{
if(a.from!=b.from)
{
return a.from<b.from;
}
else
{
return a.to<b.to;
}
}
int cmp1(point a,point b)
{
return a.num<b.num;
}
int find(int from,int to,int m)
{
int i;
for(i=1;i<=m;i++)
{
if(room[i].from==0&&room[i].to==0)
{//为初始的0,当然是可以入住的
room[i].from=from;
room[i].to=to;
return i;
}
else if(room[i].to>from)
{//大于就不能住了,继续找
continue;
}
else if(room[i].to<=from)
{//这个可以住,那就住下了吧
room[i].from=from;
room[i].to=to;
return i;
}
}
return 0;
}
int main()
{
int i;
int to,from;
while(scanf("%d%d",&n,&m))
{
memset(room,0,sizeof(room));
if(n==0&&m==0)
{
break;
}
for(i=0;i<n;i++)
{
scanf("%d%d",&temp[i].from,&temp[i].to);
temp[i].num=i;//记录查询的先后顺序,输出要按照这个顺序输出
}
sort(temp,temp+n,cmp);//按照入住的时间从小到大排序
for(i=0;i<n;i++)
{
temp[i].room=find(temp[i].from,temp[i].to,m);//找哪个房间是可以住的,存起来
}
sort(temp,temp+n,cmp1);//还原查询是的顺序
for(i=0;i<n;i++)
{
printf("%d\n",temp[i].room);
}
}
return 0;
}