poj 3190 stall reservations 题解

POJ 3190 Stall Reservations

思路:

优先选择进食最早的奶牛,晚来的奶牛如果进食时间和前一只奶牛重叠,就放到一个新栏里,否则的话就放在当前栏里。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include <algorithm>
#include <stdio.h>

using namespace std;

int n;
struct {
int first;
int second;
int index;
bool operator < (const cow &c) {
return first < c.first;
}
};
cow invl[50000];
pair<int, int> stall[50001];
int assigned[50000];

void solve() {
int res = 1;
sort(invl, invl + n);
for (int i = 0; i < n; i++) {
bool hasFound = 0;
int s = invl[i].first;
int t = invl[i].second;
int cowIndex = invl[i].index;

for (int j = 1; j <= res; j++) {
if (s > stall[j].second) {
stall[j].second = t;
hasFound = 1;
assigned[cowIndex] = j;
break;
}
}

if (hasFound == 0) {
res++;
stall[res].first = s;
stall[res].second = t;
assigned[cowIndex] = res;
}
}
printf("%dn", res);
for (int i = 0; i < n; i++) {
printf("%dn", assigned[i]);
}
}

int main() {
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d", &invl[i].first);
scanf("%d", &invl[i].second);
invl[i].index = i;
}
solve();
return 0;
}