蜗牛(基础动态规划)
1 import java.util.Scanner; 2 // 1:无需package 3 // 2: 类名必须Main, 不可修改 4 5 public class Main { 6 public static void main(String[] args) { 7 Scanner sc = new Scanner(System.in); 8 int n = sc.nextInt(); 9 int[] x = new int[n+1]; 10 int[] a = new int[n+1]; 11 int[] b = new int[n+1]; 12 for(int i = 1;i<=n;i++){ 13 x[i] = sc.nextInt(); 14 } 15 for(int i = 1;i<n;i++){ 16 a[i] = sc.nextInt(); 17 b[i+1] = sc.nextInt(); 18 } 19 20 double[][] dp = new double[n+1][2]; 21 dp[1][0] = x[1]; 22 dp[1][1] = x[1] + a[1]/0.7; 23 24 25 for(int i = 2;i <= n;i++){ 26 if(a[i]>b[i]){ 27 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); 28 }else { 29 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); 30 } 31 dp[i][0] = Math.min(dp[i-1][1] +b[i]/1.3 ,dp[i-1][0]+x[i]-x[i-1]); 32 33 } 34 35 36 System.out.printf("%.2f",dp[n][0]); 37 //在此输入您的代码... 38 sc.close(); 39 } 40 }