zoukankan      html  css  js  c++  java
  • bzoj1293

    简易贪心+heap

    注意要用链表

      1 type link=^node;
      2      node=record
      3        loc:longint;
      4        next:link;
      5      end;
      6      point=record
      7        loc,num:longint;
      8      end;
      9 var w,b:array[0..70] of link;
     10     heap:array[0..10010] of point;
     11     n,k,s,x,i,j,y,t,ans:longint;
     12     p,q:link;
     13 
     14 function min(a,b:longint):longint;
     15   begin
     16     if a>b then exit(b) else exit(a);
     17   end;
     18 
     19 procedure swap(var a,b:point);
     20   var c:point;
     21   begin
     22     c:=a;
     23     a:=b;
     24     b:=c;
     25   end;
     26 
     27 procedure up(i:longint);
     28   var j:longint;
     29   begin
     30     j:=i shr 1;
     31     while j>0 do
     32     begin
     33       if heap[i].loc<heap[j].loc then
     34       begin
     35         swap(heap[i],heap[j]);
     36         i:=j;
     37         j:=i shr 1;
     38       end
     39       else break;
     40     end;
     41   end;
     42 
     43 procedure sift(i:longint);
     44   var j:longint;
     45   begin
     46     j:=i shl 1;
     47     while j<=k do
     48     begin
     49       if (j+1<=k) and (heap[j].loc>heap[j+1].loc) then inc(j);
     50       if heap[i].loc>heap[j].loc then
     51       begin
     52         swap(heap[i],heap[j]);
     53         i:=j;
     54         j:=i shl 1;
     55       end
     56       else break;
     57     end;
     58   end;
     59 
     60 begin
     61   readln(n,k);
     62   for i:=1 to k do
     63   begin
     64     read(s);
     65     w[i]:=nil;
     66     for j:=1 to s do
     67     begin
     68       read(x);
     69       new(p);
     70       p^.next:=nil;
     71       p^.loc:=x;
     72       if w[i]=nil then
     73       begin
     74         w[i]:=p;
     75         q:=p;
     76       end
     77       else begin
     78         q^.next:=p;
     79         q:=p;
     80       end;
     81     end;
     82     b[i]:=w[i];
     83     readln;
     84   end;
     85   y:=0;
     86   for i:=1 to k do
     87   begin
     88     t:=t+1;
     89     heap[t].loc:=b[i]^.loc;
     90     heap[t].num:=i;
     91     up(t);
     92     if heap[t].loc>y then y:=heap[t].loc;
     93     b[i]:=b[i]^.next;
     94   end;
     95   ans:=y-heap[1].loc;
     96   while true do
     97   begin
     98     x:=heap[1].num;
     99     if b[x]=nil then break
    100     else begin
    101       heap[1].loc:=b[x]^.loc;
    102       if y<b[x]^.loc then y:=b[x]^.loc;
    103       b[x]:=b[x]^.next;
    104       sift(1);
    105       ans:=min(ans,y-heap[1].loc);
    106     end;
    107   end;
    108   writeln(ans);
    109 end.
    View Code
  • 相关阅读:
    视图创建
    根据表格作业题
    表格 作业题练习
    创建表格 练习题
    聚合函数、数学函数、日期时间函数
    接口自动化框架
    python+request+Excel 几十份接口测试用例一起自动化测试的接口测试框架
    python3 函数
    pip源头
    爬虫
  • 原文地址:https://www.cnblogs.com/phile/p/4473254.html
Copyright © 2011-2022 走看看