USACO07JAN Tallest Cow

题目地址

题解

如果不存在任何大小关系,不妨认为所有牛的高度都达到了最高。存在大小关系,就不妨一步一步满足。
由于最小的单位是1,就让两个牛之间的高度相对两边少1,这样就在满足尽量高的前提下满足了限制条件。这一步可以用区间减法或者差分实现。
需要注意的是可能两个牛的关系会重复出现,此时需要去重。

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
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cctype>
#define INF 2000000000
using namespace std;
typedef long long ll;
int read(){
int f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar(); }
while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
return f * x;
}
int n, id, h, m, hi[10005] = {0};
pair<int, int> p[10005];
void init(){
n = read(), id = read(), h = read(), m = read();
for(int i = 0; i < m; ++i){
p[i].first = read(), p[i].second = read();
if(p[i].first > p[i].second)
swap(p[i].first, p[i].second);
}
sort(p, p + m);
m = unique(p, p + m) - p;
}
void solve(){
for(int i = 0; i < m; ++i){
int l = p[i].first, r = p[i].second;
hi[l + 1]--, hi[r]++;
}
int sum = 0;
for(int i = 1; i <= n; ++i)
sum += hi[i], printf("%d\n", h + sum);
}
int main(){
init();
solve();
return 0;
}