当前位置: 首页 > 工具软件 > Murder > 使用案例 >

ZOJ 3317 Murder in Restaurant(数学)

潘嘉颖
2023-12-01

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3317


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 < nm <= 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


Author:  GUAN, Yao
Source:  The 10th Zhejiang University Programming Contest

题意:

给出每个旅客来住房的到来时间和退房的时间! 每次旅店给客人的是空着的房间中标号最小的房间;

求每个旅客住得房间号数!

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
    int b, e;
    int num;
    int id;
    int ans;
} p[1047];
int ans[1047];
int num[1047];
int n, m;
int f[1047];//是否有人住
bool cmp(node x, node y)
{

    return x.b < y.b;
}
bool cmp2(node x, node y)
{
    return x.id < y.id;
}
int find()
{
    int maxx = 0;
    int flag = 0;
    for(int i = 1; i <= m; i++)
    {
        if(f[i] == 0)
        {
            return i;
        }
    }
    return 0;
}
int main()
{
    int a[1047], b[1047];
    while(cin>>n>>m)
    {
        if(n==0 && m==0)
        {
            break;
        }
        for(int i = 1; i <= n; i++)
        {
            p[i].ans = 0;
            p[i].num = 0;
        }
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&p[i].b,&p[i].e);
            p[i].id = i;
        }
        sort(p+1,p+1+n,cmp);
        memset(num,0,sizeof(num));
        memset(f,0,sizeof(f));
        int time = 0;//当前时间
        p[1].ans = 1;
        f[1] = 1;
        time = p[1].b;
        for(int i = 2; i <= n; i++)
        {
            time = p[i].b;
            for(int j = 1; j < i; j++)
            {
                if(time >= p[j].e && p[j].num == 0)
                {
                    f[p[j].ans] = 0;
                    p[j].num = 1;//已走
                }
            }
            p[i].ans = find();
            f[p[i].ans] = 1;
        }
        sort(p+1,p+n+1,cmp2);
        for(int i = 1; i <= n; i++)
        {
            printf("%d\n",p[i].ans);
        }
    }
    return 0;
}
/*
2 5
1 3
2 4
4 2
1 5
2 3
3 5
4 5
*/


 类似资料: