• vj p1037题解


    原题叙述

    这个题怎么说呢,还很能唬人的....

    写了个很丑的背包....
    不过秒掉了!
    -------------------------------------
    编译通过...
    ├ 测试数据 01:答案正确... 0ms
    ├ 测试数据 02:答案正确... 0ms
    ├ 测试数据 03:答案正确... 0ms
    ├ 测试数据 04:答案正确... 0ms
    ├ 测试数据 05:答案正确... 0ms
    ├ 测试数据 06:答案正确... 0ms
    ├ 测试数据 07:答案正确... 0ms
    ├ 测试数据 08:答案正确... 0ms
    ├ 测试数据 09:答案正确... 0ms
    ├ 测试数据 10:答案正确... 0ms
    -------------------------
    Accepted 有效得分:100 有效耗时:0ms

    题解:

    f[i,j]表示前i个数搭建的塔的差为j时的最大高度

    v[i,j]为boolean数组,前i个数能否到达j的高度

    动态方程:

    f[i,j]:=max(f[i-1,j-a[i]]+a[i],f[i,j]);(if v[i-1,j-a[i]])

    f[i,j]:=max(f[i-1,j+a[i]],f[i,j]);(if v[i-1,j+a[i]])

    f[i,j]:=max(f[i-1,j],f[i,j]);if v[i-1,j+a[i]]

    代码如下: 


     1 var n,all,i,j:longint;
     2     t:boolean;
     3     s,a:array[0..100]of longint;
     4     f:array[boolean,-2100..2100]of longint;
     5     v:array[boolean,-2100..2100]of boolean;
     6 function max(a,b,c:longint):longint;
     7 begin
     8      max:=a;
     9      if b>max then max:=b;
    10      if c>max then max:=c;
    11 end;
    12 begin
    13     
    14      readln(n);
    15      all:=0;
    16      s[0]:=0;
    17      for i:=1 to n do
    18          begin
    19               read(a[i]);
    20               s[i]:=s[i-1]+a[i];
    21          end;
    22      fillchar(f,sizeof(f),0);
    23      fillchar(v,sizeof(v),0);
    24      t:=true;
    25      v[false,0]:=true;
    26      v[true,0]:=true;
    27      for i:=1 to n do
    28          begin
    29               for j:=-s[i] to s[i] do
    30                   begin
    31                        f[t,j]:=0;v[t,j]:=false;
    32                        if v[not(t),j+a[i]] then
    33                           begin
    34                                f[t,j]:=f[not(t),j+a[i]];
    35                                v[t,j]:=true;
    36                           end;
    37                        if (v[not(t),j-a[i]])and(f[not(t),j-a[i]]+a[i]>=f[t,j]) then
    38                           begin
    39                                f[t,j]:=f[not(t),j-a[i]]+a[i];
    40                                v[t,j]:=true;
    41                           end;
    42                        if (v[not(t),j])and(f[not(t),j]>=f[t,j]) then
    43                           begin
    44                                f[t,j]:=f[not(t),j];
    45                                v[t,j]:=true;
    46                           end;
    47 //                       write(j,':',f[t,j],' ');
    48                   end;
    49 //              writeln;
    50               t:=not(t);
    51          end;
    52      if v[true,0and v[false,0then
    53         begin
    54              if f[true,0]>f[false,0then
    55                 begin
    56                      if f[true,0]=0 then writeln('Impossible'else writeln(f[true,0]);
    57                 end
    58              else
    59                  begin
    60                       if f[false,0]=0 then writeln('Impossible')
    61                       else writeln(f[false,0]);
    62                  end;
    63         end
    64      else
    65          if (v[true,0])and(f[true,0]<>0then writeln(f[true,0])
    66          else
    67              if (v[false,0])and(f[true,0]<>0then writeln(f[false,0])
    68              else
    69                  writeln('Impossible');
    70 end.
  • 相关阅读:
    关于<form>标签
    javaEE学习随笔
    类与接口
    java学习中的一些总结
    java 对象的创建
    jQuery选择器
    CSS学习随笔
    JDBC笔记 二
    Java EE笔记 (1) JDBC
    泛型笔记
  • 原文地址:https://www.cnblogs.com/waterfalleagle/p/1601206.html
Copyright © 2020-2023  润新知