Skip to content

Instantly share code, notes, and snippets.

@louy2
Last active March 19, 2020 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louy2/832006947d5fb4ea6fb267021faa9573 to your computer and use it in GitHub Desktop.
Save louy2/832006947d5fb4ea6fb267021faa9573 to your computer and use it in GitHub Desktop.
Rust solution to Paiza Online Hackathon Lite 4 ending
-- https://paiza.jp/poh/enkoi-ending/ed57428d
-- しゃくとり法
import Data.List
main = do
t:_ <- map read . words <$> getLine
interact $ show . solve t . map read . lines
solve :: Int -> [Int] -> Int
solve t xs = maximum $ solve' t xs
solve' :: Int -> [Int] -> [Int]
solve' t xs = scanl' go (sum (take t xs)) (zip xs (drop t xs))
where
go s (l, h) = s - l + h
// https://paiza.jp/poh/enkoi-ending/8e85234f
// 累積和
use std::io::BufRead;
fn main() {
let stdin = std::io::stdin();
let mut l = stdin.lock().lines().map(Result::unwrap);
let t: usize = {
let first_line = l.next().unwrap();
let mut tmp = first_line.split(" ").map(|x| x.parse().unwrap());
tmp.next().unwrap()
};
let m: Vec<i64> = l.map(|x| x.parse().unwrap()).collect();
let accum: Vec<i64> = [0].iter().chain(m.iter()).scan(0, |sum, &x| { *sum += x; Some(*sum) }).collect();
let result: i64 = accum.windows(t + 1).map(|w| w[t] - w[0]).max().unwrap();
println!("{}", result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment