結果

問題 No.5004 Room Assignment
ユーザー EvbCFfp1XBEvbCFfp1XB
提出日時 2021-12-05 19:29:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 966 ms / 5,000 ms
コード長 7,544 bytes
コンパイル時間 4,459 ms
実行使用メモリ 238,532 KB
スコア 138,988,706
平均クエリ数 7637.91
最終ジャッジ日時 2021-12-05 19:31:36
合計ジャッジ時間 101,694 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 897 ms
236,896 KB
testcase_01 AC 871 ms
236,376 KB
testcase_02 AC 877 ms
236,716 KB
testcase_03 AC 914 ms
236,932 KB
testcase_04 AC 865 ms
238,392 KB
testcase_05 AC 875 ms
234,672 KB
testcase_06 AC 871 ms
237,364 KB
testcase_07 AC 879 ms
237,768 KB
testcase_08 AC 904 ms
238,312 KB
testcase_09 AC 866 ms
237,176 KB
testcase_10 AC 906 ms
236,336 KB
testcase_11 AC 871 ms
238,108 KB
testcase_12 AC 894 ms
237,172 KB
testcase_13 AC 860 ms
237,172 KB
testcase_14 AC 886 ms
236,672 KB
testcase_15 AC 880 ms
238,232 KB
testcase_16 AC 904 ms
237,560 KB
testcase_17 AC 908 ms
237,792 KB
testcase_18 AC 901 ms
236,432 KB
testcase_19 AC 870 ms
237,068 KB
testcase_20 AC 864 ms
236,568 KB
testcase_21 AC 873 ms
238,244 KB
testcase_22 AC 859 ms
236,864 KB
testcase_23 AC 887 ms
236,228 KB
testcase_24 AC 899 ms
238,016 KB
testcase_25 AC 877 ms
236,460 KB
testcase_26 AC 914 ms
235,944 KB
testcase_27 AC 851 ms
237,956 KB
testcase_28 AC 893 ms
236,268 KB
testcase_29 AC 923 ms
237,092 KB
testcase_30 AC 873 ms
236,608 KB
testcase_31 AC 895 ms
237,012 KB
testcase_32 AC 905 ms
236,084 KB
testcase_33 AC 912 ms
237,852 KB
testcase_34 AC 927 ms
237,264 KB
testcase_35 AC 855 ms
236,260 KB
testcase_36 AC 904 ms
237,776 KB
testcase_37 AC 871 ms
238,432 KB
testcase_38 AC 932 ms
238,004 KB
testcase_39 AC 850 ms
237,988 KB
testcase_40 AC 905 ms
236,796 KB
testcase_41 AC 873 ms
238,400 KB
testcase_42 AC 851 ms
236,520 KB
testcase_43 AC 912 ms
237,592 KB
testcase_44 AC 856 ms
237,676 KB
testcase_45 AC 930 ms
238,044 KB
testcase_46 AC 879 ms
236,624 KB
testcase_47 AC 890 ms
238,360 KB
testcase_48 AC 907 ms
236,856 KB
testcase_49 AC 877 ms
238,380 KB
testcase_50 AC 909 ms
238,120 KB
testcase_51 AC 917 ms
236,572 KB
testcase_52 AC 914 ms
237,900 KB
testcase_53 AC 891 ms
236,572 KB
testcase_54 AC 913 ms
237,192 KB
testcase_55 AC 899 ms
236,712 KB
testcase_56 AC 917 ms
236,420 KB
testcase_57 AC 907 ms
238,408 KB
testcase_58 AC 937 ms
235,960 KB
testcase_59 AC 871 ms
235,872 KB
testcase_60 AC 944 ms
236,224 KB
testcase_61 AC 898 ms
237,700 KB
testcase_62 AC 867 ms
236,740 KB
testcase_63 AC 924 ms
236,788 KB
testcase_64 AC 863 ms
236,676 KB
testcase_65 AC 897 ms
236,808 KB
testcase_66 AC 865 ms
235,588 KB
testcase_67 AC 925 ms
237,020 KB
testcase_68 AC 966 ms
236,536 KB
testcase_69 AC 937 ms
237,612 KB
testcase_70 AC 878 ms
237,968 KB
testcase_71 AC 907 ms
236,688 KB
testcase_72 AC 876 ms
235,024 KB
testcase_73 AC 936 ms
236,680 KB
testcase_74 AC 882 ms
236,536 KB
testcase_75 AC 908 ms
236,216 KB
testcase_76 AC 884 ms
237,300 KB
testcase_77 AC 962 ms
237,536 KB
testcase_78 AC 922 ms
238,172 KB
testcase_79 AC 876 ms
236,664 KB
testcase_80 AC 949 ms
237,684 KB
testcase_81 AC 893 ms
236,056 KB
testcase_82 AC 919 ms
236,948 KB
testcase_83 AC 905 ms
238,532 KB
testcase_84 AC 914 ms
237,100 KB
testcase_85 AC 920 ms
236,956 KB
testcase_86 AC 860 ms
238,332 KB
testcase_87 AC 905 ms
236,752 KB
testcase_88 AC 877 ms
236,120 KB
testcase_89 AC 896 ms
236,984 KB
testcase_90 AC 881 ms
237,816 KB
testcase_91 AC 905 ms
234,716 KB
testcase_92 AC 940 ms
237,020 KB
testcase_93 AC 892 ms
236,636 KB
testcase_94 AC 889 ms
237,472 KB
testcase_95 AC 890 ms
237,092 KB
testcase_96 AC 883 ms
236,580 KB
testcase_97 AC 911 ms
237,560 KB
testcase_98 AC 878 ms
237,152 KB
testcase_99 AC 932 ms
234,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;

