NOJ [1116] Flandre's Passageway

  • 问题描述
  • Flandre Scarlet(フランドール·スカーレット) is a vampire of The Embodiment of Scarlet Devil. But she is unfortunate because she is always in the undercroft.
    The undercroft has so many rooms signed as room[1][1], room[1][2], ... room[N][M].
    Today she wants to go from the room[1][1] to room[N][M]. Every room she can only go straight along the walls but some special rooms.
    In the special rooms, she Flandre can go through it.
    You can imagine that the length of every wall is 100.
    Can you help Flandre to caculate out the minimum length she would go from the room[1][1] to room[N][M]?
    You can reference the picture below.

  • 输入
  • This problem has several cases.
    The first line of each case contains 2 integers N and M (0 < N, M <= 100000).
    Then follow a line that only contains an integer K (0 < K <= 2000), indicates the number of special rooms.
    The next K lines stands for the coordinate X, Y(1 <= X <= N, 1 <= Y <= M) of each special room.
  • 输出
  • For each case, you only need to print the minimum length that Flandre would go from (1, 1) to (N, M). Round to the nearest integer.


    刚看到这题,根本没想到是最长上升子序列,因为怎么看都不像,后来看了下题解,知道了并不是整个去找最长上升子序列,而是对于那些特殊的路去进行寻找,当走过一个特殊房间时,我们可以少走2面墙壁,所以,如果可以找到自下而上的最长的这样一个特殊房间序列的话,那么走的路一定是最短的,注意,找到的序列里,在从(1,1)到(N,M)一定可以都走到,这就要求下面的房间的x,y都小于上面的房间的x,y,也就是处于斜对角关系

    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    struct room
    {
    	int x,y;
    }undercroft[2005];
    
    int cmp(room a,room b)
    {
    	return a.x < b.x;
    }
    
    int dp[2005];
    
    int main()
    {
    	int n,m;//如果说没有一个特殊走法的话,总路程就是(n+m)*100,每走一个特殊的路,就可以少走2个墙壁
    	while(~scanf("%d%d",&n,&m))
    	{
    		int k,i,j;
    		scanf("%d",&k);
    		for(i=0;i<k;i++)
    		{
    			scanf("%d %d",&undercroft[i].x,&undercroft[i].y);//存特殊房间
    		}
    		sort(undercroft,undercroft+k,cmp);
    		memset(dp,0,sizeof(dp));
    		dp[0]=1;
    		int _max=-1;
    		for(i=1;i<k;i++)
    		{
    			int ans=0;
    			for(j=0;j<i;j++)
    			{
    				
    				if(undercroft[j].x < undercroft[i].x && undercroft[j].y < undercroft[i].y)
    				{
    					ans=max(dp[j],ans);//向前找到最大的上升子序列
    				}
    			}
    			dp[i]=ans+1;
    			if(_max<dp[i])
    				_max=dp[i];
    		}
    
    		double dist=(n+m-2*_max)*100+_max*sqrt(2.0)*100;
    		printf("%.f\n",dist );
    	}
    	return 0;
    
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值