結果

問題 No.1859 ><<<
ユーザー koba-e964koba-e964
提出日時 2023-04-01 01:30:07
言語 Rust
(1.77.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 2,353 bytes
コンパイル時間 1,973 ms
コンパイル使用メモリ 172,148 KB
実行使用メモリ 14,748 KB
最終ジャッジ日時 2023-10-24 11:08:48
合計ジャッジ時間 5,642 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 54 ms
14,748 KB
testcase_05 AC 53 ms
14,748 KB
testcase_06 AC 54 ms
14,748 KB
testcase_07 AC 53 ms
14,748 KB
testcase_08 AC 55 ms
14,748 KB
testcase_09 AC 53 ms
14,748 KB
testcase_10 AC 10 ms
4,348 KB
testcase_11 AC 34 ms
9,400 KB
testcase_12 AC 30 ms
8,332 KB
testcase_13 AC 18 ms
5,900 KB
testcase_14 AC 35 ms
9,880 KB
testcase_15 AC 47 ms
12,716 KB
testcase_16 AC 52 ms
13,844 KB
testcase_17 AC 40 ms
10,760 KB
testcase_18 AC 51 ms
13,516 KB
testcase_19 AC 37 ms
10,564 KB
testcase_20 AC 45 ms
13,036 KB
testcase_21 AC 52 ms
14,372 KB
testcase_22 AC 49 ms
13,900 KB
testcase_23 AC 44 ms
12,660 KB
testcase_24 AC 51 ms
14,292 KB
testcase_25 AC 52 ms
13,564 KB
testcase_26 AC 47 ms
13,072 KB
testcase_27 AC 53 ms
13,836 KB
testcase_28 AC 49 ms
14,448 KB
testcase_29 AC 48 ms
13,356 KB
testcase_30 AC 47 ms
12,700 KB
testcase_31 AC 46 ms
12,684 KB
testcase_32 AC 55 ms
14,332 KB
testcase_33 AC 47 ms
12,692 KB
testcase_34 AC 49 ms
14,472 KB
testcase_35 AC 55 ms
14,388 KB
testcase_36 AC 54 ms
14,364 KB
testcase_37 AC 49 ms
13,028 KB
testcase_38 AC 56 ms
14,708 KB
testcase_39 AC 54 ms
14,388 KB
testcase_40 AC 51 ms
14,252 KB
testcase_41 AC 53 ms
14,380 KB
testcase_42 AC 45 ms
12,652 KB
testcase_43 AC 51 ms
13,868 KB
testcase_44 AC 50 ms
13,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($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 ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

// Z algorithm. Calculates an array a[i] = |lcp(s, &s[i..])|,
// where s is the given slice.
// If n = s.length(), the returned array has length n + 1.
// E.g. z_algorithm(b"ababa") = vec![5, 0, 3, 0, 1, 0]
// Reference: http://snuke.hatenablog.com/entry/2014/12/03/214243
// Verified by: ABC284-F (https://atcoder.jp/contests/abc284/submissions/38752029)
fn z_algorithm<T: PartialEq>(s: &[T]) -> Vec<usize> {
    let n = s.len();
    let mut ret = vec![0; n + 1];
    ret[0] = n;
    let mut i = 1; let mut j = 0;
    while i < n {
        while i + j < n && s[j] == s[i + j] { j += 1; }
        ret[i] = j;
        if j == 0 { i += 1; continue; }
        let mut k = 1;
        while i + k < n && k + ret[k] < j {
            ret[i + k] = ret[k];
            k += 1;
        }
        i += k; j -= k;
    }
    ret
}

fn main() {
    input! {
        n: usize,
        a: [usize; n],
        s: chars,
    }
    let mut s = s;
    for i in 0..2 * n {
        let c = if a[i % n] < a[(i + 1) % n] { '<' } else { '>' };
        s.push(c);
    }
    let z = z_algorithm(&s);
    const INF: i32 = 1 << 28;
    let mut ans = INF;
    for i in 0..n {
        if z[n - 1 + i] >= n - 1 {
            ans = min(ans, i as i32);
        }
    }
    println!("{}", if ans >= INF { -1 } else { ans });
}
0