ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1061. Buffer Manager

I got AC!!!(0.04 and 225K)
Posted by Sergeyev Alexander 13 Sep 2003 16:02
var
  c:char;
  a:array[1..100000] of integer;
  cyc1,cyc2,n,k,m,s,sm,i:longint;
begin
  readln(n,m);
  cyc1:=0;
  while (not eof(input))and(cyc1<=n) do
   begin
     while (not eoln(input))and(cyc1<=n) do
       begin
         read(c);
         inc(cyc1);
         if c='*' then a[cyc1]:=-1 else a[cyc1]:=ord(c)-ord('0');
       end;
     readln;
     end;
   k:=0;
   s:=0;
   i:=0;
   sm:=100000;
   for cyc1:= 1 to n do
     begin
       if a[cyc1]=-1 then
         begin
           s:=0;k:=0;
         end
           else
        begin
          if k<m then
            begin
              inc(k);
              s:=s+a[cyc1];
              if k=m then
                begin
                  if sm>s then
                    begin sm:=s; i:=cyc1-m+1;end;
                end;
            end
            else
             begin
               s:=s+a[cyc1]-a[cyc1-m];
               if sm>s then begin sm:=s;i:=cyc1-m+1;end;

             end;
        end;
     end;
   writeln(i);
end.
Re: I got AC!!!(0.04 and 225K)
Posted by Dilyan 16 Jun 2005 18:37
try using an array only for the last K elements
Re: I got AC!!!(0.04 and 225K)
Posted by Lurker 31 Jul 2006 19:21
0.031 242 KB
Program Buffer_Manager;
 Var
  A:array[0..10000] of integer;
  bj:boolean;
  N,K,c,s,i,mi,t,b:Longint;
  min:longint;
  ch:char;
 Begin
  Readln(n,k);
  min:=1000000;
  while (b<n)and(not(eof(input))) do begin
   while (b<n)and (not(eoln(input))) do begin
    Read(ch);inc(b);inc(i);
    if i>k then i:=1;
    t:=a[i];
    if ch='*' then A[i]:=-1 Else A[i]:=ord(ch)-48;
    if a[i]=-1 then begin
     c:=0;s:=0;
    End Else if c=k then begin
     S:=s-t+a[i];
     if s<min then begin
      min:=s;mi:=b-k+1;
     End;
    End Else begin
     inc(c);s:=s+a[i];
     if c=k then if s<min then begin min:=s;mi:=b-k+1;End;
    End;
   End;
   Readln;
  End;
  writeln(mi);
 End.
Re: I got AC!!!(0.04 and 225K)
Posted by AlainDelon 28 Jun 2008 17:01
Just one feeling,

Always found that most guys are very smart, but the code they write/post are quite terrible(I mean hard to read). I would recommend writing readable code with clear structure and comments as that's the rule in real world( long code with clean structure and readability is much better than urgly code with small size).
Re: I got AC!!!(0.04 and 225K)
Posted by Irina Kruglova 14 Jul 2008 23:44

Edited by author 05.08.2008 13:41

Edited by author 05.08.2008 13:41