Browse files

modify nodas

  • Loading branch information...
1 parent 68e34e9 commit 8a8f404aa0799a75b5092d34240396abd9e2d13f @libitte committed Dec 15, 2013
Showing with 88 additions and 8 deletions.
  1. +8 −8 noda2.pl
  2. +80 −0 noda3.pl
View
16 noda2.pl
@@ -58,19 +58,19 @@ sub get_max_pair {
my $max_index = scalar(@n) - 1;
my $candidates;
- #for my $i (0 .. $max_index) {
- # my $x = $n[$i];
- # splice(@n, $i, 1);
- # push(@$candidates, _max([grep { $_ <= $m_j } map { $x + $_ } @n]));
- # splice(@n, $i, 0, $x);
- #}
- my $indexes = [0 .. $max_index];
- while (defined(my $i = shift(@$indexes))) {
+ for my $i (0 .. $max_index) {
my $x = $n[$i];
splice(@n, $i, 1);
push(@$candidates, _max([grep { $_ <= $m_j } map { $x + $_ } @n]));
splice(@n, $i, 0, $x);
}
+ #my $indexes = [0 .. $max_index];
+ #while (defined(my $i = shift(@$indexes))) {
+ # my $x = $n[$i];
+ # splice(@n, $i, 1);
+ # push(@$candidates, _max([grep { $_ <= $m_j } map { $x + $_ } @n]));
+ # splice(@n, $i, 0, $x);
+ #}
return _max($candidates);
}
View
80 noda3.pl
@@ -0,0 +1,80 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use utf8;
+binmode(STDOUT, ":utf8");
+
+# parse args
+#----------------
+
+my $N_D = <STDIN>;
+chomp($N_D);
+my @N_D = split(/ /, $N_D);
+
+my $N = $N_D[0];
+my $D = $N_D[1];
+#printf("*** N:%d, D:%d ***\n", $N_D[0], $N_D[1]);
+
+my @p_N;
+my $i = 0;
+while (my $p_i = <STDIN>) {
+ chomp($p_i);
+ push(@p_N, $p_i);
+ $i++;
+ last if $i == $N;
+}
+#print("*** @p_N ***\n");
+
+my $j = 0;
+while (my $m_j = <STDIN>) {
+ chomp($m_j);
+
+ print(get_max_pair($m_j, \@p_N), "\n");
+ #printf("*** campaign price/max pair price:%d/%d ***\n", $m_j, get_max_pair($m_j, @p_N));
+
+ $j++;
+ last if $j == $D;
+}
+#print("*** @m_D ***\n");
+
+sub get_max_pair {
+ my $m_j = shift;
+ my $p_N = shift;
+
+ # reduce useless elements
+ #my @n = sort { $a <=> $b } grep { $_ < $m_j } @p_N;
+ my @n = grep { $_ < $m_j } @$p_N;
+
+ return 0 unless(@n);
+
+ my $dest_index = scalar(@n) - 1;
+
+ my $max = 0;
+ for my $i (0 .. $dest_index) {
+ for my $j ($i+1 .. $dest_index) {
+ my $sum = $n[$i] + $n[$j];
+ next if $sum > $m_j;
+ $max = $sum if $sum >= $max;
+ }
+ }
+
+ return $max;
+}
+
+#sub _max {
+# my ($n) = @_;
+# my @ns = sort { $b <=> $a } @$n;
+# return shift(@ns);
+#}
+
+#sub sum {
+# my ($s) = @_;
+# my $ret = 0;
+# for (@$s) {
+# $ret += $_;
+# }
+# return $ret;
+#}
+
+

0 comments on commit 8a8f404

Please sign in to comment.