Submission #1097636


Source Code Expand

use std::io::{self, Stdin};
use std::str::{self, FromStr};
use std::error::Error;

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.ne();
    let nn = n as i64;
    let a: Vec<i64> = (0..n).map(|_| sc.ne()).collect();
    let sum: i64 = a.iter().fold(0, |acc, &x| acc + x);
    let sumn: i64 = (1..nn + 1).fold(0, |acc, x| acc + x);
    let mut ok = true;
    if sum % sumn != 0 {
        ok = false;
    }
    let key = sum / sumn;
    let mut cntd = 0;
    for i in 0..n {
        let j = (i + 1) % n;
        let x = a[j] - a[i];
        match x {
            val if val == key => {}
            val if val > 0 => ok = false,
            _ => {
                let d = key - x;
                if d % nn != 0 {
                    ok = false;
                } else {
                    cntd += d / nn;
                }
            }
        }
    }
    if cntd != key {
        ok = false;
    }
    println!("{}", if ok { "YES" } else { "NO" });
}
struct Scanner {
    stdin: Stdin,
    id: usize,
    buf: Vec<u8>,
}

impl Scanner {
    fn new() -> Scanner {
        Scanner {
            stdin: io::stdin(),
            id: 0,
            buf: Vec::new(),
        }
    }
    fn next_line(&mut self) -> Option<String> {
        let mut res = String::new();
        match self.stdin.read_line(&mut res) {
            Ok(0) => return None,
            Ok(_) => Some(res),
            Err(why) => panic!("error in read_line: {}", why.description()),
        }
    }
    fn next<T: FromStr>(&mut self) -> Option<T> {
        while self.buf.len() == 0 {
            self.buf = match self.next_line() {
                Some(r) => {
                    self.id = 0;
                    r.trim().as_bytes().to_owned()
                }
                None => return None,
            };
        }
        let l = self.id;
        assert!(self.buf[l] != b' ');
        let n = self.buf.len();
        let mut r = l;
        while r < n && self.buf[r] != b' ' {
            r += 1;
        }
        let res = match str::from_utf8(&self.buf[l..r]).ok().unwrap().parse::<T>() {
            Ok(s) => Some(s),
            Err(_) => panic!("parse error"),
        };
        while r < n && self.buf[r] == b' ' {
            r += 1;
        }
        if r == n {
            self.buf.clear();
        } else {
            self.id = r;
        }
        res
    }
    fn ne<T: FromStr>(&mut self) -> T {
        self.next::<T>().unwrap()
    }
}

Submission Info

Submission Time
Task B - Boxes
User mio_h1917
Language Rust (1.15.1)
Score 500
Code Size 2553 Byte
Status AC
Exec Time 14 ms
Memory 3196 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 30
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in3.txt, in4.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt
Case Name Status Exec Time Memory
in1.txt AC 11 ms 2556 KB
in10.txt AC 3 ms 508 KB
in11.txt AC 12 ms 3196 KB
in12.txt AC 13 ms 3068 KB
in13.txt AC 11 ms 2684 KB
in14.txt AC 3 ms 380 KB
in15.txt AC 3 ms 380 KB
in16.txt AC 3 ms 380 KB
in17.txt AC 3 ms 380 KB
in18.txt AC 3 ms 380 KB
in19.txt AC 3 ms 380 KB
in2.txt AC 13 ms 3196 KB
in20.txt AC 2 ms 380 KB
in21.txt AC 14 ms 3068 KB
in22.txt AC 14 ms 3068 KB
in23.txt AC 14 ms 3068 KB
in24.txt AC 3 ms 508 KB
in25.txt AC 3 ms 508 KB
in26.txt AC 12 ms 3068 KB
in27.txt AC 12 ms 3068 KB
in3.txt AC 13 ms 3196 KB
in4.txt AC 13 ms 3196 KB
in5.txt AC 12 ms 3196 KB
in6.txt AC 13 ms 3196 KB
in7.txt AC 13 ms 3196 KB
in8.txt AC 3 ms 508 KB
in9.txt AC 3 ms 508 KB
sample1.txt AC 3 ms 380 KB
sample2.txt AC 3 ms 380 KB
sample3.txt AC 3 ms 380 KB