题目大意:
有几个平台,各有各的高度,每个平台两端各有一个柱子,柱子一直向下延长,直到碰到其他平台或地板。问所有柱子总共多长。
正文:
(n^2) 暴力匹配平台 (i) 的某端有没有被平台 (j) 的两端围住(即:(j_lleq i_l<j_r) 或 (j_l<i_rleq j_r))。
代码:
for (int i = 1; i <= n; i++)
{
int yl, yr;
yl = yr = y[i];
for (int j = 1; j <= n; j++)
{
if(i == j || y[i] <= y[j]) continue;
if(x1[i] >= x1[j] && x1[i] < x2[j])
yl = min(yl, y[i] - y[j]);
if(x2[i] > x1[j] && x2[i] <= x2[j])
yr = min(yr, y[i] - y[j]);
}
ans += yl + yr;
}