complete 1725.rs
This commit is contained in:
42
storage/zeta/rs/completed/1725.rs
Normal file
42
storage/zeta/rs/completed/1725.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use std::io::{read_to_string, stdin};
|
||||
|
||||
fn main() {
|
||||
let temp = read_to_string(stdin()).unwrap();
|
||||
let mut iter = temp
|
||||
.split_ascii_whitespace()
|
||||
.map(|x| x.parse::<usize>().unwrap());
|
||||
|
||||
let n = iter.next().unwrap();
|
||||
let mut arr = (0..n).map(|_| iter.next().unwrap()).collect::<Vec<_>>();
|
||||
|
||||
let mut stack = vec![(0, 0)];
|
||||
let mut res = 0;
|
||||
for i in 1..=n {
|
||||
let e = arr[i - 1];
|
||||
let (mut top_i, mut top_e) = *stack.last().unwrap();
|
||||
|
||||
while top_e >= e {
|
||||
let (p_i, p_e) = stack.pop().unwrap();
|
||||
if stack.is_empty() {
|
||||
break;
|
||||
}
|
||||
(top_i, top_e) = *stack.last().unwrap();
|
||||
res = res.max((i - top_i - 1) * p_e);
|
||||
|
||||
}
|
||||
if stack.is_empty() {
|
||||
stack.push((i, e));
|
||||
continue;
|
||||
}
|
||||
stack.push((i, e));
|
||||
res = res.max((i - top_i) * e);
|
||||
}
|
||||
while stack.len() > 1 {
|
||||
let (ti, te) = stack.pop().unwrap();
|
||||
let (top_i, top_e) = stack.last().unwrap();
|
||||
res = res.max((n - top_i) * te);
|
||||
|
||||
}
|
||||
|
||||
println!("{}", res);
|
||||
}
|
||||
Reference in New Issue
Block a user