結果

問題 No.15 カタログショッピング
ユーザー yuki2006yuki2006
提出日時 2015-02-09 16:04:51
言語 Java19
(openjdk 21)
結果
AC  
実行時間 322 ms / 5,000 ms
コード長 3,959 bytes
コンパイル時間 4,816 ms
コンパイル使用メモリ 88,804 KB
実行使用メモリ 64,340 KB
最終ジャッジ日時 2023-09-05 20:59:55
合計ジャッジ時間 6,791 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,836 KB
testcase_01 AC 123 ms
56,004 KB
testcase_02 AC 128 ms
55,776 KB
testcase_03 AC 130 ms
55,652 KB
testcase_04 AC 125 ms
56,348 KB
testcase_05 AC 289 ms
62,736 KB
testcase_06 AC 313 ms
63,524 KB
testcase_07 AC 322 ms
64,340 KB
testcase_08 AC 304 ms
63,204 KB
testcase_09 AC 298 ms
63,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Yuki015 {
    public static void main(String[] args) {
        Yuki015 p = new Yuki015();

    }

    public class Ans extends ArrayList<Integer> implements Comparable<ArrayList<Integer>> {

        @Override
        public int compareTo(ArrayList<Integer> o) {

            for (int i = 0; i < Math.min(this.size(), o.size()); i++) {
                int comp = Integer.compare(get(i), o.get(i));
                if (comp != 0) {
                    return comp;
                }
            }

            return Integer.compare(size(), o.size());
        }

        @Override
        public String toString() {
            StringBuilder ans = new StringBuilder();
            for (int i = 0; i < size(); i++) {
                if (ans.length() > 0) {
                    ans.append(" ");
                }
                ans.append(get(i));
            }
            return ans.toString();
        }
    }

    public class HanbunTuple implements Comparable<HanbunTuple> {
        public long value;
        public long bit;

        HanbunTuple(long value, long bit) {
            this.value = value;
            this.bit = bit;
        }

        @Override
        public int compareTo(HanbunTuple o) {
            return Long.compare(this.value, o.value);
        }
    }

    public class Tuple<S, T> {
        S a;
        T b;

        public Tuple(S a, T b) {
            this.a = a;
            this.b = b;

        }
    }


    public Yuki015() {
        Scanner scanner = new Scanner(System.in);
        final int N = scanner.nextInt();
        final int S = scanner.nextInt();

        int[] P = new int[N];
        for (int i = 0; i < N; i++) {
            P[i] = scanner.nextInt();
        }

        int sizeL = N / 2;
        ArrayList<HanbunTuple> leftList = new ArrayList<HanbunTuple>(2 << sizeL);
        ArrayList<HanbunTuple> rightList = new ArrayList<HanbunTuple>(2 << sizeL);

        leftList.add(new HanbunTuple(0, 0));
        for (int i = 0; i < sizeL; i++) {
            int size = leftList.size();
            for (int j = 0; j < size; j++) {
                HanbunTuple tuple = leftList.get(j);
                leftList.add(new HanbunTuple(tuple.value + P[i], tuple.bit | 1 << i));
            }
        }
        rightList.add(new HanbunTuple(0, 0));

        for (int i = sizeL; i < N; i++) {
            int size = rightList.size();
            for (int j = 0; j < size; j++) {
                HanbunTuple tuple = rightList.get(j);
                rightList.add(new HanbunTuple(tuple.value + P[i], tuple.bit | 1 << i));
            }
        }

        Collections.sort(rightList);

        ArrayList<Ans> anses = new ArrayList<>();

        for (HanbunTuple tuple : leftList) {
            final int index = Collections.binarySearch(rightList, tuple, new Comparator<HanbunTuple>() {
                @Override
                public int compare(HanbunTuple o1, HanbunTuple o2) {
                    return Long.compare(o1.value, S - o2.value);
                }
            });
            if (index < 0) {
                continue;
            }

            for (int rightIndex = index; rightIndex < rightList.size(); rightIndex++) {
                if (rightList.get(rightIndex).value + tuple.value != S) {
                    break;
                }
                Ans list = new Ans();

                for (long bitIndex = 0; bitIndex < N; bitIndex++) {

                    if ((rightList.get(rightIndex).bit + tuple.bit & 1 << bitIndex) > 0) {
                        //インデックスが1ずれるため
                        list.add((int) bitIndex + 1);
                    }
                }
                anses.add(list);

            }
        }
        Collections.sort(anses);

        for (Ans ans : anses) {
            System.out.println(ans);
        }

    }

}
0