模拟水题,按check in的日期排下序然后依次遍历处理下就over了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Renter
{
int index;
int in;
int out;
};
int cmp3317(const void *p1, const void *p2)
{
struct Renter *r1 = (struct Renter *) p1;
struct Renter *r2 = (struct Renter *) p2;
return r1->in - r2->in;
}
int main()
{
int n, m, room[100], ans[100];
while (scanf("%d %d", &n, &m), n && m)
{
struct Renter *array = malloc(n * sizeof(struct Renter));
int i, j;
for (i = 0; i < n; i++)
{
array[i].index = i;
scanf("%d %d", &(array[i].in), &(array[i].out));
}
qsort(array, n, sizeof(struct Renter), cmp3317);
memset(room, 0, 100 * sizeof(int));
for (i = 0; i < n; i++)
{
int in = array[i].in;
int index = array[i].index;
for (j = 0; j < m; j++)
if (room[j] <= in)
{
room[j] = array[i].out;
ans[index] = j + 1;
break;
}
if (j == m)
ans[index] = 0;
}
for (i = 0; i < n; i++)
printf("%d\n", ans[i]);
free(array);
}
return 0;
}