Last active
August 29, 2015 14:11
-
-
Save omas/146e63483c331ae1229e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/perl | |
| #2014/12/09 尺取り虫とか累積和とかよーわからんのじゃー | |
| #2014/12/13 累積和 version by Omas | |
| use strict; | |
| use warnings; | |
| my @info = split(/ /,<STDIN>); | |
| my @line = <STDIN>; | |
| my @cum_sum = &cumsum(@line[0..$info[1] - 1]); | |
| print(&secmax(\@cum_sum,\@info)); | |
| sub cumsum { | |
| my @cumsum; | |
| my $sum = 0; | |
| push(@cumsum,$sum += $_) for @_; | |
| return @cumsum; | |
| } | |
| sub secmax { | |
| my ($cumsum,$info) = @_; | |
| my ($seclen,$len) = ($$info[0],$$info[1]); | |
| my ($head,$tail) = (0,$seclen); | |
| my ($secsum,$secmax) = (0,$$cumsum[$tail - 1]); | |
| while($tail < $len) { | |
| $secsum = $$cumsum[$tail++] - $$cumsum[$head++]; | |
| $secmax = $secsum if $secsum > $secmax; | |
| } | |
| return $secmax; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment