正题
luogu 2003
题目大意
在平面上有若干块板子,每块板子的左右端分别向下连一条柱子,连到第一块板子,问你共要多少长度的柱子
解题思路
枚举一个板子中间的柱子(即对这些柱子可能有贡献)
然后取一个最高的立即可
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define N 111
using namespace std;
int n, y, w, x1, x2, anss, ans[N<<1];
struct node
{int v, x, y, nx;
}a[N<<1];
bool cmp(node a, node b)
{return a.x < b.x || a.x == b.x && a.y < b.y;
}
int main()
{scanf("%d", &n);for (int i = 1; i <= n; ++i){scanf("%d%d%d", &y, &x1, &x2);x2--;a[++w] = (node){i, x1, y, x2};a[++w] = (node){i, x2, y, 0};}sort(a + 1, a + 1 + w, cmp);for (int i = 1; i <= w; ++i)if (a[i].nx){int k = i + 1;while(a[k].x <= a[i].nx && k <= w)//在该板子内{if (a[k].y > a[i].y) ans[k] = max(ans[k], a[i].y);//高于该板子k++;}}for (int i = 1; i <= w; ++i)anss += a[i].y - ans[i];printf("%d", anss);return 0;
}