提出 #70622350


ソースコード 拡げる

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            String s = sc.next();
            if (n <= 10) {
                @SuppressWarnings("unchecked")
                Set<Integer>[] sets = new Set[n];
                for (int i = 0; i < n; i++) sets[i] = new HashSet<Integer>();
                int[] nums = new int[n];
                for (int i = 0; i < n; i++) nums[i] = i + 1;
                generatePerms(nums, 0, s, sets);
                for (int i = 0; i < n; i++) {
                    System.out.print(sets[i].size());
                    if (i < n - 1) System.out.print(" ");
                }
                System.out.println();
            } else {
                int[] min = new int[n + 1], max = new int[n + 1];
                Arrays.fill(min, 1);
                Arrays.fill(max, n);
                boolean changed = true;
                while (changed) {
                    changed = false;
                    for (int i = 0; i < n - 1; i++) {
                        int a = i + 1, b = i + 2;
                        if (s.charAt(i) == 'R') {
                            int newMin = Math.max(min[b], min[a] + 1);
                            int newMax = Math.min(max[a], max[b] - 1);
                            if (newMin != min[b] || newMax != max[a]) {
                                min[b] = newMin; max[a] = newMax; changed = true;
                            }
                        } else {
                            int newMin = Math.max(min[a], min[b] + 1);
                            int newMax = Math.min(max[b], max[a] - 1);
                            if (newMin != min[a] || newMax != max[b]) {
                                min[a] = newMin; max[b] = newMax; changed = true;
                            }
                        }
                    }
                    for (int i = n - 2; i >= 0; i--) {
                        int a = i + 1, b = i + 2;
                        if (s.charAt(i) == 'R') {
                            int newMin = Math.max(min[b], min[a] + 1);
                            int newMax = Math.min(max[a], max[b] - 1);
                            if (newMin != min[b] || newMax != max[a]) {
                                min[b] = newMin; max[a] = newMax; changed = true;
                            }
                        } else {
                            int newMin = Math.max(min[a], min[b] + 1);
                            int newMax = Math.min(max[b], max[a] - 1);
                            if (newMin != min[a] || newMax != max[b]) {
                                min[a] = newMin; max[b] = newMax; changed = true;
                            }
                        }
                    }
                }
                int[] result = new int[n + 1];
                for (int num = 1; num <= n; num++) {
                    if (min[num] <= max[num]) {
                        for (int pos = min[num]; pos <= max[num]; pos++) {
                            result[pos]++;
                        }
                    }
                }
                for (int i = 1; i <= n; i++) {
                    System.out.print(result[i]);
                    if (i < n) System.out.print(" ");
                }
                System.out.println();
            }
        }
        sc.close();
    }
    
    static boolean isValid(int[] arr, String s) {
        int[] pos = new int[arr.length + 1];
        for (int i = 0; i < arr.length; i++) pos[arr[i]] = i;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'R') {
                if (pos[i + 2] <= pos[i + 1]) return false;
            } else {
                if (pos[i + 2] >= pos[i + 1]) return false;
            }
        }
        return true;
    }
    
    static void generatePerms(int[] nums, int start, String s, Set<Integer>[] sets) {
        if (start == nums.length) {
            if (isValid(nums, s)) {
                for (int i = 0; i < nums.length; i++) sets[i].add(nums[i]);
            }
            return;
        }
        for (int i = start; i < nums.length; i++) {
            swap(nums, start, i);
            generatePerms(nums, start + 1, s, sets);
            swap(nums, start, i);
        }
    }
    
    static void swap(int[] arr, int i, int j) {
        int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
    }
}

提出情報

