Optimal allocation of resources

Given a group of users, each user has 3 choices A, B, and C, but the values ​​are different, and adjacent users cannot choose the same one, and the total value after all users' choices is the largest.

输入
3
30 8 4
50 20 9
11 7 6
输出
65(选8 50 7)

There are two ways of thinking about this question: first, find the most valuable users; second, find the most valuable column first.

1. Find the most valuable users

There are many sorting algorithms for sorting user ids according to user value needs. Here is one for reference.

    /**
     * 排序并返回对应原始数组的下标
     *
     * @param input 输入
     * @param desc 是否降序
     * @return 对应原始数组的下标
     */
    public static int[] arraySort(double[] input, boolean desc) {
        double temp;
        int index;
        int k = input.length;
        int[] Index = new int[k];
        for (int i = 0; i < k; i++) {
            Index[i] = i;
        }

        for (int i = 0; i < input.length; i++) {
            for (int j = 0; j < input.length - i - 1; j++) {
                if (desc) {
                    if (input[j] < input[j + 1]) {
     

Guess you like

Origin blog.csdn.net/dragon_T1985/article/details/114821159