結果

問題 No.1311 Reverse Permutation Index
ユーザー tentententen
提出日時 2022-08-17 13:07:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 57 ms / 1,500 ms
コード長 2,394 bytes
コンパイル時間 2,456 ms
コンパイル使用メモリ 78,848 KB
実行使用メモリ 50,512 KB
最終ジャッジ日時 2024-04-15 03:41:34
合計ジャッジ時間 3,594 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
50,156 KB
testcase_01 AC 56 ms
50,416 KB
testcase_02 AC 55 ms
50,076 KB
testcase_03 AC 55 ms
50,248 KB
testcase_04 AC 55 ms
50,260 KB
testcase_05 AC 57 ms
50,316 KB
testcase_06 AC 57 ms
49,948 KB
testcase_07 AC 57 ms
50,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        long v = sc.nextLong();
        int length = sc.nextInt();
        int[] values = getArray(v, length);
        int[] nexts = new int[length];
        for (int i = 0; i < length; i++) {
            nexts[values[i]] = i;
        }
        System.out.println(getOrder(nexts));
    }
    
    static long getOrder(int[] arr) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            list.add(i);
        }
        long ans = 0;
        for (int i = 0; i < arr.length; i++) {
            long tmp = proc(arr.length - i - 1);
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j) == arr[i]) {
                    list.remove(j);
                    break;
                }
                ans += tmp;
            }
        }
        return ans;
    }
    
    static int[] getArray(long v, int length) {
        int[] ans = new int[length];
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            list.add(i);
        }
        for (int i = 0; i < length; i++) {
            long tmp = proc(length - i - 1);
            for (int j = 0; j < list.size(); j++) {
                if (tmp > v) {
                    ans[i] = list.remove(j);
                    break;
                }
                v -= tmp;
            }
        }
        return ans;
    }
    
    static long proc(int x) {
        if (x == 0) {
            return 1;
        } else {
            return x * proc(x - 1);
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0