| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
第一次发帖,真诚求教大牛帮忙看一下我的程序,很简单的dp,老是是WA1661,应该是很简单的dp,从高度递增的递推,更新每个平台左右端点下落需要的时间。下面是小弟的代码,始终WA,找不到原因,还请各位大牛花几分钟看一下,不甚感激~
#include <iostream>
#include <cstdlib>
#include <limits>
using namespace std;
const int MAX_N = 1002;
typedef struct platform {
int x[2]; //x[0]:左端点坐标,x[1]:右端点坐标
int h;
platform& operator = ( platform &rhs ) {
x[0] = rhs.x[0];
x[1] = rhs.x[1];
h = rhs.h;
return *this;
}
} platform_t;
int comp( const void *a ,const void *b ) {
platform_t * p1 = (platform*) a;
platform_t * p2 = (platform*) b;
return p2->h - p1->h;
}
platform_t plat[MAX_N];
int d[MAX_N][2];
int main(void)
{
int t, MAX, X, Y, N;
int i, j, dir, nleft, nright;
const int INF = numeric_limits<int>::max();
cin >> t;
while ( t-- ) {
cin >> N >> X >> Y >> MAX;
//起点,相当于x[0]==x[1]的plat
plat[0].x[0] = plat[0].x[1] = X;
plat[0].h = Y;
for ( i = 1; i <= N; i++ )
cin >> plat[i].x[0] >> plat[i].x[1] >> plat[i].h;
//由高度递减顺序排列plat
qsort( plat, N+1, sizeof(platform_t), comp );
//d[i][0]表示从左端点出发还需要多长时间着地
//d[i][1]表示从右端点出发还需要多长时间着地
//-1表示不能着地
for ( i = 0; i < N; i++ ) d[i][0] = d[i][1] = -1;
//高度最小的必然能着地,且时间为其高度
d[N][0] = d[N][1] = plat[N].h;
for ( i = N-1; i >= 0; i-- ) {
for ( dir = 0; dir <= 1; dir++ ) { //左右两个方向更新d
//找到从i的dir方向落下的plat
for ( j = i+1; j <= N; j++ )
if ( plat[j].x[0] <= plat[i].x[dir] && plat[j].x[1] >= plat[i].x[dir] ) break;
if ( j > N ) {
//从i的dir端点没有plat可以落,如果小于MAX,可以着地,否则不能
if ( plat[i].h <= MAX ) d[i][dir] = plat[i].h;
} else {
nleft = nright = INF;
//从j左侧可以着地
if ( d[j][0] != -1 ) nleft = d[j][0] + plat[i].x[dir] - plat[j].x[0];
//从j右侧可以着地
if ( d[j][1] != -1 ) nright = d[j][1] + plat[j].x[1] - plat[i].x[dir];
//如果左右都不能,跳过
if ( nleft == INF && nright == INF ) continue;
//选择较小的一端
if ( nleft < nright ) d[i][dir] = nleft;
else d[i][dir] = nright;
//加上之间的高度差
d[i][dir] += plat[i].h - plat[j].h;
}
}
}
cout << d[0][0] << endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator