Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

搜索+小根堆。。。居然16ms

Posted by wukewen at 2013-12-13 00:04:40 on Problem 1018
没想到会有这么快。。。。
本人习惯堆排用dp。。。不要在意细节
写的略渣
。。。。。。。。。。。。。。。。。。。。。。。。
type
  arr=record
    num:longint;
    dp:array[1..110,1..2] of longint;
  end;
var
  a:array[0..110] of arr;
  b:array[0..11000,1..3] of longint;
  where:array[0..11000] of longint;
  n,i,j,k,l,s,m,x,y,sum,p,t:longint;
  v:double;
  f:boolean;
procedure swap(var a,b:longint);
  var
    c:longint;
  begin
    c:=a;
    a:=b;
    b:=c;
  end;
procedure up(t,x:longint);
  begin
  if x=1 then exit;
  with a[t] do
    if dp[x,1]<dp[x shr 1,1] then
       begin
       swap(where[dp[x,2]],where[dp[x shr 1,2]]);
       swap(dp[x,1],dp[x shr 1,1]);
       swap(dp[x,2],dp[x shr 1,2]);
       up(t,x shr 1);
       end;
  end;
procedure down(t,x:longint);
  begin
  with a[t] do
    begin
    if x shl 1>num then exit;
    if x shl 1=num then
       begin
       if dp[x,1]>dp[x shl 1,1] then
          begin
          swap(where[dp[x,2]],where[dp[x shl 1,2]]);
          swap(dp[x,1],dp[x shl 1,1]);
          swap(dp[x,2],dp[x shl 1,2]);
          end;
       exit;
       end;
    if dp[x shl 1,1]<dp[x shl 1+1,1] then
       begin
       if dp[x,1]>dp[x shl 1,1] then
          begin
          swap(where[dp[x,2]],where[dp[x shl 1,2]]);
          swap(dp[x,1],dp[x shl 1,1]);
          swap(dp[x,2],dp[x shl 1,2]);
          down(t,x shl 1);
          end;
       end
       else
       begin
       if dp[x,1]>dp[x shl 1+1,1] then
          begin
          swap(where[dp[x,2]],where[dp[x shl 1+1,2]]);
          swap(dp[x,1],dp[x shl 1+1,1]);
          swap(dp[x,2],dp[x shl 1+1,2]);
          down(t,x shl 1+1);
          end;
       end;
    end;
  end;
procedure sort(l,r: longint);
var  i,j,x,y: longint;
begin
  i:=l;j:=r;x:=b[(l+r) div 2,1];
  repeat
    while b[i,1]<x do inc(i);
    while x<b[j,1] do dec(j);
    if not(i>j)
       then begin
              swap(a[b[i,3]].dp[where[i],2],a[b[j,3]].dp[where[j],2]);
              swap(b[i,1],b[j,1]);
              swap(b[i,2],b[j,2]);
              swap(b[i,3],b[j,3]);
              swap(where[i],where[j]);
              inc(i);dec(j);
            end;
  until i>j;
  if l<j then sort(l,j);
  if i<r then sort(i,r);
end;
begin
  read(s);
  for l:=1 to s do
    begin
    read(n);
    m:=0;
    for i:=1 to n do
      begin
      read(k);
      for j:=1 to k do
        begin
        read(x,y);
        inc(m);
        b[m,1]:=x;
        b[m,2]:=y;
        b[m,3]:=i;
        with a[i] do
          begin
          num:=j;
          dp[j,1]:=y;
          dp[j,2]:=m;
          end;
        where[m]:=j;
        up(i,j);
        end;
      end;
    sort(1,m);
    v:=0;
    i:=1;
    f:=true;
    while (i<=m)and(f) do
      begin
      for j:=i+1 to m+1 do
        if b[j,1]<>b[i,1] then break;
      for k:=i to j-1 do
        begin
        p:=b[k,1];
        sum:=b[k,2];
        for x:=1 to b[k,3]-1 do
          inc(sum,a[x].dp[1,1]);
        for x:=b[k,3]+1 to n do
          inc(sum,a[x].dp[1,1]);
        if p/sum>v then v:=p/sum;
        end;

      for k:=i to j-1 do
        begin
        x:=b[k,3];
        y:=where[k];
        if where[k]=a[x].num then
           begin
           dec(a[x].num);
           if a[x].num=0 then begin f:=false; break; end;
           continue;
           end;
        a[x].dp[y]:=a[x].dp[a[x].num];
        dec(a[x].num);
        if a[x].num=0 then
           begin f:=false; break; end;
        where[a[x].dp[y,2]]:=y;
        t:=a[x].dp[y,2];
        up(x,y);
        down(x,where[t]);
        end;
      i:=j;
      end;
    writeln(v:0:3);
    end;
end.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator