Submission #1662381


Source Code Expand

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

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

impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { 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()
    }
}

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

    while let Some(s) = sc.read1::<i64>() {
        let c: i64 = sc.read();

        // (s, c) -> (s + a, c - 2a) -> min(s + a, (c - 2a) // 2)
        // s + a = (c - 2a) // 2 <=> a = c/4 - s/2
        let mut res: i64 = 0;
        for b in -10..10 {
            let mut a = c / 4 - s / 2 + b;
            a = cmp::max(a, 0);
            a = cmp::min(a, c / 2);
            let cand = cmp::min(s + a, (c - 2 * a) / 2);
            res = cmp::max(res, cand);
        }
        println!("{}", res);
    }
}

Submission Info

Submission Time
Task C - Scc Puzzle
User ir5
Language Rust (1.15.1)
Score 0
Code Size 1244 Byte
Status CE

Compile Error

error: struct field shorthands are unstable (see issue #37340)
  --> ./Main.rs:11:19
   |
11 |         Scanner { cin }
   |                   ^^^

error: aborting due to previous error