public class Main {
    private static final int VAL = -1000000;

    private int T;
    private int R;
    private int p = 0;
    private int[] tim = new int[5555];
    private int[][] tim2 = new int[5555][5555];
    private int[] S = new int[5555];
    private ArrayList<ArrayList<Integer>> groups = new ArrayList<>();

    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        try (Scanner in = new Scanner(System.in)) {
            T = in.nextInt();
            R = in.nextInt();
            for (int i = 0; i < tim2.length; i++) {
                Arrays.fill(tim2[i], VAL);
            }
            for (int t = 0; t < T; t++) {
                int N = in.nextInt();
                int[] s = IntStream.range(0, N).map(i -> in.nextInt()).toArray();
                ArrayList<A> list = solve(N, s, t);

                StringBuilder sb = new StringBuilder();
                sb.append(list.size() + "\n");
                for (A a : list) {
                    sb.append(a.i + " " + a.j + "\n");
                }
                System.out.print(sb.toString());
                System.out.flush();
            }
        }
    }

    private ArrayList<A> solve(int N, int[] s, int t) {
        ArrayList<A> list = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            p++;
            tim[p] = t;
            S[p] = s[i];
            ArrayList<Integer> best = null;
            int bestV = 0;
            for (int j = 0; j < groups.size(); j++) {
                ArrayList<Integer> g = groups.get(j);

                for (int k = 0; k < g.size(); k++) {
                    tim2[p][g.get(k)] = t;
                    tim2[g.get(k)][p] = t;
                }
                g.add(p);
                int evaluation = evaluate(g);
                if (evaluation > bestV) {
                    bestV = evaluation;
                    best = g;
                }
                g.remove(g.size() - 1);
                for (int k = 0; k < g.size(); k++) {
                    tim2[p][g.get(k)] = VAL;
                    tim2[g.get(k)][p] = VAL;
                }

            }

            if (best == null) {
                ArrayList<Integer> group = new ArrayList<>();
                group.add(p);
                groups.add(group);
            } else {
                for (int k = 0; k < best.size(); k++) {
                    tim2[p][best.get(k)] = t;
                    tim2[best.get(k)][p] = t;
                }

                best.add(p);
                list.add(new A(p, best.get(0)));

                if (best.size() == R) {
                    ArrayList<Integer> listS = new ArrayList<>();
                    for (int j = 0; j < best.size(); j++) {
                        listS.add(S[best.get(j)]);
                    }
                    ArrayList<Integer> listTime = new ArrayList<>();
                    for (int j = 0; j < best.size(); j++) {
                        listTime.add(tim[best.get(j)]);
                    }
                    groups.remove(best);
                }
            }

        }

        if (t == T - 1) {
            for (int i = 0; i < groups.size(); i++) {
                ArrayList<Integer> gi = groups.get(i);
                for (int j = i + 1; j < groups.size(); j++) {
                    ArrayList<Integer> gj = groups.get(j);
                    if (gi.size() + gj.size() > R) {
                        continue;
                    }

                    int ei = evaluate(gi);
                    int ej = evaluate(gj);

                    for (int k = 0; k < gi.size(); k++) {
                        for (int k2 = 0; k2 < gj.size(); k2++) {
                            tim2[gj.get(k2)][gi.get(k)] = t;
                            tim2[gi.get(k)][gj.get(k2)] = t;
                        }
                    }

                    gi.addAll(gj);
                    if (evaluate(gi) > ei + ej) {
                        groups.remove(gj);
                        if (gi.size() == R) {
                            groups.remove(gi);
                            i--;
                        }

                        for (int k2 = 0; k2 < gj.size(); k2++) {
                            list.add(new A(gi.get(0), gj.get(k2)));
                        }

                        break;

                    } else {
                        for (Integer p : gj) {
                            boolean b = gi.remove(p);
                        }

                        for (int k = 0; k < gi.size(); k++) {
                            for (int k2 = 0; k2 < gj.size(); k2++) {
                                tim2[gj.get(k2)][gi.get(k)] = VAL;
                                tim2[gi.get(k)][gj.get(k2)] = VAL;
                            }
                        }

                    }

                }
            }
        }

        return list;
    }

    private int evaluate(ArrayList<Integer> g) {
        int E = 0;
        for (int i = 0; i < g.size(); i++) {
            int pi = g.get(i).intValue();
            for (int j = i + 1; j < g.size(); j++) {
                int pj = g.get(j).intValue();
                E += Math.abs(tim[pi] - tim2[pi][pj]);
                E += Math.abs(tim[pj] - tim2[pi][pj]);
            }
        }

        int maxS = (int) -1e9;
        int minS = (int) 1e9;
        for (int i = 0; i < g.size(); i++) {
            int p = g.get(i).intValue();
            maxS = Math.max(maxS, S[p]);
            minS = Math.min(minS, S[p]);
        }
        int mean = (minS + maxS) / 2;
        int abs = Math.abs(mean - 50);
        if (abs < 5) {
            if (maxS - minS > 2) {
                return 0;
            }
        } else if (abs < 10) {
            if (maxS - minS > 2) {
                return 0;
            }
        } else if (abs < 15) {
            if (maxS - minS > 3) {
                return 0;
            }
        } else if (abs < 20) {
            if (maxS - minS > 3) {
                return 0;
            }
        } else if (abs < 25) {
            if (maxS - minS > 3) {
                return 0;
            }
        } else if (abs < 30) {
            if (maxS - minS > 4) {
                return 0;
            }
        } else if (abs < 35) {
            if (maxS - minS > 5) {
                return 0;
            }
        } else {
            if (maxS - minS > 5) {
                return 0;
            }
        }

        int ce = g.size() == 1 ? 0 : g.size() == 2 ? 1 : g.size() == 3 ? 3 : g.size() == 4 ? 6 : -1000;
        return Math.max(0, ce * (200 - (maxS - minS) * (maxS - minS)) - E);
    }

}

class A {
    int i;
    int j;

    public A(int i, int j) {
        this.i = i;
        this.j = j;
    }
}

class UnionFind {
    int[] data;

    UnionFind(int size) {
        data = new int[size];
        Arrays.fill(data, -1);
    }

    boolean unionSet(int x, int y) {
        x = root(x);
        y = root(y);
        if (x != y) {
            if (data[y] < data[x]) {
                int swap = x;
                x = y;
                y = swap;
            }
            data[x] += data[y];
            data[y] = x;
        }
        return x != y;
    }

    boolean findSet(int x, int y) {
        return root(x) == root(y);
    }

    int root(int x) {
        return data[x] < 0 ? x : (data[x] = root(data[x]));
    }

    int size(int x) {
        return -data[root(x)];
    }
}
0