例题:2023年第十四届蓝桥杯Java软件开发B组E题 蜗牛
参考解答:
参考代码示例:
import java.util.Scanner;
public class Main {
static int N = 100010;
static int[] arr = new int[N];
static int[] a = new int[N]; //传送带的起始坐标
static int[] b = new int[N]; //第i-1根杆子的传送带的坐标
static double[][] dp = new double[N][2];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(int i=1;i<=n;i++) {
arr[i] = scan.nextInt();
}
for(int i=1;i<n;i++) {
a[i] = scan.nextInt();
b[i] = scan.nextInt();
}
dp[1][0] = arr[1];
dp[1][1] = arr[1]+a[1]/0.7;
for(int i=2;i<=n;i++) {
dp[i][0] = Math.min(dp[i-1][0]+arr[i]-arr[i-1], dp[i-1][1]+b[i-1]/1.3);
if(b[i-1]>=a[i]) {
dp[i][1]=Math.min(dp[i][0]+a[i]/0.7,dp[i-1][1]+(b[i-1]-a[i])/1.3 );
}else {
dp[i][1] = Math.min(dp[i][0]+a[i]/0.7,dp[i-1][1]+(a[i]-b[i-1])/0.7);
}
}
System.out.printf("%.2f",dp[n][0]);
scan.close();
}
}