社区讨论
不用线段树、不用扫描线
P1904天际线参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mbw6ozfh
- 此快照首次捕获于
- 2025/06/14 19:59 8 个月前
- 此快照最后确认于
- 2025/06/14 20:38 8 个月前
CPP
#include <bits/stdc++.h>
using namespace std;
/*
分析:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 5 7 9 12 14 16 19 22 23 24 25 28 29
11 11 11
6 6 6
13 13 13
7 7
3 3 3 3 3 3
18
13 13 13 13
4 4
11 11 13 13 13 0 7 7 3 18 3 13 13 13 13 0
ans: 1 11 3 13 9 0 7 16 3 19 18 22 3 23 13 29 0
*/
const int N = 5000 + 5;
int a[N];
int b[N];
int h[N];
int t[2 * N]; // 离散化数组
int maxh[N];
int idd;
int idx;
int main()
{
int x, c, y;
while (scanf("%d%d%d", &x, &c, &y) != EOF) {
a[++idd] = t[++idx] = x;
h[idd] = c;
b[idd] = t[++idx] = y;
}
// 离散化操作
sort(t + 1, t + idx + 1);
int m = unique(t + 1, t + idx + 1) - t - 1;
for (int i = 1; i <= idd; i++) {
a[i] = lower_bound(t + 1, t + m + 1, a[i]) - t;
b[i] = lower_bound(t + 1, t + m + 1, b[i]) - t;
for (int j = a[i]; j < b[i]; j++) {
maxh[j] = max(maxh[j], h[i]);
}
}
// for (int i = 1; i <= idx; i++) {
// printf("%d ", maxh[i]);
// }
// 输出
int now = 1;
while (now <= idx) {
printf("%d %d ", t[now], maxh[now]);
while (now < idx && maxh[now] == maxh[now + 1]) {
now++;
}
if (now <= idx) {
now++;
}
}
return 0;
}
就这些了,新方法,支持一下~
回复
共 1 条回复,欢迎继续交流。
正在加载回复...