为什么第4组过不了

program newyearthing;

var i,j,n,now,t,f,wha:longint;

w:array[-1..900] of longint;

g,way:array[-1..132001] of longint;

bag,quence:array[-1..132000] of boolean;

begin

fillchar(bag,sizeof(bag),false);

fillchar(quence,sizeof(quence),true);

fillchar(way,sizeof(way),0);

read(now);

read(n);

t:=0;f:=0;

for i:=1 to n do

read(w[i]);

bag[0]:=true; way[0]:=1;

for i:=n downto 1 do

for j:=now-w[i] downto 0 do

if bag[j] then

begin

bag[w[i]+j]:=true;

way[w[i]+j]:=way[w[i]+j]+way[j];

g[w[i]+j]:=i;

end;

if not bag[now] then

write(0)

else

if way[now]1 then write(-1)

else begin

wha:=now;

while wha>0 do

begin

quence[g[wha]]:=false;

wha:=wha-w[g[wha]];

end;

t:=0;

for i:=n downto 1 do

if quence[i] then

break;

for j:=1 to n do

if (quence[j])and(ji) then

begin

write(j,' ')

end else

if j=i then

begin

writeln(j);

break;

end;

end;

end.

2 条评论

  • @ 2014-07-07 09:43:13

    const
    maxw=100010;
    maxn=110;
    var
    path,opt:array[0..maxw] of int64;
    w:array[0..maxn] of longint;
    ans:array[0..maxn] of boolean;
    n,total:longint;
    procedure init;
    var
    i:longint;
    begin
    read(total);
    read(n);
    for i:=1 to n do
    read(w[i]);
    end;
    procedure main;
    var
    i,j:longint;
    begin
    fillchar(opt,sizeof(opt),0);
    fillchar(ans,sizeof(ans),true);
    opt[0]:=1;
    for i:=1 to n do
    for j:=total downto w[i] do
    if opt[j-w[i]]>0 then
    begin
    if opt[j]=0 then
    path[j]:=i; {只有当前状态没求过才记录方案}
    inc(opt[j],opt[j-w[i]]);
    end;
    if opt[total]=0 then
    begin
    writeln('0');
    halt;
    end;
    if opt[total]>1 then
    begin
    writeln('-1');
    halt;
    end;
    i:=total;
    while i>0 do
    begin
    ans[path[i]]:=false;
    i:=i-w[path[i]];
    end;
    end;
    procedure print;
    var
    i:longint;
    begin
    for i:=1 to n do
    if ans[i] then write(i,' ');
    end;
    begin
    init;
    main;
    print;
    end.

  • @ 2009-09-05 10:48:53

    错了 程序是这样的

    program newyearthing;

    var i,j,n,now,t,f,wha:longint;

    w:array[-1..900] of longint;

    g,way:array[-1..132001] of longint;

    bag,quence:array[-1..132000] of boolean;

    begin

    fillchar(bag,sizeof(bag),false);

    fillchar(quence,sizeof(quence),true);

    fillchar(way,sizeof(way),0);

    read(now);

    read(n);

    t:=0;f:=0;

    for i:=1 to n do

    read(w[i]);

    bag[0]:=true; way[0]:=1;

    for i:=n downto 1 do

    for j:=now-w[i] downto 0 do

    if bag[j] then

    begin

    bag[w[i]+j]:=true;

    way[w[i]+j]:=way[w[i]+j]+way[j];

    g[w[i]+j]:=i;

    end;

    if not bag[now] then

    write(0)

    else

    if way[now]1 then write(-1)

    else begin

    wha:=now;

    while wha>0 do

    begin

    quence[g[wha]]:=false;

    wha:=wha-w[g[wha]];

    end;

    t:=0;

    for i:=n downto 1 do

    if quence[i] then

    break;

    for j:=1 to n do

    if (quence[j])and(ji) then

    begin

    write(j,' ')

    end else

    if (j=i)and(quence[i]) then

    begin

    writeln(j);

    break;

    end;

    end;

    end.

  • 1

信息

ID
1071
难度
7
分类
动态规划 | 背包 点击显示
标签
递交数
6302
已通过
1439
通过率
23%
被复制
13
上传者