Description
Alice要搭建平台,平台不能漂在空气中,必须要有两根柱子支撑,具体地说,每个平台的两端必须由一根柱子支撑,柱子的另一端在地板或另一个平台上。
给你平台的放置位置,每个平台的位置由它的高度(离地面的垂直距离)和水平方向两个端点的坐标决定,每根柱子必须安放在离端点0.5个单位的位置,
编程计算所需柱子总长是多少。
Input
第一行包含整数N,1<=N<=100,表示平台的数量。
接下来N行,每行三个数Y,X1和X2用来描述每个平台的位置,Y表示高度,X1,X2表示两个端点的X坐标,三个数都是正整数,并且小于等于10000,同时满足X2>X1+1(也就是说平台的长度至少为2)。
输入保证平台不会重叠。
Output
输出柱子的总长度。
Sample Input
输入1:
3
1 5 10
3 1 5
5 3 7
输入2:
5
50 50 90
40 40 80
30 30 70
20 20 60
10 10 50
Sample Output
输出1:
14
输出2:
200
做法:模拟
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#define rep(i, a, b) for (int i=a;i<=b;i++)
#define dep(i, a, b) for (int i=a;i>=b;i--)
#define N 1007
using namespace std;
int n;
struct arr{
int x, y, h;
}f[N];
int ans = 0;
bool t1, t2;
bool cmp(arr a, arr b){ return a.h < b.h; }
int judge(int a, int b){
int sum = 0;
if (!t1){
if (f[b].y >= (double)f[a].y - 0.5 && f[b].x <= (double)f[a].y - 0.5){
t1 = 1;
sum += f[a].h - f[b].h;
}
}
if (!t2){
if (f[b].x <= (double)f[a].x + 0.5 && f[b].y >= (double)f[a].x + 0.5){
t2 = 1;
sum += f[a].h - f[b].h;
}
}
return sum;
}
int main(){
scanf("%d", &n);
rep(i, 1, n)
scanf("%d%d%d", &f[i].h, &f[i].x, &f[i].y);
sort(f + 1, f + n + 1, cmp);
ans += f[1].h * 2;
rep(i, 2, n){
t1 = 0, t2 = 0;
dep(j, i - 1, 1){
if (!t1 || !t2) ans += judge(i, j);
}
if (!t1) ans += f[i].h;
if (!t2) ans += f[i].h;
}
printf("%d", ans);
}