AtCoder Regular Contest 069

Submission #1662787

Source codeソースコード

use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;


fn main(){
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin);

    while let Some(n) = sc.read1() {
        let mut xs = vec![];
        for _ in 0..n {
            let x: i64 = sc.read();
            xs.push(x);
        }
        let xs = xs;

        // calculate ascending subsequence
        let mut ascending = vec![(0, std::usize::MAX)];
        for (i, &x) in xs.iter().enumerate() {
            if x > ascending.last().unwrap().0 {
                ascending.push((x, i));
            }
        }

        let k = ascending.len();
        let mut res = vec![0; n];
        let mut partial_sum = vec![0; k];
        for x in xs {
            // binary search...
            let mut lo = 0;
            let mut hi = k;
            while hi - lo > 1 {
                let mid = (lo + hi) / 2;
                if x < ascending[mid].0 {
                    hi = mid;
                } else {
                    lo = mid;
                }
            }
            assert!(ascending[lo].0 <= x);
            assert!(hi == k || x < ascending[hi].0);

            let idx = std::cmp::min(lo + 1, k - 1);
            res[ascending[idx].1] += x - ascending[lo].0;
            if lo > 0 {
                partial_sum[lo - 1] += 1;
            }
        }

        for j in (1..k).rev() {
            partial_sum[j - 1] += partial_sum[j];
        }

        for j in 1..k {
            let gain = partial_sum[j - 1] * (ascending[j].0 - ascending[j - 1].0);
            res[ascending[j].1] += gain;
        }

        for r in res {
            println!("{}", r);
        }
    }
}

//=============================================================================

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }
}

Submission

Task問題 E - Frequency
User nameユーザ名 もふ
Created time投稿日時
Language言語 Rust (1.15.1)
Status状態 AC
Score得点 700
Source lengthソースコード長 2374 Byte
File nameファイル名
Exec time実行時間 206 ms
Memory usageメモリ使用量 11004 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - 00_example_01.txt,00_example_02.txt
All 700 / 700 00_example_01.txt,00_example_02.txt,01.txt,02.txt,03.txt,04.txt,05.txt,06.txt,07.txt,08.txt,09.txt,10.txt,11.txt,12.txt,13.txt,14.txt,15.txt,16.txt,17.txt,18.txt,19.txt,20.txt,21.txt,22.txt,23.txt,24.txt,25.txt,26.txt,27.txt,28.txt,29.txt,30.txt,31.txt,32.txt,33.txt,34.txt,35.txt,36.txt,37.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
00_example_01.txt AC 2 ms 4352 KB
00_example_02.txt AC 2 ms 4352 KB
01.txt AC 2 ms 4352 KB
02.txt AC 13 ms 4352 KB
03.txt AC 2 ms 4352 KB
04.txt AC 3 ms 4352 KB
05.txt AC 2 ms 4352 KB
06.txt AC 2 ms 4352 KB
07.txt AC 2 ms 4352 KB
08.txt AC 2 ms 4352 KB
09.txt AC 18 ms 4352 KB
10.txt AC 2 ms 4352 KB
11.txt AC 184 ms 6524 KB
12.txt AC 184 ms 6524 KB
13.txt AC 186 ms 6524 KB
14.txt AC 183 ms 6524 KB
15.txt AC 183 ms 6524 KB
16.txt AC 198 ms 6524 KB
17.txt AC 206 ms 11004 KB
18.txt AC 196 ms 6524 KB
19.txt AC 170 ms 6524 KB
20.txt AC 181 ms 6524 KB
21.txt AC 2 ms 4352 KB
22.txt AC 2 ms 4352 KB
23.txt AC 2 ms 4352 KB
24.txt AC 14 ms 4352 KB
25.txt AC 2 ms 4352 KB
26.txt AC 3 ms 4352 KB
27.txt AC 2 ms 4352 KB
28.txt AC 200 ms 9212 KB
29.txt AC 197 ms 9212 KB
30.txt AC 197 ms 9212 KB
31.txt AC 195 ms 9212 KB
32.txt AC 197 ms 9212 KB
33.txt AC 198 ms 6524 KB
34.txt AC 189 ms 6524 KB
35.txt AC 188 ms 6524 KB
36.txt AC 187 ms 6524 KB
37.txt AC 190 ms 6524 KB