按距离从小到大排序
f[i][j][0或1]表示在i或j还有i-j没有完成
转移
tmp=dp[i][j][0]; tmp=min(tmp,max(dp[i][j+1][1]+a[j+1].dist-a[i].dist,a[i].t)); tmp=min(tmp,max(dp[i-1][j][0]+a[i].dist-a[i-1].dist,a[i].t));
注意边界 比如:dp[0][i],dp[0][c+3]初值应为inf
#include<bits/stdc++.h> using namespace std; const int N=1e3+30,inf=1e6; int dp[N][N][2]; #define ri register int int az1,az2,tmp; int c,b,h; struct re{ int dist; int t; }; re a[N]; bool cmp(re x,re y){return x.dist<y.dist;} //int max(int x,int y){if(x>y)return x;else return y;} //int min(int x,int y){if(x<y)return x;else return y;} int main() { //freopen("p.in","r",stdin); cin>>c>>h>>b; for(int i=1;i<=c;i++) cin>>a[i].dist>>a[i].t; sort(a+1,a+1+c,cmp); for(int i=0;i<N;i++) for(int j=0;j<N;j++)dp[i][j][0]=dp[i][j][1]=inf; int sdr5s; dp[1][c][0]=max(a[1].t,a[1].dist); dp[1][c][1]=max(a[c].t,a[c].dist); for(int i=1;i<=c;i++) for(int j=c;j>=i;j--){ tmp=dp[i][j][0]; tmp=min(tmp,max(dp[i][j+1][1]+a[j+1].dist-a[i].dist,a[i].t)); tmp=min(tmp,max(dp[i-1][j][0]+a[i].dist-a[i-1].dist,a[i].t)); dp[i][j][0]=tmp; tmp=dp[i][j][1]; tmp=min(tmp,max(dp[i][j+1][1]+a[j+1].dist-a[j].dist,a[j].t)); tmp=min(tmp,max(dp[i-1][j][0]+a[j].dist-a[i-1].dist,a[j].t)); dp[i][j][1]=tmp; } az1=1e8; for(int i=1;i<=c;i++)az1=min(az1,(min(dp[i][i][0],dp[i][i][1])+(int)fabs(b-a[i].dist))); cout<<az1; return 0; }