Submission #1689974


Source Code Expand

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

fn solve0(xy: &Vec<(i64, i64)>) -> i64 {
    // fix rmin and bmax

    let n = xy.len();
    if n == 1 {
        return 0
    }

    let mut rmin = xy[0].0;
    let mut rmax = xy[0].0;
    let mut bmin = xy[0].1;
    let mut bmax = xy[0].1;

    let mut bi = 0;
    for i in 0..n {
        if bmax <= xy[i].1 {
            bmax = xy[i].1;
            bi = i;
        }
    }
    rmin = min(rmin, xy[bi].0);
    rmax = max(rmax, xy[bi].0);

    for i in 0..n {
        if i == 0 || i == bi {
            continue
        }
        bmin = min(bmin, xy[i].1);
        rmax = max(rmax, xy[i].0);
    }

    (rmax - rmin) * (bmax - bmin)
}

fn solve1(xy: &Vec<(i64, i64)>) -> i64 {
    // fix rmin and rmax
    let n = xy.len();
    if n == 1 {
        return 0
    }

    let mut rmin = xy[0].0;
    let mut rmax = xy[0].0;
    let mut bmin = xy[0].1;
    let mut bmax = xy[0].1;

    let mut bi = 0;
    for i in 1..n {
        if rmax <= xy[i].1 {
            rmax = xy[i].1;
            bi = i;
        }
    }
    bmin = min(bmin, xy[bi].0);
    bmax = max(bmax, xy[bi].0);

    if n == 2 {
        return (rmax - rmin) * (bmax - bmin)
    }

    let mut pairs = vec![];
    for i in 0..n {
        if i != 0 && i != bi {
            pairs.push(xy[i]);
        }
    }

    let pn = pairs.len();

    let mut right_min = 1e11 as i64;
    let mut right_max = 0;
    let mut best_blue = (0, 1e11 as i64);
    for i in 0..pn {
        let bmin = min(min(bmin, pairs[i].0), right_min);
        let bmax = max(max(bmax, right_max), pairs[pn-1].0);
        if best_blue.1 - best_blue.0 >= bmax - bmin {
            best_blue = (bmin, bmax);
        }
        right_min = min(right_min, pairs[i].1);
        right_max = max(right_max, pairs[i].1);
    }

    (rmax - rmin) * (best_blue.1 - best_blue.0)
}

fn main() {
    let n: usize = read();
    let mut xy: Vec<(i64, i64)> = readn(n);

    for i in 0..n {
        if xy[i].1 < xy[i].0 {
            xy[i] = (xy[i].1, xy[i].0);
        }
    }
    xy.sort();

    // println!("{:?}", xy);
    println!("{}", min(solve0(&xy), solve1(&xy)));
}

Submission Info

Submission Time
Task E - Ball Coloring
User hamadu
Language Rust (1.15.1)
Score 700
Code Size 3485 Byte
Status AC
Exec Time 81 ms
Memory 13624 KB

Compile Error

warning: variable does not need to be mutable, #[warn(unused_mut)] on by default
   --> ./Main.rs:103:9
    |
103 |     let mut rmin = xy[0].0;
    |         ^^^^^^^^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 35
Set Name Test Cases
Sample example0, example1, example2
All div20, div21, div22, div23, div24, example0, example1, example2, maxrand0, maxrand1, maxrand2, maxrand20, maxrand21, maxrand210, maxrand211, maxrand22, maxrand23, maxrand24, maxrand25, maxrand26, maxrand27, maxrand28, maxrand29, maxrand3, maxrand4, smallrand0, smallrand1, smallrand2, smallrand3, smallrand4, sparse0, sparse1, sparse2, sparse3, sparse4
Case Name Status Exec Time Memory
div20 AC 79 ms 13588 KB
div21 AC 80 ms 13588 KB
div22 AC 79 ms 13604 KB
div23 AC 79 ms 13624 KB
div24 AC 79 ms 13588 KB
example0 AC 2 ms 4352 KB
example1 AC 2 ms 4352 KB
example2 AC 2 ms 4352 KB
maxrand0 AC 79 ms 13624 KB
maxrand1 AC 79 ms 13624 KB
maxrand2 AC 79 ms 13624 KB
maxrand20 AC 80 ms 13624 KB
maxrand21 AC 81 ms 13624 KB
maxrand210 AC 81 ms 13604 KB
maxrand211 AC 80 ms 13496 KB
maxrand22 AC 81 ms 13624 KB
maxrand23 AC 80 ms 13624 KB
maxrand24 AC 79 ms 13604 KB
maxrand25 AC 81 ms 13624 KB
maxrand26 AC 80 ms 13624 KB
maxrand27 AC 81 ms 13624 KB
maxrand28 AC 81 ms 13588 KB
maxrand29 AC 80 ms 13588 KB
maxrand3 AC 79 ms 13428 KB
maxrand4 AC 79 ms 13604 KB
smallrand0 AC 2 ms 4352 KB
smallrand1 AC 2 ms 4352 KB
smallrand2 AC 2 ms 4352 KB
smallrand3 AC 2 ms 4352 KB
smallrand4 AC 2 ms 4352 KB
sparse0 AC 80 ms 13556 KB
sparse1 AC 80 ms 13624 KB
sparse2 AC 79 ms 13588 KB
sparse3 AC 79 ms 13604 KB
sparse4 AC 80 ms 13624 KB