[动态规划][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 ≤ 10 5, the number of errands you have to run that day.
• The next line contains four integers 0 ≤ xh, yh, xw, yw ≤ 10 9 corresponding to the locations of your house and workplace.
• The next n lines each contain two integers 0 ≤ xi, yi ≤ 10 9 , 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); }

转载于:https://www.cnblogs.com/lllxq/p/8747957.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值