結果

問題 No.1279 Array Battle
ユーザー neko_the_shadowneko_the_shadow
提出日時 2020-11-15 18:57:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 740 ms / 2,000 ms
コード長 7,692 bytes
コンパイル時間 2,983 ms
コンパイル使用メモリ 88,160 KB
実行使用メモリ 63,752 KB
最終ジャッジ日時 2023-09-30 07:04:41
合計ジャッジ時間 8,430 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
51,136 KB
testcase_01 AC 60 ms
51,244 KB
testcase_02 AC 68 ms
51,328 KB
testcase_03 AC 58 ms
51,312 KB
testcase_04 AC 58 ms
51,228 KB
testcase_05 AC 58 ms
51,136 KB
testcase_06 AC 58 ms
51,128 KB
testcase_07 AC 59 ms
51,136 KB
testcase_08 AC 59 ms
51,088 KB
testcase_09 AC 63 ms
51,208 KB
testcase_10 AC 80 ms
51,112 KB
testcase_11 AC 119 ms
53,832 KB
testcase_12 AC 164 ms
57,004 KB
testcase_13 AC 345 ms
63,752 KB
testcase_14 AC 340 ms
63,516 KB
testcase_15 AC 583 ms
61,956 KB
testcase_16 AC 597 ms
62,488 KB
testcase_17 AC 689 ms
63,132 KB
testcase_18 AC 700 ms
63,596 KB
testcase_19 AC 740 ms
62,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder._1279;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.lang.reflect.Array;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class Main {
    public void exec() {
        int n = stdin.nextInt();
        int[] a = stdin.nextIntArray(n);
        int[] b = stdin.nextIntArray(n);

        int ans = 0;
        Map<Integer, Integer> count = new HashMap<>();
        for (List<Integer> c : new Permutations<>(Arrays.stream(a).boxed().collect(Collectors.toList()))) {
            int sum = 0;
            for (int i = 0; i < n; i++) {
                int diff = c.get(i) -  b[i];
                if (diff >= 0) sum += diff;
            }
            count.put(sum, count.getOrDefault(sum, 0)+1);
        }

        int max = Collections.max(count.keySet());
        stdout.println(count.get(max));
    }

    public  class Permutations<T> implements Iterable<List<T>>{
        private List<T> list;
        public Permutations(List<T> list) {
            this.list = list;
        }

        @Override
        public Iterator<List<T>> iterator() {
            return new PermutationIterator<>(list);
        }

        private class PermutationIterator<T> implements Iterator<List<T>> {
            private int n;
            private List<T> list;
            private Deque<LinkedHashSet<Integer>> stack;
            private List<T> ptr;

            public PermutationIterator(List<T> list) {
                this.n = list.size();
                this.list = list;
                this.stack = new ArrayDeque<>();
                for (int i = 0; i < n; i++) {
                    LinkedHashSet<Integer> set = new LinkedHashSet<>();
                    set.add(i);
                    stack.add(set);
                }
            }

            @Override
            public boolean hasNext() {
                while (!stack.isEmpty()) {
                    LinkedHashSet<Integer> set = stack.removeLast();
                    if (set.size() == n) {
                        this.ptr = set.stream().map(i -> list.get(i)).collect(Collectors.toList());
                        return true;
                    } else {
                        for (int i = 0; i < n; i++) {
                            if (set.contains(i)) continue;
                            LinkedHashSet<Integer> nextSet = new LinkedHashSet<>();
                            nextSet.addAll(set);
                            nextSet.add(i);
                            stack.addLast(nextSet);
                        }
                    }
                }
                return false;
            }

            @Override
            public List<T> next() {
                return this.ptr;
            }
        }
    }


    private static final Stdin stdin = new Stdin(System.in);
    private static final Stdout stdout = new Stdout(System.out);
    private static final Stderr stderr = new Stderr(System.err, false);

    public static void main(String[] args) {
        try {
            new Main().exec();
        } finally {
            stdout.flush();
        }
    }

    // ASCII ONLY
    public static class Stdin {
        private InputStream in;
        private byte[] buf;
        private int ptr;
        private int len;

        public Stdin(InputStream in) {
            this.in = in;
            this.buf = new byte[1024];
            this.ptr = 0;
            this.len = 0;
        }

        public String nextString() {
            StringBuilder sb = new StringBuilder();
            byte b;
            while ((b = read()) != -1) {
                sb.appendCodePoint(b);
            }
            return sb.toString();
        }

        public int nextInt() {
            return (int)nextLong();
        }

        public double nextDouble() {
            return Double.parseDouble(nextString());
        }

        public long nextLong() {
            boolean negative = false;
            int x = 0;

            byte b = read();
            if (b == '-') {
                negative = true;
            } else {
                x += b-'0';
            }

            while ((b=read()) != -1) {
                x *= 10;
                x += b-'0';
            }

            return negative ? -x : x;
        }

        private byte read() {
            byte b = readByte();
            if (b == '\r') {
                readByte(); // LFを読み飛ばす
                return -1;
            } else if (b == '\n' || b == ' ') {
                return -1;
            } else {
                return b;
            }
        }

        private byte readByte(){
            if (len == ptr) {
                try {
                    ptr = 0;
                    len = in.read(buf);
                    if (len == -1) return -1;
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
            return buf[ptr++];
        }

        public String[] nextStringArray(int n) {
            String[] a = new String[n];
            for (int i = 0; i < n; i++) a[i] = nextString();
            return a;
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        public double[] nextDoubleArray(int n) {
            double[] a = new double[n];
            for (int i = 0; i < n; i++) a[i] = nextDouble();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) a[i] = nextLong();
            return a;
        }
    }

    public static class Stdout {
        private PrintWriter stdout;

        public Stdout(PrintStream stdout) {
            this.stdout =  new PrintWriter(stdout, false);
        }

        public void println(Object ... objs) {
            for (int i = 0, len = objs.length; i < len; i++) {
                stdout.print(objs[i]);
                if (i != len-1) stdout.print(' ');
            }
            stdout.println();
        }

        public void flush() {
            stdout.flush();
        }
    }

    public static class Stderr {
        private PrintWriter stderr;
        private boolean debug;

        public Stderr(PrintStream stderr, boolean debug) {
            this.stderr =  new PrintWriter(stderr, false);
            this.debug = debug;
        }

        public void println(Object ... objs) {
            if (!debug) return ;

            stderr.print("DEBUG: ");
            for (int i = 0, len = objs.length; i < len; i++) {
                stderr.print(deepToString(objs[i]));
                if (i != len-1) stderr.print(' ');
            }
            stderr.println();
            stderr.flush();
        }

        private String deepToString(Object o) {
            if (o == null) {
                return "null";
            }

            // 配列の場合
            if (o.getClass().isArray()) {
                int len = Array.getLength(o);
                String[] tokens = new String[len];
                for (int i = 0; i < len; i++) {
                    tokens[i] = deepToString(Array.get(o, i));
                }
                return "{" + String.join(", ", tokens) + "}";
            }

            return Objects.toString(o);
        }
    }
}
0