結果

問題 No.270 next_permutation (1)
ユーザー koba-e964koba-e964
提出日時 2017-01-11 13:59:49
言語 Rust
(1.77.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 2,774 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 144,564 KB
実行使用メモリ 18,104 KB
最終ジャッジ日時 2023-08-23 08:27:22
合計ジャッジ時間 2,371 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 4 ms
4,680 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 9 ms
5,264 KB
testcase_10 AC 63 ms
15,892 KB
testcase_11 AC 66 ms
18,092 KB
testcase_12 AC 67 ms
18,104 KB
testcase_13 AC 59 ms
14,724 KB
testcase_14 AC 59 ms
15,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::*;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    return ret;
}
fn get_word() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.is_err() || res.ok().unwrap() == 0 || u8b[0] <= ' ' as u8 {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = std::string::String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}
fn parse<T: std::str::FromStr>(s: &str) -> T { s.parse::<T>().ok().unwrap() }

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { parse(&get_word()) }

// Returns the least index of elements that are modified, wrapped with Some.
// If the entire array is reversed, it returns None instead.
// v's elements must be pairwise distinct.
fn next_permutation<T: Ord>(v: &mut [T]) -> Option<usize> {
    let mut tail_dec: usize = 1;
    let n = v.len();
    while tail_dec < n {
        if v[n - tail_dec - 1] > v[n - tail_dec] {
            tail_dec += 1;
        } else {
            break;
        }
    }
    // v[n - tail_dec .. n] is strictly decreasing
    if tail_dec < n {
        let x = n - tail_dec - 1;
        let mut y = n;
        {
            let pivot = &v[x];
            for i in (n - tail_dec .. n).rev() {
                if v[i] > *pivot {
                    y = i;
                    break;
                }
            }
            assert!(y < n);
        }
        v.swap(x, y);
    }
    v[n - tail_dec .. n].reverse();
    if tail_dec < n {
        Some(n - tail_dec - 1)
    } else {
        None
    }
}

fn main() {
    let n: usize = get();
    let k: usize = get();
    let mut p: Vec<i64> = (0 .. n).map(|_| get()).collect();
    let b: Vec<i64> = (0 .. n).map(|_| get()).collect();
    let mut mods: Vec<Vec<(i64, usize)>> = vec![Vec::new(); n];
    for i in 0 .. n {
        mods[i].push((p[i], 0));
    }
    let mut tot: u64 = 0;
    for stage in 1 .. k {
        let ret = match next_permutation(&mut p) {
            Some(idx) => idx,
            None => 0,
        };
        for i in ret .. n {
            mods[i].push((p[i], stage));
        }
    }
    for i in 0 .. n {
        mods[i].push((0x3141592653589793, k));
    }
    for i in 0 .. n {
        let m = &mods[i];
        for j in 0 .. m.len() - 1 {
            tot += (b[i] - m[j].0).abs() as u64 * ((m[j + 1].1 - m[j].1) as u64);
        }
    }
    println!("{}", tot);
}
0