|
|
вернуться в форум1003:My program uses about only 100K, but on the judge machine, it needs more than 1000K. Why? program Timus_1003; const MaxQAs=5000; EndFlag=-1; type TQA= record First,Last:LongInt; Answer:Boolean; end; var nQAs,X:LongInt; QAs:array [1..MaxQAs] of TQA; function InputData:Boolean; var i,Len:LongInt; Temp:string; begin ReadLn(Len); if Len=EndFlag then begin InputData:=False; Exit; end; ReadLn(nQAs); for i:=1 to nQAs do with QAs[i] do begin ReadLn(First,Last,Temp); while Temp[1]=' ' do Delete(Temp,1,1); Answer:=Temp='odd'; end; InputData:=True; end; procedure Solve; var i,nItems:LongInt; Items:array [1..MaxQAs] of TQA; function Insert(QA:TQA):Boolean; var i,MaxLast,MinLast:LongInt; MaxAnswer,MinAnswer:Boolean; Item:TQA; function FindItem(First:LongInt):LongInt; var i,j,k:LongInt; begin i:=1; j:=nItems; repeat k:=(i+j) div 2; if Items[k].First=First then begin FindItem:=k; Exit; end else if Items[k].First>First then j:=k-1 else i:=k+1; until i>j; FindItem:=0; end; procedure InsertItem(Item:TQA); var i,j:LongInt; begin for i:=1 to nItems do if Items[i].First>Item.First then begin for j:=nItems downto i do Items[j+1]:=Items[j]; Inc(nItems); Items[i]:=Item; Exit; end; Inc(nItems); Items[nItems]:=Item; end; function Subtract(A,B:Boolean):Boolean; begin Subtract:=A xor B; end; begin repeat i:=FindItem(QA.First); if i=0 then begin InsertItem(QA); Insert:=True; Exit; end else if Items[i].Last=QA.Last then begin Insert:=Items[i].Answer=QA.Answer; Exit; end else begin if Items[i].Last>QA.Last then begin MaxLast:=Items[i].Last; MinLast:=QA.Last; MaxAnswer:=Items[i].Answer; MinAnswer:=QA.Answer; end else begin MaxLast:=QA.Last; MinLast:=Items[i].Last; MaxAnswer:=QA.Answer; MinAnswer:=Items[i].Answer; end; with Items[i] do begin Last:=MinLast; Answer:=MinAnswer; end; with Item do begin First:=MinLast+1; Last:=MaxLast; Answer:=Subtract(MaxAnswer,MinAnswer); end; QA:=Item; end; until False; end; begin nItems:=0; FillChar(Items,SizeOf(Items),0); for i:=1 to nQAs do if not Insert(QAs[i]) then begin X:=i-1; Exit; end; X:=nQAs; end; procedure Print; begin WriteLn(X:0); end; begin while InputData do begin Solve; Print; end; end. This is a Delphi compiler ^))) > program Timus_1003; > const > MaxQAs=5000; > EndFlag=-1; > type > TQA= > record > First,Last:LongInt; > Answer:Boolean; > end; > var > nQAs,X:LongInt; > QAs:array [1..MaxQAs] of TQA; > function InputData:Boolean; > var > i,Len:LongInt; > Temp:string; > begin > ReadLn(Len); > if Len=EndFlag then > begin > InputData:=False; > Exit; > end; > ReadLn(nQAs); > for i:=1 to nQAs do > with QAs[i] do > begin > ReadLn(First,Last,Temp); > while Temp[1]=' ' do > Delete(Temp,1,1); > Answer:=Temp='odd'; > end; > InputData:=True; > end; > procedure Solve; > var > i,nItems:LongInt; > Items:array [1..MaxQAs] of TQA; > function Insert(QA:TQA):Boolean; > var > i,MaxLast,MinLast:LongInt; > MaxAnswer,MinAnswer:Boolean; > Item:TQA; > function FindItem(First:LongInt):LongInt; > var > i,j,k:LongInt; > begin > i:=1; > j:=nItems; > repeat > k:=(i+j) div 2; > if Items[k].First=First then > begin > FindItem:=k; > Exit; > end > else > if Items[k].First>First then > j:=k-1 > else > i:=k+1; > until i>j; > FindItem:=0; > end; > procedure InsertItem(Item:TQA); > var > i,j:LongInt; > begin > for i:=1 to nItems do > if Items[i].First>Item.First then > begin > for j:=nItems downto i do > Items[j+1]:=Items[j]; > Inc(nItems); > Items[i]:=Item; > Exit; > end; > Inc(nItems); > Items[nItems]:=Item; > end; > function Subtract(A,B:Boolean):Boolean; > begin > Subtract:=A xor B; > end; > begin > repeat > i:=FindItem(QA.First); > if i=0 then > begin > InsertItem(QA); > Insert:=True; > Exit; > end > else > if Items[i].Last=QA.Last then > begin > Insert:=Items[i].Answer=QA.Answer; > Exit; > end > else > begin > if Items[i].Last>QA.Last then > begin > MaxLast:=Items[i].Last; > MinLast:=QA.Last; > MaxAnswer:=Items[i].Answer; > MinAnswer:=QA.Answer; > end > else > begin > MaxLast:=QA.Last; > MinLast:=Items[i].Last; > MaxAnswer:=QA.Answer; > MinAnswer:=Items[i].Answer; > end; > with Items[i] do > begin > Last:=MinLast; > Answer:=MinAnswer; > end; > with Item do > begin > First:=MinLast+1; > Last:=MaxLast; > Answer:=Subtract(MaxAnswer,MinAnswer); > end; > QA:=Item; > end; > until False; > end; > begin > nItems:=0; > FillChar(Items,SizeOf(Items),0); > for i:=1 to nQAs do > if not Insert(QAs[i]) then > begin > X:=i-1; > Exit; > end; > X:=nQAs; > end; > procedure Print; > begin > WriteLn(X:0); > end; > begin > while InputData do > begin > Solve; > Print; > end; > end. > |
|
|