[动态规划][LIS变形]Manhattan Mornings

Posted lz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[动态规划][LIS变形]Manhattan Mornings相关的知识,希望对你有一定的参考价值。

题目描述

As a New Yorker you are always very busy. Apart from your long work day you tend to have a very long list of
errands that need to be done on any particular day. You really hate getting up early so you always end up going over
your to-do list after work, but this is starting to seriously hurt your free time.
One day you realize that some of the places you have to go by lie on your walk to the office, so you can visit them before work. The next day you notice that if you take a slightly different route to work you can run most of your errands without increasing the length of your route. 
Since errands take a negligible amount of time, you don’t even have to get up any earlier to do this! This nice little side effect of the grid-like New York streets gets you thinking. Given all the locations of your errands on the New York grid, how many can you visit on your way to work without getting up any earlier?
The New York grid is modelled with streets that are parallel to the x-axis and avenues that are parallel to the y-axis. Specifically, there is a street given by y = a for every a ∈ Z , and there is an avenue given by x = b for every b ∈ Z . It is given that an errand always takes place on an intersection between a street and an avenue. Since you walk to your work, you can use all roads in both directions.

输入

• The first line contains an integer 0 ≤ n ≤ 105, the number of errands you have to run that day.
• The next line contains four integers 0 ≤ xh, yh, xw, yw ≤ 109 corresponding to the locations of your house and workplace.
• The next n lines each contain two integers 0 ≤ xi, yi ≤ 109 , the coordinates of your ith errand.

输出

Output a single line, containing the number of errands you can run before work without taking a longer route than necessary.

样例输入

3
0 0 6 6
5 4
2 6
3 1

样例输出

2

思路:本质为求最长不减子序列长度

house和workplace的相对位置有两种情况:1.

2.(其实还有H在W上方的情况——当H在W上方时,将H,W互换)

在情况1下:将H,W内的errands按列号递增排序(不在合法区域内的忽略),求此时对应行号序列的最长不减子序列

在情况2下:将H,W内的errands按列号递减排序(不在合法区域内的忽略),求此时对应行号序列的最长不减子序列

 AC代码:

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100500
using namespace std;

int n,a,b,c,d;

struct node{
    int x,y;
}stone[N];

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 ans[N]; int main() { scanf("%d",&n); scanf("%d%d%d%d",&a,&b,&c,&d); int kase; if(b>d){ swap(a,c); swap(b,d); } if(a<c) kase=1; else kase=-1; int tot=0; for (int i=1;i<=n;i++) { int x,y; scanf("%d%d",&x,&y); if(min(a,c)<=x&&x<=max(a,c)&&min(b,d)<=y&&y<=max(b,d)) {
stone[++tot].x=x;
stone[tot].y=y;
} }
if(tot==0){printf("0\\n"); return 0;} if(kase==1) sort(stone+1,stone+1+tot,cmp1); if(kase==-1) sort(stone+1,stone+1+tot,cmp2); int len = 0; ans[++len]=stone[1].y; for (int i=2;i<=tot;i++) { if(stone[i].y>=ans[len]) ans[++len]=stone[i].y; else{ int pos=upper_bound(ans+1,ans+1+len,stone[i].y)-ans;//求最长不减子序列时对应用upper_bound ans[pos]=stone[i].y; } } printf("%d\\n",len); }

以上是关于[动态规划][LIS变形]Manhattan Mornings的主要内容,如果未能解决你的问题,请参考以下文章

动态规划解决最长上升子序列LIS避坑

动态规划(DP),最长递增子序列(LIS)

动态规划-LIS最长上升子序列

动态规划(DP),类似LIS,FatMouse's Speed

动态规划_leetcode300(LIS)

动态规划 | 对输入进行hash处理的LIS 1045