ZOJ 3317 Murder in Restaurant(数学啊 )

题目链接: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
*/
时间: 2024-10-01 05:04:56

ZOJ 3317 Murder in Restaurant(数学啊 )的相关文章

HDU 1990 &amp; ZOJ 2992 Monkey Vines(数学啊)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1990 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1991 Problem Description Deep in the Amazon jungle, exceptionally tall trees grow that support a rich biosphere of figs and junipe

zoj 1028 Flip and Shift(数学)

Flip and Shift Time Limit: 2 Seconds      Memory Limit: 65536 KB This puzzle consists of a random sequence of m black disks and n white disks on an oval-shaped track, with a turnstile capable of flipping (i.e., reversing) three consecutive disks. In

ZOJ 3327 Friend Number(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3327 Friend Number Time Limit: 1 Second      Memory Limit: 32768 KB Given a positive integer x, let P(x) denotes the product of all x's digits. Two integers x and y are friend numbers

ZOJ 3600 Taxi Fare(数学啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4703 Last September, Hangzhou raised the taxi fares. The original flag-down fare in Hangzhou was 10 yuan, plusing 2 yuan per kilometer after the first 3km and 3 yuan per kilometer after

ZOJ 2680 Clock()数学

主题链接:problemId=1680" target="_blank">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1680 There is an analog clock with two hands: an hour hand and a minute hand. The two hands form an angle. The angle is measured as the small

ZOJ 3488 Conic Section(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4311 The conic sections are the nondegenerate curves generated by the intersections of a plane with one or two nappes of a cone. For a plane perpendicular to the axis of the cone, a circ

zoj 3041 City Selection(数学啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2039 The government decided to build some cities in a newly developping area. Now they had N different locations to select from, while there were M factories in this newly developping ar

ZOJ 3778 Talented Chef(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5017 As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same time. Tonight he is going to have a hearty dinner with his girlfriend at his home. Of c

ZOJ 3713 In 7-bit(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5016 Very often, especially in programming contests, we treat a sequence of non-whitespace characters as a string. But sometimes, a string may contain whitespace characters or even be em