清帝之惑之顺治(Vijos1011)

本文介绍了一种基于动态规划(DP)的算法实现方案,通过排序和状态转移的方式解决特定问题。文章详细展示了如何初始化变量、进行快速排序,并逐步更新状态以找到最优解。
算法:DP
 
分析:先排序一下,这样的话就从最大的那个点开始,因为这样能滑的更远。

      如果状态成立的话,每个点可以由这个点的四个方向得来,记录一个最大值,然后继续找第二大的点。以此类推。

program P1011;

const
 maxn=500;
 maxt=250000;
 dx:array [1..4] of -1..1=(0,-1,0,1);
 dy:array [1..4] of -1..1=(-1,0,1,0);
 
type
 atp=record
  x,y,val:longint;
 end;

var
 n,m,tot,ans:longint;
 a:array [0..maxt] of atp;
 v,f:array [0..maxn,0..maxn] of longint;
 
procedure init;
var
 i,j,x:longint;
begin
 ans:=-maxlongint;
 tot:=0;
 readln(n,m);
 for i:=1 to n do
  begin
   for j:=1 to m do
    begin
     read(x);
     v[i,j]:=x;
     inc(tot);
     a[tot].x:=i;
     a[tot].y:=j;
     a[tot].val:=x;
    end;
   readln;
  end;
end;

procedure qsort(l,r:longint);
var
 i,j,m:longint;
 t:atp;
begin
 i:=l;
 j:=r;
 m:=a[(l+r) shr 1].val;
 repeat
  while a[i].val>m do inc(i);
  while a[j].val<m do dec(j);
  if i<=j then
   begin
    t:=a[i];
    a[i]:=a[j];
    a[j]:=t;
    inc(i);
    dec(j);
   end;
 until i>j;
 if i<r then qsort(i,r);
 if l<j then qsort(l,j);
end;

procedure main;
var
 i,j,x,y,now:longint;
begin
 for i:=1 to tot do
  begin
   x:=a[i].x;
   y:=a[i].y;
   now:=0;
   for j:=1 to 4 do
    begin
     if (v[x+dx[j],y+dy[j]]>v[x,y]) and (f[x+dx[j],y+dy[j]]>now) then
     now:=f[x+dx[j],y+dy[j]];
    end;
   f[x,y]:=now+1;
   if f[x,y]>ans then ans:=f[x,y];
  end;
end;

begin
 assign(input,'P1011.in'); reset(input);
 assign(output,'P1011.out'); rewrite(output);
 
 init;
 qsort(1,tot);
 main;
 writeln(ans);

 close(input); close(output);
end.




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值