結果

問題 No.15 カタログショッピング
ユーザー jp_stejp_ste
提出日時 2016-05-30 04:10:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 299 ms / 5,000 ms
コード長 5,672 bytes
コンパイル時間 2,939 ms
コンパイル使用メモリ 81,320 KB
実行使用メモリ 67,360 KB
最終ジャッジ日時 2024-10-07 17:56:41
合計ジャッジ時間 4,720 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
50,300 KB
testcase_01 AC 50 ms
50,180 KB
testcase_02 AC 51 ms
50,188 KB
testcase_03 AC 54 ms
50,148 KB
testcase_04 AC 52 ms
50,080 KB
testcase_05 AC 269 ms
66,860 KB
testcase_06 AC 297 ms
67,228 KB
testcase_07 AC 299 ms
67,016 KB
testcase_08 AC 277 ms
67,360 KB
testcase_09 AC 269 ms
67,256 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