- 又发现了一个学校的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);