Submission #71639705


Source Code Expand

import std;

void main () {
    int N, K;
    readln.read(N, K);
    auto P = readln.split.to!(int[]);
    P[] -= 1;

    // Q[i] := 値iがある位置
    auto Q = new int[](N);
    foreach (i; 0 .. N) {
        Q[P[i]] = i;
    }

    auto swm = new SlidingWindowMinimum!(int)(Q);
    auto qs = new Tuple!(int, int)[](N - K + 1);

    foreach (i; 0 .. N - K + 1) {
        qs[i] = tuple(i, i + K);
    }
    auto minIdx = swm.solve(qs);
    auto maxIdx = swm.solve!"a > b"(qs);

    int ans = int.max;
    foreach (i; 0 .. N - K + 1) {
        ans = min(ans, Q[maxIdx[i]] - Q[minIdx[i]]);
    }

    writeln(ans);
}

class SlidingWindowMinimum (T) {
    T[] A;
    this (T[] A_) {
        A = A_;
    }

    int[] solve (alias cmp = "a < b") (const Tuple!(int, int)[] queries) {
        alias cmpfun = binaryFun!(cmp);
        auto ret = new int[](queries.length);
        auto st = DList!(T)();

        int l = 0;
        int r = 0;
        foreach (i; 0 .. queries.length) {
            while (r < queries[i][1]) {
                while (!st.empty() && !cmpfun(A[st.front()], A[r])) {
                    st.removeFront();
                }
                st.insertFront(r);
                r++;
            }

            while (l < queries[i][0]) {
                if (st.back() == l) {
                    st.removeBack();
                }
                l++;
            }

            ret[i] = st.empty() ? -1 : st.back();
        }

        return ret;
    }
}

void read (T...) (string S, ref T args) {
    import std.conv : to;
    import std.array : split;
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}

Submission Info

Submission Time
Task D - Permutation Subsequence
User InTheBloom
Language D (DMD 2.104.0)
Score 425
Code Size 1754 Byte
Status AC
Exec Time 66 ms
Memory 25268 KiB

Compile Error

/home/runner/.dub/packages/mir-algorithm/3.20.4/mir-algorithm/source/mir/serde.d(52,9): Deprecation: alias this for classes/interfaces is deprecated

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status
AC × 3
AC × 28
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 03_handmade_00.txt, 03_handmade_01.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3736 KiB
00_sample_01.txt AC 1 ms 3732 KiB
00_sample_02.txt AC 1 ms 3700 KiB
01_random_00.txt AC 39 ms 11256 KiB
01_random_01.txt AC 64 ms 14904 KiB
01_random_02.txt AC 22 ms 7524 KiB
01_random_03.txt AC 63 ms 14864 KiB
01_random_04.txt AC 60 ms 14596 KiB
01_random_05.txt AC 63 ms 14904 KiB
01_random_06.txt AC 51 ms 11312 KiB
01_random_07.txt AC 62 ms 13972 KiB
01_random_08.txt AC 55 ms 10540 KiB
01_random_09.txt AC 65 ms 14680 KiB
01_random_10.txt AC 16 ms 6600 KiB
01_random_11.txt AC 62 ms 14140 KiB
01_random_12.txt AC 10 ms 6000 KiB
01_random_13.txt AC 66 ms 14756 KiB
01_random_14.txt AC 23 ms 7392 KiB
01_random_15.txt AC 60 ms 12564 KiB
01_random_16.txt AC 65 ms 13648 KiB
01_random_17.txt AC 1 ms 3500 KiB
02_random2_00.txt AC 54 ms 23964 KiB
02_random2_01.txt AC 57 ms 14716 KiB
02_random2_02.txt AC 60 ms 15192 KiB
02_random2_03.txt AC 60 ms 14904 KiB
02_random2_04.txt AC 55 ms 14220 KiB
03_handmade_00.txt AC 61 ms 25268 KiB
03_handmade_01.txt AC 61 ms 25224 KiB