2018 codeM 初赛A卷 第二题 棋盘题 -java


import java.util.Scanner;
/**
 * Created by 90684 on 2018/6/10.
 */
// codeM 初赛A卷 第二题
    /*
    贪心算法 该题。start[i]  end[j]  对于 i<=j 代价为j-i;否则为j+i;
    所以找到最大的可以匹配的i,就相当于减少了2i的花费。
     */
public class codeM初赛_02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] start = new int[n];
        int[] end = new int[n];
        for (int i = 0; i < n; i++) {
            start[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
            end[i] = sc.nextInt();
        }
        int aft = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            ans += (start[i] + end[i]) * i;
        }                     
        for (int i = n - 1; i >= 0; i--) {
            aft += start[i];
            if (aft >= end[i]) {
                ans -= 2 * i * end[i];
                aft -= end[i];
            }
        }
        System.out.println(ans);
    }
}

只是思路哈 

你可能感兴趣的:(算法)