提出 #58034179


ソースコード 拡げる

#![allow(unused)]

fn main() {
    let tc = read::<usize>();
    let mut ans = vec![];
    for _ in 0..tc {
        let n = read::<usize>();
        let arr = readv::<i32>();

        // dp1[i] = max length of increasing subsequence ending at i
        let dp1 = longest_increasing_subsequence(&arr, true, i32::MAX);

        // dp2[i] = max length of increasing subsequence starting from i
        let inv_rev = (0..n).map(|i| -arr[n - 1 - i]).collect();
        let mut dp2 = longest_increasing_subsequence(&inv_rev, true, i32::MAX);
        dp2.reverse();

        // index i is included in LIS if dp1[i] + dp2[i] > len(LIS)
        let lis = *dp1.iter().max().unwrap();
        let indices: Vec<usize> = (0..n)
            .filter(|&i| dp1[i] + dp2[i] > lis)
            .map(|i| i + 1)
            .collect();

        ans.push(format!("{}\n{}", indices.len(), join(&indices, " ")));
    }

    println!("{}", join(&ans, "\n"));
}

fn longest_increasing_subsequence<T>(arr: &Vec<T>, strict: bool, inf: T) -> Vec<usize>
where
    T: PartialOrd + Clone,
{
    // dp[i] = the minimum value of the last element of IS of length i
    // lis[i] = maximum length of IS ending at i
    let n = arr.len();
    let mut dp = vec![inf; n];
    let mut lis = vec![0; n];
    for i in 0..n {
        let j = dp.partition_point(|x| {
            if strict {
                *x < arr[i] // strictly LIS
            } else {
                *x <= arr[i] // monotonically LIS
            }
        });
        dp[j] = arr[i].clone();
        lis[i] = j + 1;
    }
    lis
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn readv<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_ascii_whitespace()
        .map(|t| t.parse().ok().unwrap())
        .collect()
}

fn reads() -> Vec<char> {
    read::<String>().chars().collect()
}

fn mapv<T, S, F: Fn(&T) -> S>(arr: &Vec<T>, f: F) -> Vec<S> {
    arr.iter().map(f).collect()
}

fn join<T: ToString>(arr: &[T], sep: &str) -> String {
    arr.iter()
        .map(|x| x.to_string())
        .collect::<Vec<String>>()
        .join(sep)
}

提出情報

提出日時
問題 F - Useless for LIS
ユーザ amoshuangyc
言語 Rust (rustc 1.70.0)
得点 525
コード長 2281 Byte
結果 AC
実行時間 79 ms
メモリ 24436 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 525 / 525
結果
AC × 2
AC × 46
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.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, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 02_hand_00.txt, 02_hand_01.txt, 02_hand_02.txt, 02_hand_03.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 1848 KiB
00_sample_01.txt AC 1 ms 2072 KiB
01_random_00.txt AC 79 ms 24340 KiB
01_random_01.txt AC 32 ms 7624 KiB
01_random_02.txt AC 29 ms 6628 KiB
01_random_03.txt AC 24 ms 3968 KiB
01_random_04.txt AC 22 ms 3260 KiB
01_random_05.txt AC 30 ms 7408 KiB
01_random_06.txt AC 30 ms 7548 KiB
01_random_07.txt AC 30 ms 7404 KiB
01_random_08.txt AC 30 ms 7216 KiB
01_random_09.txt AC 30 ms 7416 KiB
01_random_10.txt AC 75 ms 24388 KiB
01_random_11.txt AC 29 ms 7688 KiB
01_random_12.txt AC 28 ms 7540 KiB
01_random_13.txt AC 20 ms 3568 KiB
01_random_14.txt AC 21 ms 3404 KiB
01_random_15.txt AC 29 ms 7536 KiB
01_random_16.txt AC 29 ms 7404 KiB
01_random_17.txt AC 29 ms 7340 KiB
01_random_18.txt AC 29 ms 7436 KiB
01_random_19.txt AC 28 ms 7224 KiB
01_random_20.txt AC 74 ms 24436 KiB
01_random_21.txt AC 29 ms 7684 KiB
01_random_22.txt AC 25 ms 5356 KiB
01_random_23.txt AC 23 ms 4284 KiB
01_random_24.txt AC 20 ms 4004 KiB
01_random_25.txt AC 31 ms 20252 KiB
01_random_26.txt AC 31 ms 20296 KiB
01_random_27.txt AC 32 ms 20328 KiB
01_random_28.txt AC 32 ms 20308 KiB
01_random_29.txt AC 27 ms 20292 KiB
01_random_30.txt AC 29 ms 18472 KiB
01_random_31.txt AC 30 ms 19064 KiB
01_random_32.txt AC 28 ms 19568 KiB
01_random_33.txt AC 30 ms 19324 KiB
01_random_34.txt AC 30 ms 19052 KiB
01_random_35.txt AC 30 ms 19352 KiB
01_random_36.txt AC 29 ms 20140 KiB
01_random_37.txt AC 29 ms 18672 KiB
01_random_38.txt AC 31 ms 18564 KiB
01_random_39.txt AC 30 ms 20144 KiB
02_hand_00.txt AC 1 ms 1952 KiB
02_hand_01.txt AC 25 ms 20372 KiB
02_hand_02.txt AC 41 ms 19980 KiB
02_hand_03.txt AC 26 ms 20208 KiB