标签:
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int n,x,y,maxh;
struct Plat{
int l,r,h;
bool operator < (const Plat & a)const{
return h < a.h;
}
Plat(int _l = 0,int _r = 0,int _h = 0):l(_l),r(_r),h(_h){}
int canFallOn(int _x,int _h){
return _x >= l && _x <= r && _h >= h && (_h - h) <= maxh;
}
}a[1000 + 10];
int dp[1000 + 10][2];
void toleft(int i){
for(int k = i - 1;k > 0;--k){
if(a[k].canFallOn(a[i].l,a[i].h)){
dp[i][0] = a[i].h - a[k].h + min (a[i].l - a[k].l + dp[k][0], a[k].r - a[i].l + dp[k][1]);
return ;
}
}
if(a[i].h - a[0].h > maxh)
dp[i][0] = 0x7f7f7f7f;
else dp[i][0] = a[i].h;
}
void toright(int i){
for(int k = i - 1;k > 0;--k){
if(a[k].canFallOn(a[i].r,a[i].h)){
dp[i][1] = a[i].h - a[k].h + min (a[i].r - a[k].l + dp[k][0], a[k].r - a[i].r + dp[k][1]);
return ;
}
}
if(a[i].h - a[0].h > maxh)
dp[i][1] = 0x7f7f7f7f;
else dp[i][1] = a[i].h;
}
void dfs(){
Plat tmp(x,x,y);
a[0] = tmp;
a[n + 1] = Plat(-20010,20010,0);
sort(a,a + n + 2);
for(int i = 1 ;i <= n + 1 ;++i){
toleft(i);
toright(i);
}
printf("%d\n",min(dp[n + 1][0],dp[n + 1][1]));
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d",&n,&x,&y,&maxh);
for(int i = 1 ; i <= n ; ++i){
scanf("%d%d%d",&a[i].l ,&a[i].r,&a[i].h );
}
dfs();
}
return 0;
}
[2016-03-29][POJ][1661][]Help Jimmy]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/d927499ad00e3274749daa7aefe5fdb2.html