船
(ships.pas/c/cpp)
来源:《奥赛经典》(提高篇)
【问题描述】
PALMIA国家被一条河流分成南北两岸,南北两岸上各有N个村庄。北岸的每一个村庄有一个唯一的朋友在南岸,且他们的朋友村庄彼此不同。
每一对朋友村庄想要一条船来连接他们,他们向政府提出申请以获得批准。由于河面上常常有雾,政府决定禁止船只航线相交(如果相交,则很可能导致碰船)。
你的任务是编写一个程序,帮助政府官员决定批准哪些船只航线,使得不相交的航线数目最大。
【输入文件】ships.in
【输出文件】ships.out
【输入样例】
30 4
7
22 4
2 6
10 3
15 12
9 8
17 17
4 2
0 0
【输出样例】
4
=========================
========================
{
ID:jie19952
PROG:
LANG:PASCAL
}
type
re=record
c,d:longint;
end;
var
len,wide:longint;
cun:array[1..5000]of re;
f:array[1..5000]of longint;
n:longint;
procedure init;
begin
assign(input,'ships.in');
assign(output,'ships.out');
reset(input); rewrite(output);
end;
procedure terminate;
begin
close(input); close(output);
halt;
end;
procedure qsort(s,t:longint);
var
i,j:longint;
tem:re;
x:re;
begin
x:=cun[(s+t)shr 1];
i:=s; j:=t;
repeat
while cun[j].c>x.c do dec(j);
while cun[i].c<x.c do inc(i);
if i<=j then
begin
tem:=cun[i];
cun[i]:=cun[j];
cun[j]:=tem;
inc(i); dec(j);
end;
until i>j;
if i<t then qsort(i,t);
if s<j then qsort(s,j);
end;
procedure main;
var
i,j:longint;
ans:longint;
begin
readln(len,wide);
readln(n);
for i:=1 to n do
begin
readln(cun[i].c,cun[i].d);
f[i]:=1;
end;
qsort(1,n);
for i:=1 to n do
begin
for j:=i-1 downto 1 do
begin
if (f[i]<f[j]+1) and (cun[j].d<cun[i].d) then f[i]:=f[j]+1;
end;
end;
ans:=0;
for i:=1 to n do
begin
if ans<f[i] then
ans:=f[i];
end;
writeln(ans);
end;
begin
init;
main;
terminate;
end.