First, I see no reason to avoid strict and warnings. The only overhead it adds is the clearing of the variables in each loop pass. If that's an issue, you can avoid the clearing by declaring the vars outside the loops.

IO carries a lot of overhead. Do it all at once. (The python player probably did the same thing, thus the high memory usage.)

#!/usr/bin/perl use strict; use warnings; my (@file, @tr, $prev, $cur, $f, $g); { local $/; @file = split ' ', scalar(<>); } for (1..shift(@file)) { ($prev, @tr) = reverse map [ splice(@file, 0, $_) ], 1..shift(@file); for $cur (@tr) { $g = $prev->[0]; for (0 .. $#$cur) { $cur->[$_] += ($f = $g) > ($g = $prev->[$_ + 1]) ? $f : $g; } $prev = $cur; } print "$prev->[0]\n"; }

And then, let's avoid building an AoA. It saves on building time, memory costs, and avoids the need to deref. Note that splicing/poping/shifting from the ends of an array is very efficient.

#!/usr/bin/perl use strict; use warnings; my (@file, $rows, @tr, @prev, @cur, $f, $g); { local $/; @file = split ' ', scalar(<>); } for (1..shift(@file)) { $rows = shift(@file); @tr = splice(@file, 0, $rows*($rows+1)/2); @prev = splice(@tr, -($rows--)); while ($rows) { @cur = splice(@tr, -($rows--)); $g = $prev[0]; for (0 .. $#cur) { $cur[$_] += ($f = $g) > ($g = $prev[$_ + 1]) ? $f : $g; } @prev = @cur; } print "$prev[0]\n"; }

It might be faster to calculate the index into @tr instead of copying the values into in and out. But maybe not. The hindered readability and the extra additions might cancel out the benefit.

#!/usr/bin/perl use strict; use warnings; my (@file, $rows, @tr, $prev, $cur, $f, $g); { local $/; @file = split ' ', scalar(<>); } for (1..shift(@file)) { $rows = shift(@file); @tr = splice(@file, 0, $rows*($rows+1)/2); $prev = @tr - $rows--; while ($rows) { $cur = $prev - $rows--; $g = $tr[$prev]; for (0 .. $rows) { $tr[$cur + $_] += ($f = $g) > ($g = $tr[$prev + $_ + 1]) ? $f : $g; } $prev = $cur; } print "$tr[0]\n"; }

You could do something similar to avoid creating @tr.

By the way, ($f = $g) > ($g = $prev->[$_ + 1]) is not guaranteed to be evaluated in the order in which you want it to be evaluated. It's technically a bug since operand evaluation order is undefined (although predictable and unlikely to change) in Perl.


In reply to Re: Adding a lot of small integers by ikegami
in thread Adding a lot of small integers by kappa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.