dp[i][0] 状态转移方程:
1. 从上一个竹竿的底部转移过来,即:
dp[i][0]=dp[i−1][0]+x[i]−x[i−1];
2. 从上一个竹竿的传送门转移过来,即:
dp[i][0]=dp[i−1][1]+b[i]/1.3;
dp[i][1] 状态转移方程:
1. 从上一个竹竿的底部转移过来,即:
dp[i][1]=dp[i−1][0]+x[i]−x[i−1]+a[i]/0.7;
2.从上一个竹杠传送门过来:
需要判断上一个竹杠传送门过来后是在当前竹杠传送门上分还是下方,在当前竹杠传送门上分就需要向下移动,否则反之。
a[i]>b[i]
dp[i][1] = Math.min(dp[i-1][0] + x[i]-x[i-1] + a[i]/0.7, dp[i-1][1] + (a[i]-b[i])/0.7);
b[i]>=a[i]
dp[i][1] = Math.min(dp[i-1][0] + x[i]-x[i-1] + a[i]/0.7,dp[i-1][1] + (b[i]-a[i]) /1.3);
注意:
数组a的下标是从一开始的,而数组b的下标是从二开始的。
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] x = new int[n+1];
int[] a = new int[n+1];
int[] b = new int[n+1];
for(int i = 1;i<=n;i++){
x[i] = sc.nextInt();
}
for(int i = 1;i<n;i++){
a[i] = sc.nextInt();
b[i+1] = sc.nextInt();
}
double[][] dp = new double[n+1][2];
dp[1][0] = x[1];
dp[1][1] = x[1] + a[1]/0.7;
for(int i = 2;i <= n;i++){
if(a[i]>b[i]){
dp[i][1] = Math.min(dp[i-1][0] + x[i]-x[i-1] + a[i]/0.7, dp[i-1][1] + (a[i]-b[i])/0.7);
}else {
dp[i][1] = Math.min(dp[i-1][0] + x[i]-x[i-1] + a[i]/0.7,dp[i-1][1] + (b[i]-a[i]) /1.3);
}
dp[i][0] = Math.min(dp[i-1][1] +b[i]/1.3 ,dp[i-1][0]+x[i]-x[i-1]);
}
System.out.printf("%.2f",dp[n][0]);
}
}