这超时超的,编译器都出不来了,求优化方案

P1478 陶陶摘苹果(升级版)

CCCCCreeper @ 2020-02-25 20:39:03

program p1478;
var x,y:array [1..5000] of integer;
n,s,a,b,i,j,app:integer;
begin
  readln(n,s);
  readln(a,b);
  for i:=1 to n do readln(x[i],y[i]);
  app:=0;
  while s>=0 do
  begin
    j:=1;
    for i:=2 to n do if y[i]<y[j] then j:=i;
    if (s-y[j]>=0)and(a+b>=x[i]) then
    begin
      s:=s-y[j];
      app:=app+1;
      y[j]:=32767;
    end;
  end;
  write(app);
end.

by cstdios @ 2020-02-25 20:40:13

@s_TDY 都10202年了还有人用pascal!


by wjh_ @ 2020-02-25 20:42:24

谔谔


by btng_smith666 @ 2020-02-25 20:44:33

@s_TDY 现在这个年头都流行用wenyan


by CCCCCreeper @ 2020-02-25 20:47:38

跟不上时代了


by YUYGFGG @ 2020-02-25 20:50:03

@s_TDY 现在流行文言、东北话


by CBW2007 @ 2020-02-25 20:50:40

啊艹,熟悉的fpc,让我想起了lazarus和刚入坑OI学pascal的时候

我戏真多


by Heap_Sort @ 2020-02-25 21:27:05

一只P党来了


by Heap_Sort @ 2020-02-25 21:27:31

@s_TDY 您死循环了吧


by Heap_Sort @ 2020-02-25 21:30:27

 for i:=2 to n do if y[i]<y[j] then j:=i;

这句改成

 for i:=2 to n do if(y[i]<y[j])and(a+b>=x[i]) then j:=i;

by Heap_Sort @ 2020-02-25 21:43:30

这是我帮您全部改好后的代码

program p1478;
var x,y:array[0..5000] of integer;
n,s,a,b,i,j,app:integer;
begin
  readln(n,s);
  readln(a,b);
  for i:=1 to n do readln(x[i],y[i]);
  app:=0;
  y[0]:=32767;
  while s>=0 do
  begin
    j:=0;
    for i:=1 to n do if(y[i]<y[j])and(a+b>=x[i])then j:=i;
    if s-y[j]>=0 then
    begin
      s:=s-y[j];
      app:=app+1;
      y[j]:=32767;
    end
    else break;
  end;
  write(app);
end.

| 下一页