Manhattan Mornings 二维上的最长不下降子序列


最长不下降子序列模板:https://www.cnblogs.com/itlqs/p/5743114.html

处理一下出发点,终点的位置。

之后就是套模板了..

先按照x排序,之后遍历y即可

#include<iostream>

#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int x1,x2;
int y11,y2;
int n;
struct node
{
    int x,y;
}lou[1100000];
bool cmp1(node a,node b)
{
    if (a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}


bool cmp2(node a,node b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x>b.x;
}
int dp[110000];
int main()
{
    cin>>n;
    cin>>x1>>y11>>x2>>y2;
    int flag;
    if(y11>y2)//保证出发点在下面
    {
        swap(x1,x2);
        swap(y11,y2);
    }
    if(x1<x2) //出发点在左边
    {
        flag=1;
    }
    else//出发在右边
    {
        flag=-1;
    }
    int tot=0;
    for(int i=0;i<n;i++)
    {
        int aa,bb;
        cin>>aa>>bb;
        if(min(x1,x2)<=aa&&aa<=max(x1,x2)&&min(y11,y2)<=bb&&bb<=max(y11,y2))
        {
               lou[++tot].x=aa;
               lou[tot].y=bb;
        }
    }
    if(tot==0)
    {
        cout<<"0"<<endl;
        return 0;
    }
    if(flag==1)
    {
        sort(lou+1,lou+1+tot,cmp1);
    }
    else
    {
        sort(lou+1,lou+1+tot,cmp2);
    }
    int len=0;
    dp[++len]=lou[1].y;
    for(int i=2;i<=tot;i++)
    {
        if(lou[i].y>=dp[len])
        {
            dp[++len]=lou[i].y;
        }
        else
        {
            int pos=upper_bound(dp+1,dp+1+len,lou[i].y)-dp;//求最长不减子序列时对应用upper_bound
            dp[pos]=lou[i].y;
        }
    }
    cout<<len<<endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值