PPT1 例2

来源:互联网 发布:mysql ifnull不起作用 编辑:程序博客网 时间:2024/05/20 07:37

题意/Description

    桌子上零散地放着若干个不同颜色的盒子,桌子的后方是一堵墙。如右图所示。问从桌子前方可以看到多少个盒子?假设人站得足够远(自己设计测试数据,输入时,由底向上,从左到右)。

读入/Input

(不详)

16  //桌子长度
5   // 盒子数量
4 7
12 14
1 5
6 10
11 16

输出/Output

  可以看到多少个盒子。(4)


题解/solution

    使用一个数组F,初始化为0。遍历线段树,对于每种颜色c对F[c]赋值1。最后统计F中1的个数即可。(注意颜色0应该排除在外,可以在最后减1)

代码/Code

type  arr=record        cover:longint;      end;var  tree:array[0..2001] of arr;  n,m,ans:longint;  f:array[0..2001] of integer;procedure ins(p,l,r,a,b,c:longint);var  m:longint;begin  with tree[p] do    begin      if cover<>c then        begin          m:=(l+r) div 2;          if (a=l) and (b=r) then cover:=c else            begin              if cover>=0 then                begin                  tree[p*2].cover:=cover;                  tree[p*2+1].cover:=cover;                  cover:=-1;                end;          if b<=m then ins(p*2,l,m,a,b,c) else            if a>=m then ins(p*2+1,m,r,a,b,c) else              begin                ins(p*2,l,m,a,m,c);                ins(p*2+1,m,r,m,b,c);              end;            end;        end;    end;end;procedure count(p,l,r:longint);var  m:longint;begin  m:=(l+r) div 2;  with tree[p] do    begin      if cover>=0 then f[cover]:=1 else        if r-l>1 then          begin            count(p*2,l,m);            count(p*2+1,m,r);          end;    end;end;procedure main;var  a,b,i:longint;begin  fillchar(tree,sizeof(tree),255);  readln(m);  readln(n);  for i:=1 to n do    begin      readln(a,b);      ins(1,1,m,a,b,i);    end;end;procedure print;var  i:longint;begin  count(1,1,m);  for i:=0 to m do    if f[i]=1 then inc(ans);  writeln(ans);end;begin  main;  print;end.


3 0
原创粉丝点击