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 1154. Mages Contest

What is wrong with my code?
Posted by Koala 23 Feb 2003 08:57
program p1154;

const
  zero=1e-5;
  allday=24*60*60;
  maxnum=1e+16;

var
  t:array [1..4,1..4] of longint;
  p:array [1..4,1..4] of real;
  g:array [1..2,1..4] of longint;
  now:array [1..4] of real;
  delta:array [1..4,1..3] of real;
  power:array [1..2] of real;
  pp,j,t1,t2,i,time,anstime,h,m,s:longint;
  p1,p2,ans,advantage:real;
  ch:char;

  function change(ch1,ch2:char):longint;
  begin
    change:=(ord(ch1)-ord('0'))*10+(ord(ch2)-ord('0'));
  end;

  procedure readdata(var t:longint; var p:real);
  var
    ch,ch1:char;
    h,m,s:longint;
  begin
    read(ch); while ch=' ' do read(ch); read(ch1);
    h:=change(ch,ch1);
    read(ch); read(ch); read(ch1);
    m:=change(ch,ch1);
    read(ch); read(ch); read(ch1);
    s:=change(ch,ch1);
    t:=h*3600+m*60+s; read(p);
  end;

  procedure print(a:longint);
  begin
    if a<10 then write(0);
    write(a);
  end;

begin
  for pp:=1 to 4 do
  begin
    read(ch);
    j:=0;
    case ch of
      'A': j:=1;
      'E': j:=2;
      'F': j:=3;
      'W': j:=4;
    end;
    readdata(t1,p1); {Power}
    readdata(t2,p2); {Weakness}
    readln;
    if t1<t2
      then begin
        t[j,1]:=t2-allday; p[j,1]:=p2;
        t[j,2]:=t1; p[j,2]:=p1;
        t[j,3]:=t2; p[j,3]:=p2;
        t[j,4]:=t1+allday; p[j,4]:=p1;
      end
      else begin
        t[j,1]:=t1-allday; p[j,1]:=p1;
        t[j,2]:=t2; p[j,2]:=p2;
        t[j,3]:=t1; p[j,3]:=p1;
        t[j,4]:=t2+allday; p[j,4]:=p2;
      end
  end;

  fillchar(g,sizeof(g),0);
  for i:=1 to 2 do
  begin
    while not eoln do
    begin
      read(ch);
      case ch of
        'A': inc(g[i,1]);
        'E': inc(g[i,2]);
        'F': inc(g[i,3]);
        'W': inc(g[i,4]);
      end;
    end;
    readln;
  end;

  for i:=1 to 4 do
    for j:=1 to 3 do
      delta[i,j]:=(p[i,j+1]-p[i,j])/(t[i,j+1]-t[i,j]);

  ans:=-maxnum; anstime:=0;
  for time:=0 to allday-1 do
  begin
    for i:=1 to 4 do
      for j:=1 to 3 do
        if (t[i,j]<time) and (time<=t[i,j+1])
          then now[i]:=p[i,j]+delta[i,j]*(time-t[i,j]);
    for i:=1 to 2 do
    begin
      power[i]:=0;
      for j:=1 to 4 do
        power[i]:=power[i]+g[i,j]*now[j];
    end;
    advantage:=power[1]-power[2];
    if advantage>ans+zero then
    begin
      ans:=advantage;
      anstime:=time;
    end;
  end;

  if ans<0
    then writeln('We can''t win!')
    else begin
      h:=anstime div 3600;
      m:=(anstime div 60) mod 60;
      s:=anstime mod 60;
      print(h); write(':'); print(m); write(':'); print(s); writeln;
      writeln(ans:0:2);
    end;
end.
i don't know what's wrong with you, my program make the same answer of you, need help!
Posted by BShell 30 Apr 2003 18:14
const ch:array[1..4] of char=('A','E','F','W');
      maxtime=24*60*60;
      e=1e-20;

type elem=array[1..4] of longint;

var a:elem;
    d,f:array[1..4,1..2] of longint;
    l,g:array[1..4] of longint;
    s:string;
    i,j,flag:longint;
    ans,x:extended;

function find(k:char):integer;
var i:integer;
begin
  for i:=1 to 4 do
  if ch[i]=k then exit(i);
end;

function getch:char;
var ch:char;
begin
  read(ch);
  while ch=' ' do read(ch);
  getch:=ch;
end;

function getnext:longint;
var now,i,x:longint;
    ch:char;
begin
  now:=0;
  for i:=1 to 3 do
  begin
    ch:=getch;
    x:=ord(ch)-ord('0');
    ch:=getch;
    x:=x*10+ord(ch)-ord('0');
    now:=now+x;
    now:=now*60;
    if i<3 then ch:=getch;
  end;
  getnext:=now div 60;
end;

procedure readin;
var ch:char;
    t,x,max,min,i:longint;
begin
  read(ch);
  i:=find(ch);
  x:=getnext;
  read(max);
  f[i,1]:=x;
  x:=getnext;
  readln(min);
  f[i,2]:=x;
  if f[i,1]>f[i,2] then
  begin
    t:=f[i,1];
    f[i,1]:=f[i,2];
    f[i,2]:=t;
    d[i,1]:=min;
    d[i,2]:=max;
    g[i]:=(max-min);
    l[i]:=f[i,2]-f[i,1];
  end
  else begin
    g[i]:=min-max;
    l[i]:=f[i,2]-f[i,1];
    d[i,1]:=max;
    d[i,2]:=min;
  end;
end;

procedure print(k:longint);
var x:longint;
begin
  x:=k div 3600;
  if x<10 then write(0);
  write(x,':');
  k:=k mod 3600;
  x:=k div 60;
  if x<10 then write(0);
  write(x,':');
  k:=k mod 60;
  if k<10 then write(0);
  writeln(k);
  writeln(ans:0:2);
end;

function calc(k:longint):extended;
var i,t:longint;
    ans,x:extended;

begin
  ans:=0;
  for i:=1 to 4 do
  if a[i]<>0 then
  begin
    if (f[i,1]<=k) and (k<=f[i,2]) then
      x:=d[i,1]+(k-f[i,1])*g[i]/l[i]
    else begin
      if k>f[i,2] then t:=k-f[i,2]
      else t:=(maxtime-f[i,2])+k;
      x:=d[i,2]-t*g[i]/(maxtime-l[i]);
    end;
    ans:=ans+x*a[i];
  end;
  calc:=ans;
end;

begin
  for i:=1 to 4 do
    readin;
  fillchar(a,sizeof(a),0);
  readln(s);
  for i:=1 to length(s) do
    inc(a[find(s[i])]);
  readln(s);
  for i:=1 to length(s) do
    dec(a[find(s[i])]);
  ans:=-1e20;
  for i:=1 to 4 do
    for j:=1 to 2 do
    begin
      x:=calc(f[i,j]);
      if (x>ans) or (x=ans) and (f[i,j]<flag) then
      begin
        ans:=x;
        flag:=f[i,j];
      end;
    end;
  x:=calc(0);
  if x>ans then
  begin
    ans:=x;
    flag:=0;
  end;
  if ans<=e then writeln('We can''t win!')
  else print(flag);
end.