結果

問題 No.15 カタログショッピング
ユーザー jp_stejp_ste
提出日時 2016-05-30 04:10:59
言語 Java19
(openjdk 21)
結果
AC  
実行時間 256 ms / 5,000 ms
コード長 5,672 bytes
コンパイル時間 2,657 ms
コンパイル使用メモリ 90,116 KB
実行使用メモリ 68,924 KB
最終ジャッジ日時 2023-07-29 12:06:35
合計ジャッジ時間 4,546 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
49,548 KB
testcase_01 AC 37 ms
49,604 KB
testcase_02 AC 39 ms
49,592 KB
testcase_03 AC 42 ms
50,272 KB
testcase_04 AC 39 ms
49,540 KB
testcase_05 AC 218 ms
68,740 KB
testcase_06 AC 238 ms
68,924 KB
testcase_07 AC 248 ms
68,348 KB
testcase_08 AC 255 ms
68,276 KB
testcase_09 AC 256 ms
68,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Main {
    static FastScanner scan = new FastScanner();
    static PrintWriter out = new PrintWriter(System.out);
    
    static int n;
    static long s;
    static long[] list;
    
    public static void main(String[] args) {
        n = scan.nextInt();
        s = scan.nextLong();
        list = new long[n];
        for(int i=0; i<n; i++) {
            list[i] = scan.nextLong();
        }
        
        ArrayList<Integer> indexList = new ArrayList<Integer>();
        ArrayList<Node> left = new ArrayList<Node>();
        ArrayList<Node> right = new ArrayList<Node>();
        ArrayList<String> outList = new ArrayList<String>();
        
        solve(0, n/2, 0, left, indexList);
        solve(n/2, n, 0, right, indexList);
        Collections.sort(right);
        
        for(int i=0; i<left.size(); i++) {
            int l = 0;
            int r = right.size();
            while(l < r) {
                int m = (l+r)/2;
                long v = left.get(i).v + right.get(m).v;
                if(v == s) {
                    outList.add((left.get(i).add(right.get(m)).toString()));
                    r = m;
                } else if(v > s) {
                    r = m;
                } else if(v < s) {
                    l = m+1;
                }
            }
        }
        
        Collections.sort(outList, new UserComparator());
        for(String str : outList) {
            out.println(str);
        }
        out.flush();
    }
    
    static void solve(int i, int n, long sum, ArrayList<Node> nowList, ArrayList<Integer> indexList) {
        if(i == n) {
            nowList.add(new Node(sum, (ArrayList<Integer>)indexList.clone()));
            return;
        }
        solve(i+1, n, sum, nowList, indexList);
        indexList.add(i);
        solve(i+1, n, sum + list[i], nowList, indexList);
        indexList.remove(Integer.valueOf(i));
    }
    
    static class Node implements Comparable<Node>{
        long v;
        ArrayList<Integer> indexes;
        Node(long v, ArrayList<Integer> indexes) {
            this.v = v;
            this.indexes = indexes;
        }
        @Override
        public int compareTo(Node o) {
            return (int)(v - o.v);
        }
        public Node add(Node o) {
            ArrayList<Integer> ret = new ArrayList<Integer>();
            ret.addAll(indexes);
            ret.addAll(o.indexes);
            return new Node(v, ret);
        }
        
        @Override
        public String toString() {
            StringBuffer sb = new StringBuffer();
            for(int i=0; i<indexes.size(); i++) {
                if(i>0) sb.append(" ");
                sb.append(indexes.get(i)+1);
            }
            return sb.toString();
        }
    }
    
    final static class UserComparator implements Comparator {
        public int compare(Object o1, Object o2) {
            String values1[] = ((String)o1).split(" ");
            String values2[] = ((String)o2).split(" ");
            
            int i=0;
            while(true) {
                long v1 = Long.parseLong(values1[i]);
                long v2 = Long.parseLong(values2[i]);
                if(v1 == v2) {
                    i++;
                    continue;
                }
                return (int)(v1 - v2);
            }
        }
    }
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        } else {
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }

    private int readByte() {
        if (hasNextByte())
            return buffer[ptr++];
        else
            return -1;
    }

    private static boolean isPrintableChar(int c) {
        return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr]))
            ptr++;
    }

    public boolean hasNext() {
        skipUnprintable();
        return hasNextByte();
    }

    public String next() {
        if (!hasNext())
            throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext())
            throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while (true) {
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            } else if (b == -1 || !isPrintableChar(b)) {
                return minus ? -n : n;
            } else {
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    
    public int nextInt() {
        return (int)nextLong();
    }
}
0