結果

問題 No.366 ロボットソート
ユーザー keikei
提出日時 2018-11-12 01:20:51
言語 Rust
(1.77.0 + proconio)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,370 bytes
コンパイル時間 13,488 ms
コンパイル使用メモリ 387,876 KB
最終ジャッジ日時 2024-11-14 20:40:57
合計ジャッジ時間 14,265 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
error: expected one of `:`, `@`, or `|`, found `)`
  --> src/main.rs:27:29
   |
27 |     fn lower_bound(&self, &T) -> usize;
   |                             ^ expected one of `:`, `@`, or `|`
   |
   = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a parameter name, give it a type
   |
27 |     fn lower_bound(&self, T: &TypeName) -> usize;
   |                           ~~~~~~~~~~~~
help: if this is a type, explicitly ignore the parameter name
   |
27 |     fn lower_bound(&self, _: &T) -> usize;
   |                           ++

error: expected one of `:`, `@`, or `|`, found `)`
  --> src/main.rs:28:29
   |
28 |     fn upper_bound(&self, &T) -> usize;
   |                             ^ expected one of `:`, `@`, or `|`
   |
   = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a parameter name, give it a type
   |
28 |     fn upper_bound(&self, T: &TypeName) -> usize;
   |                           ~~~~~~~~~~~~
help: if this is a type, explicitly ignore the parameter name
   |
28 |     fn upper_bound(&self, _: &T) -> usize;
   |                           ++

error: could not compile `main` (bin "main") due to 2 previous errors

ソースコード

diff #

#![allow(unused_mut, non_snake_case, unused_imports)]

use std::iter;
use std::cmp::{max, min, Ordering};
use std::mem::swap;
use std::collections::{HashMap, BTreeMap, HashSet, BTreeSet, BinaryHeap, VecDeque};
use std::iter::FromIterator;

// 高速 EOF要
//macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();input_inner!{iter, $($r)*}};($($r:tt)*) => {let mut s = {use std::io::Read;let mut s = String::new();std::io::stdin().read_to_string(&mut s).unwrap();s};let mut iter = s.split_whitespace();input_inner!{iter, $($r)*}};}
//macro_rules! input_inner {($iter:expr) => {};($iter:expr, ) => {};($iter:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($iter, $t);input_inner!{$iter $($r)*}};}
//macro_rules! read_value {($iter:expr, ( $($t:tt),* )) => {( $(read_value!($iter, $t)),* )};($iter:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()};($iter:expr, chars) => {read_value!($iter, String).chars().collect::<Vec<char>>()};($iter:expr, usize1) => {read_value!($iter, usize) - 1};($iter:expr, $t:ty) => {$iter.next().unwrap().parse::<$t>().expect("Parse error")};}
// 低速 EOF不要
macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() };input_inner!{next, $($r)*}};($($r:tt)*) => {let stdin = std::io::stdin();let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));let mut next = move || -> String{bytes.by_ref().map(|r|r.unwrap() as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect()};input_inner!{next, $($r)*}};}
macro_rules! input_inner {($next:expr) => {};($next:expr, ) => {};($next:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($next, $t);input_inner!{$next $($r)*}};}
macro_rules! read_value {($next:expr, ( $($t:tt),* )) => {( $(read_value!($next, $t)),* )};($next:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()};($next:expr, chars) => {read_value!($next, String).chars().collect::<Vec<char>>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};}

/*
 <url:https://yukicoder.me/problems/no/366>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

pub trait BinarySearch<T> {
    fn lower_bound(&self, &T) -> usize;
    fn upper_bound(&self, &T) -> usize;
}

impl<T: Ord> BinarySearch<T> for [T] {
    fn lower_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();

        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less => {
                    low = mid + 1;
                }
                Ordering::Equal | Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }

    fn upper_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();

        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less | Ordering::Equal => {
                    low = mid + 1;
                }
                Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
}

fn main() {
    input!(N:usize,K:usize,a:[usize;N]);
    let N:usize = N;
    let K:usize = K;
    let mut a:Vec<usize> = a;

    let mut t = a.clone();
    t.sort();

    for i in 0..N{
        a[i] = t.lower_bound(&a[i]);
    }
    let mut ans = 0;
    for i in 0..N{
        if a[i] == i{ continue; }
        let mut target = i;
        while target < N{
            if a[target] == i{
                break;
            }
            target += K;
        }
        if target >= N{
            println!("-1");
            return;
        }
        while target != i{
            ans += 1;
            let t1 = a[target];
            a[target] = a[target-K];
            a[target-K] = t1;
            target -= K;
        }
    }
    println!("{}",ans);
}
0