提出日時
問題 F - Back and Forth Filling
ユーザ addy
言語 Java24 (OpenJDK 24.0.2)
得点 0
コード長 4607 Byte
結果 WA
実行時間 > 2000 ms
メモリ 86304 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 500
結果
WA × 1
AC × 13
WA × 16
TLE × 40
セット名 テストケース
Sample sample_01.txt
All sample_01.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt, test_41.txt, test_42.txt, test_43.txt, test_44.txt, test_45.txt, test_46.txt, test_47.txt, test_48.txt, test_49.txt, test_50.txt, test_51.txt, test_52.txt, test_53.txt, test_54.txt, test_55.txt, test_56.txt, test_57.txt, test_58.txt, test_59.txt, test_60.txt, test_61.txt, test_62.txt, test_63.txt, test_64.txt, test_65.txt, test_66.txt, test_67.txt, test_68.txt
ケース名 結果 実行時間 メモリ
sample_01.txt WA 82 ms 40448 KiB
test_01.txt AC 75 ms 40620 KiB
test_02.txt AC 72 ms 40272 KiB
test_03.txt AC 75 ms 40844 KiB
test_04.txt AC 73 ms 40484 KiB
test_05.txt AC 74 ms 40780 KiB
test_06.txt AC 79 ms 41132 KiB
test_07.txt AC 100 ms 43584 KiB
test_08.txt AC 155 ms 60500 KiB
test_09.txt AC 279 ms 61128 KiB
test_10.txt AC 1710 ms 61236 KiB
test_11.txt TLE > 2000 ms 40280 KiB
test_12.txt WA 181 ms 46516 KiB
test_13.txt WA 227 ms 49188 KiB
test_14.txt WA 322 ms 59688 KiB
test_15.txt WA 487 ms 69912 KiB
test_16.txt WA 875 ms 81116 KiB
test_17.txt AC 622 ms 62328 KiB
test_18.txt AC 688 ms 67104 KiB
test_19.txt TLE > 2000 ms 28496 KiB
test_20.txt TLE > 2000 ms 27940 KiB
test_21.txt WA 623 ms 63596 KiB
test_22.txt WA 651 ms 64092 KiB
test_23.txt WA 614 ms 65488 KiB
test_24.txt WA 642 ms 64432 KiB
test_25.txt WA 960 ms 74692 KiB
test_26.txt WA 1069 ms 86304 KiB
test_27.txt WA 1114 ms 78644 KiB
test_28.txt WA 900 ms 75588 KiB
test_29.txt WA 1117 ms 77060 KiB
test_30.txt WA 1068 ms 68368 KiB
test_31.txt TLE > 2000 ms 35152 KiB
test_32.txt TLE > 2000 ms 37260 KiB
test_33.txt TLE > 2000 ms 25280 KiB
test_34.txt TLE > 2000 ms 24720 KiB
test_35.txt TLE > 2000 ms 28076 KiB
test_36.txt TLE > 2000 ms 27808 KiB
test_37.txt TLE > 2000 ms 27724 KiB
test_38.txt TLE > 2000 ms 27860 KiB
test_39.txt TLE > 2000 ms 27796 KiB
test_40.txt TLE > 2000 ms 27784 KiB
test_41.txt TLE > 2000 ms 28000 KiB
test_42.txt TLE > 2000 ms 27876 KiB
test_43.txt TLE > 2000 ms 28108 KiB
test_44.txt TLE > 2000 ms 27320 KiB
test_45.txt TLE > 2000 ms 27900 KiB
test_46.txt TLE > 2000 ms 28156 KiB
test_47.txt TLE > 2000 ms 27556 KiB
test_48.txt TLE > 2000 ms 28112 KiB
test_49.txt TLE > 2000 ms 27756 KiB
test_50.txt TLE > 2000 ms 27876 KiB
test_51.txt TLE > 2000 ms 41504 KiB
test_52.txt TLE > 2000 ms 28444 KiB
test_53.txt AC 617 ms 62520 KiB
test_54.txt TLE > 2000 ms 27476 KiB
test_55.txt TLE > 2000 ms 27888 KiB
test_56.txt TLE > 2000 ms 28360 KiB
test_57.txt TLE > 2000 ms 27912 KiB
test_58.txt TLE > 2000 ms 27856 KiB
test_59.txt TLE > 2000 ms 27896 KiB
test_60.txt TLE > 2000 ms 28404 KiB
test_61.txt TLE > 2000 ms 28076 KiB
test_62.txt TLE > 2000 ms 28964 KiB
test_63.txt TLE > 2000 ms 28732 KiB
test_64.txt TLE > 2000 ms 28276 KiB
test_65.txt TLE > 2000 ms 27708 KiB
test_66.txt TLE > 2000 ms 28160 KiB
test_67.txt TLE > 2000 ms 27856 KiB
test_68.txt TLE > 2000 ms 27864 KiB