用二维数组和一维数组记录状态的差别在哪里?

请问:
我使用二维数组记录所有前面计算过的状态,然后输出最后一次状态的值;
和只使用一维数组记录每次计算后的状态,最终状态也就保存在这个数组里。
为什么使用二维数组的代码,会有3个WA,
而使用一维数组的代码,则是AC。

用一维数组(statesOfEachProcess[1])记录状态

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main {
    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer st = new StreamTokenizer(br);

        st.nextToken();
        int totalMoney = (int) st.nval / 10;
        st.nextToken();
        int numOfItem = (int) st.nval;

        int value[] = new int[numOfItem + 1];
        int price[] = new int[numOfItem + 1];
        int father[] = new int[numOfItem + 1];
        int left[] = new int[numOfItem + 1];
        int right[] = new int[numOfItem + 1];

        for (int i = 1; i <= numOfItem; i++) {
            st.nextToken();
            price[i] = (int) st.nval / 10;
            st.nextToken();
            value[i] = (int) st.nval * price[i];
            st.nextToken();
            father[i] = (int) st.nval;

            if (father[i] > 0) {
                if (left[father[i]] > 0) {
                    right[father[i]] = i;
                } else {
                    left[father[i]] = i;
                }
            }
        }

        int currGroup = 1;
        int[][] statesOfEachProcess = new int[numOfItem + 1][totalMoney + 1];
        for (int i = 1; i <= numOfItem; i++) {
            if (father[i] != 0) {
                continue;
            }
            for (int j = totalMoney; j >= 0; j--) {
                statesOfEachProcess[currGroup][j] = statesOfEachProcess[currGroup][j];
                if (j >= price[i])
                    statesOfEachProcess[currGroup][j] = Math.max(statesOfEachProcess[currGroup][j],
                            statesOfEachProcess[currGroup][j - price[i]] + value[i]);
                if (left[i] > 0 && j >= price[i] + price[left[i]])
                    statesOfEachProcess[currGroup][j] = Math.max(statesOfEachProcess[currGroup][j],
                            statesOfEachProcess[currGroup][j - price[i] - price[left[i]]] + value[i] + value[left[i]]);
                if (right[i] > 0 && j >= price[i] + price[right[i]])
                    statesOfEachProcess[currGroup][j] = Math.max(statesOfEachProcess[currGroup][j],
                            statesOfEachProcess[currGroup][j - price[i] - price[right[i]]] + value[i] + value[right[i]]);
                if (left[i] > 0 && right[i] > 0 && j >= price[i] + price[right[i]] + price[left[i]])
                    statesOfEachProcess[currGroup][j] = Math.max(statesOfEachProcess[currGroup][j],
                            statesOfEachProcess[currGroup][j - price[i] - price[right[i]] - price[left[i]]] + value[i] + value[right[i]] + value[left[i]]);
            }
            // currGroup++;
        }
        System.out.println(statesOfEachProcess[currGroup][totalMoney] * 10);
    }
}

用二维数组(statesOfEachProcess)记录状态

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main {
    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer st = new StreamTokenizer(br);

        st.nextToken();
        int totalMoney = (int) st.nval / 10;
        st.nextToken();
        int numOfItem = (int) st.nval;

        int value[] = new int[numOfItem + 1];
        int price[] = new int[numOfItem + 1];
        int father[] = new int[numOfItem + 1];
        int left[] = new int[numOfItem + 1];
        int right[] = new int[numOfItem + 1];

        for (int i = 1; i <= numOfItem; i++) {
            st.nextToken();
            price[i] = (int) st.nval / 10;
            st.nextToken();
            value[i] = (int) st.nval * price[i];
            st.nextToken();
            father[i] = (int) st.nval;

            if (father[i] > 0) {
                if (left[father[i]] > 0) {
                    right[father[i]] = i;
                } else {
                    left[father[i]] = i;
                }
            }
        }

        int currGroup = 1;
        int[][] statesOfEachProcess = new int[numOfItem + 1][totalMoney + 1];
        for (int i = 1; i <= numOfItem; i++) {
            if (father[i] != 0) {
                continue;
            }
            for (int j = totalMoney; j >= 0; j--) {
                statesOfEachProcess[currGroup][j] = statesOfEachProcess[currGroup - 1][j];
                if (j >= price[i])
                    statesOfEachProcess[currGroup][j] = Math.max(statesOfEachProcess[currGroup - 1][j],
                            statesOfEachProcess[currGroup - 1][j - price[i]] + value[i]);
                if (left[i] > 0 && j >= price[i] + price[left[i]])
                    statesOfEachProcess[currGroup][j] = Math.max(statesOfEachProcess[currGroup - 1][j],
                            statesOfEachProcess[currGroup - 1][j - price[i] - price[left[i]]] + value[i] + value[left[i]]);
                if (right[i] > 0 && j >= price[i] + price[right[i]])
                    statesOfEachProcess[currGroup][j] = Math.max(statesOfEachProcess[currGroup - 1][j],
                            statesOfEachProcess[currGroup - 1][j - price[i] - price[right[i]]] + value[i] + value[right[i]]);
                if (left[i] > 0 && right[i] > 0 && j >= price[i] + price[right[i]] + price[left[i]])
                    statesOfEachProcess[currGroup][j] = Math.max(statesOfEachProcess[currGroup - 1][j],
                            statesOfEachProcess[currGroup - 1][j - price[i] - price[right[i]] - price[left[i]]] + value[i] + value[right[i]] + value[left[i]]);
            }
            currGroup++;
        }
        System.out.println(statesOfEachProcess[currGroup - 1][totalMoney] * 10);
    }
}

0 条评论

目前还没有评论...

信息

ID
1313
难度
6
分类
动态规划 | 背包 点击显示
标签
递交数
8324
已通过
2464
通过率
30%
被复制
20
上传者