码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-09 00:27:53      阅读:370      评论:0      收藏:0      [点我收藏+]

标签:src   printf   main   int   dir   span   变形   define   str   

题目描述

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 o?ce, 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. Speci?cally, 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 ?rst 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

标签:src   printf   main   int   dir   span   变形   define   str   

原文地址:https://www.cnblogs.com/lllxq/p/8747957.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!