結果

問題 No.393 2本の竹
ユーザー KilisameKilisame
提出日時 2016-09-14 14:33:18
言語 Java17
(openjdk 17.0.1)
結果
AC  
実行時間 394 ms / 1,000 ms
コード長 3,038 bytes
コンパイル時間 3,002 ms
使用メモリ 109,144 KB
最終ジャッジ日時 2022-12-17 20:53:27
合計ジャッジ時間 9,098 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 129 ms
38,996 KB
testcase_01 AC 109 ms
37,804 KB
testcase_02 AC 114 ms
37,940 KB
testcase_03 AC 124 ms
39,068 KB
testcase_04 AC 107 ms
37,592 KB
testcase_05 AC 113 ms
38,064 KB
testcase_06 AC 107 ms
37,872 KB
testcase_07 AC 129 ms
39,308 KB
testcase_08 AC 135 ms
40,064 KB
testcase_09 AC 394 ms
109,144 KB
testcase_10 AC 162 ms
59,320 KB
testcase_11 AC 193 ms
62,340 KB
testcase_12 AC 220 ms
67,196 KB
testcase_13 AC 165 ms
54,804 KB
testcase_14 AC 136 ms
45,280 KB
testcase_15 AC 120 ms
51,812 KB
testcase_16 AC 149 ms
63,232 KB
testcase_17 AC 125 ms
39,080 KB
testcase_18 AC 117 ms
38,744 KB
testcase_19 AC 100 ms
37,492 KB
testcase_20 AC 110 ms
38,140 KB
testcase_21 AC 121 ms
39,044 KB
testcase_22 AC 246 ms
74,800 KB
testcase_23 AC 166 ms
62,244 KB
testcase_24 AC 115 ms
38,992 KB
testcase_25 AC 108 ms
38,300 KB
testcase_26 AC 100 ms
37,704 KB
testcase_27 AC 361 ms
102,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class TwoBamboo {

    /* メモ化用Map */
    private static int[][] calculatedArray;

    /* 2本の竹のうち短い方の竹を対象にしてナップサック問題として考える。 */
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int d = cin.nextInt();
        int[][][] allQuestions = new int[d][2][];
        for (int i = 0; i < d; i++) {
            /* 2本の竹情報 */
            allQuestions[i][0] = new int[2];
            allQuestions[i][0][0] = cin.nextInt();
            allQuestions[i][0][1] = cin.nextInt();
            /* 要求数 */
            int requestsNum = cin.nextInt();
            /* 要求情報 */
            allQuestions[i][1] = new int[requestsNum];
            for (int j = 0; j < requestsNum; j++) {
                allQuestions[i][1][j] = cin.nextInt();
            }
        }
        for (int[][] question : allQuestions) {
            System.out.println(answer(question[0], question[1]));
        }
    }

    /* 各データセット単位で解く */
    private static int answer(int[] bamboo, int[] requests) {
        /* 2本の竹の総和に対し、要求された長さを小さい順に足していく。要求された長さの和が2本の竹の総和を越えたら、以降の要求には絶対に応えられないので無視する。また、長いほうの竹を越える要求も満たせないので無視する */
        Arrays.sort(requests);
        Arrays.sort(bamboo);
        calculatedArray = new int[requests.length][bamboo[0] + 1];
        return getNum(0, bamboo[0], bamboo[1], requests);
    }

    private static int getNum(int currentIndex, int shortBamboo, int longBamboo, int[] requests) {
        /* 要求されるのは長さのみで重みはないので、要求を小さい順にソートしたとき、R(n)の要求に応えずにR(n+1)の要求に応えたほうがよい、というケースはない。なので、必ずどちらかの竹から切リ出して次の要求を処理する */
        if (calculatedArray[currentIndex][shortBamboo] != 0) {
            /* 計算済み */
            return calculatedArray[currentIndex][shortBamboo];
        }
        int shortResult = 0;
        if (requests[currentIndex] <= shortBamboo) {
            shortResult++;
            if (currentIndex + 1 < requests.length) {
                shortResult += getNum(currentIndex + 1, shortBamboo - requests[currentIndex], longBamboo, requests);
            }
        }
        int longResult = 0;
        if (requests[currentIndex] <= longBamboo) {
            longResult++;
            if (currentIndex + 1 < requests.length) {
                longResult += getNum(currentIndex + 1, shortBamboo, longBamboo - requests[currentIndex], requests);
            }
        }
        int result = shortResult > longResult ? shortResult : longResult;
        calculatedArray[currentIndex][shortBamboo] = result;
        return result;
    }

}
0