P3842 [TJOI2007]线段
很简单的东西
分左右端点讨论
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int n;
int l[20005],r[20005];
int dp[20005][2];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d%d",&l[i],&r[i]);
}
dp[1][0]=r[1]-1+r[1]-l[1];
dp[1][1]=r[1]-1;
for(int i=2;i<=n;++i){
dp[i][0]=min(dp[i-1][0]+1+abs(l[i-1]-r[i]),dp[i-1][1]+1+abs(r[i-1]-r[i]))+r[i]-l[i];
dp[i][1]=min(dp[i-1][1]+1+abs(l[i]-r[i-1]),dp[i-1][0]+1+abs(l[i]-l[i-1]))+r[i]-l[i];
}
printf("%d",min(dp[n][0]+n-l[n],dp[n][1]+n-r[n]));
return 0;
}