离散化泛做

  • 又发现了一个学校的OJ….话说我现在收集了多少个了…

[USACO Open 2012 铜组]岛屿

传送门

http://oi.nks.edu.cn/showproblem?problem_id=1279

题解

先排序,由小到大淹没,对于一个点,如果它左右两个点都没被淹没,那么淹没它,岛的个数就加一,如果两边都被淹没,那么岛数就减一,如果只有一边被掩埋,那么岛数不变

var
 x,z:array[0..100000]of longint;
 y:array[0..100000,1..2]of longint;
 i,j,k:longint;
 n,len,now,ans:longint;
procedure sort(l,r:longint);
var i,j,a,b:longint;
begin
 i:=l; j:=r; a:=y[(l+r) div 2,1];
 repeat
  while y[i,1]<a do inc(i);
  while a<y[j,1] do dec(j);
  if not(i>j) then
   begin
    b:=y[i,1]; y[i,1]:=y[j,1]; y[j,1]:=b;
    b:=y[i,2]; y[i,2]:=y[j,2]; y[j,2]:=b;
    inc(i); dec(j);
   end;
 until i>j;
 if l<j then sort(l,j);
 if i<r then sort(i,r);
end;

begin
 readln(n);
 for i:=1 to n do
  begin readln(x[i]); y[i,1]:=x[i]; y[i,2]:=i; end;
 sort(1,n); {y[i,1]}
 fillchar(z,sizeof(z),0);
 ans:=1; now:=1; z[0]:=1; z[n+1]:=1;
 for i:=1 to n do
  begin
   if (z[y[i,2]-1]=0)and(z[y[i,2]+1]=0)
   then inc(now)
   else
    if (z[y[i,2]-1]=1)and(z[y[i,2]+1]=1)
    then dec(now);
   z[y[i,2]]:=1;
   if now>ans then ans:=now;
  end;
 writeln(ans);
end.

好多狼骑

传送门

http://oi.nks.edu.cn/showproblem?problem_id=1178

题解

双关键字排序后,模拟就好了

var
 x:array[0..20000,1..2]of longint;
 i,j,k:longint;
 n:longint;
 now,ans:longint;
procedure sort(l,r:longint);
var i,j,a,b,c:longint;
begin
 i:=l; j:=r; a:=x[(l+r) div 2,1]; b:=x[(l+r)div 2,2];
 repeat
  while (x[i,1]<a)or((x[i,1]=a)and(x[i,2]<b)) do inc(i);
  while (a<x[j,1])or((x[j,1]=a)and(x[j,2]>b)) do dec(j);
  if not(i>j) then
   begin
    c:=x[i,1]; x[i,1]:=x[j,1]; x[j,1]:=c;
    c:=x[i,2]; x[i,2]:=x[j,2]; x[j,2]:=c;
    inc(i); dec(j);
   end;
 until i>j;
 if l<j then sort(l,j);
 if i<r then sort(i,r);
end;

begin
 readln(n);
 for i:=1 to n do
  readln(x[i,1],x[i,2]);
 sort(1,n); {x[i,1],x[i,2]}
 now:=x[1,1]-1; ans:=0;
 for i:=1 to n do
  if x[i,1]>now
  then begin inc(ans,x[i,2]-x[i,1]+1); now:=x[i,2]; end
  else
   if x[i,2]>now
   then begin inc(ans,x[i,2]-now); now:=x[i,2]; end;
 writeln(ans);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值