船(ships.pas/c/cpp)

(ships.pas/c/cpp)

 

来源:《奥赛经典》(提高篇)

【问题描述】

PALMIA国家被一条河流分成南北两岸,南北两岸上各有N个村庄。北岸的每一个村庄有一个唯一的朋友在南岸,且他们的朋友村庄彼此不同。

每一对朋友村庄想要一条船来连接他们,他们向政府提出申请以获得批准。由于河面上常常有雾,政府决定禁止船只航线相交(如果相交,则很可能导致碰船)。

你的任务是编写一个程序,帮助政府官员决定批准哪些船只航线,使得不相交的航线数目最大。

【输入文件】ships.in

    输入文件由几组数据组成。每组数据的第一行有2个整数X,Y,中间有一个空格隔开,X代表PALMIA河的长度(10<=X<=6000),Y代表河的宽度(10<=Y<=100)。第二行包含整数N,表示分别坐落在南北两岸上的村庄的数目(1<=N<=5000)。在接下来的N行中,每一行有两个非负整数C,D,由一个空格隔开,分别表示这一对朋友村庄沿河岸与PALMIA河最西边界的距离(C代表北岸的村庄,D代表南岸的村庄),不存在同岸又同位置的村庄。最后一组数据的下面仅有一行,是两个0,也被一空格隔开。

【输出文件】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.


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值