#![allow(unused_macros)] #![allow(dead_code)] #![allow(unused_imports)] // # ファイル構成 // - use宣言 // - libモジュール // - main関数 // - basicモジュール // // 常に使うテンプレートライブラリはbasicモジュール内にあります。 // 問題に応じて使うライブラリlibモジュール内にコピペしています。 // ライブラリのコードはこちら → https://github.com/RheoTommy/at_coder // Twitterはこちら → https://twitter.com/RheoTommy use std::collections::*; use std::io::{stdout, BufWriter, Write}; use crate::basic::*; use crate::lib::*; pub mod lib {} fn main() { let out = stdout(); let mut writer = BufWriter::new(out.lock()); let mut sc = Scanner::new(); let n = sc.next_usize(); let a = (0..n).map(|_| sc.next_int()).collect::>(); let mut cnt = 0; let mut r = 1; let mut map = BTreeMap::new(); *map.entry(a[0]).or_insert(0) += 1; *map.entry(a[1]).or_insert(0) += 1; for l in 0..n - 1 { while { let mut iter = map.iter().flat_map(|(k, v)| (0..*v).map(move |_| *k)); let m1 = iter.next().unwrap(); let m2 = iter.next().unwrap(); let m = iter.next_back().unwrap_or(m2); // writeln!(writer, "{:?}", (l, r, m1 + m2 >= m && r < n)); m1 + m2 >= m && r < n } { r += 1; if r < n { *map.entry(a[r]).or_insert(0) += 1; } } cnt += r - l - 1; *map.entry(a[l]).or_insert(0) -= 1; if map[&a[l]] == 0 { map.remove(&a[l]); } // writeln!(writer, "{:?}", (l, r)); } writeln!(writer, "{}", cnt).unwrap(); } pub mod basic { pub const U_INF: u64 = 1 << 60; pub const I_INF: i64 = 1 << 60; pub struct Scanner { buf: std::collections::VecDeque, reader: std::io::BufReader, } impl Scanner { pub fn new() -> Self { Self { buf: std::collections::VecDeque::new(), reader: std::io::BufReader::new(std::io::stdin()), } } fn scan_line(&mut self) { use std::io::BufRead; let mut flag = 0; while self.buf.is_empty() { let mut s = String::new(); self.reader.read_line(&mut s).unwrap(); let mut iter = s.split_whitespace().peekable(); if iter.peek().is_none() { if flag >= 5 { panic!("There is no input!"); } flag += 1; continue; } for si in iter { self.buf.push_back(si.to_string()); } } } pub fn next(&mut self) -> T { self.scan_line(); self.buf .pop_front() .unwrap() .parse() .unwrap_or_else(|_| panic!("Couldn't parse!")) } pub fn next_usize(&mut self) -> usize { self.next() } pub fn next_int(&mut self) -> i64 { self.next() } pub fn next_uint(&mut self) -> u64 { self.next() } pub fn next_chars(&mut self) -> Vec { self.next::().chars().collect() } pub fn next_string(&mut self) -> String { self.next() } pub fn next_char(&mut self) -> char { self.next() } pub fn next_float(&mut self) -> f64 { self.next() } } }