Gym 101666M Manhattan Mornings(最长不降子序列)

Gym 101666M Manhattan Mornings

Description

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.

Input

• 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

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

Sample Input 1

3
0 0 6 6
5 4
2 6
3 1

Sample Output 1

2

Sample Input 2

5
2 1 0 0
0 0
0 1
2 0
2 1
3 1

Sample Output 2

3

Sample Input 3

4
200 100 100 200
50 150
200 200
100 100
100 100

Sample Output 3

2

题解

题意

一个城市道路都是曼哈顿道路,给出家庭与上班地点的坐标;定义一些事务点。问在不绕远的情况下能通过最多的给出的事务点,输出该值。

思路

非常明显的最长不降子序列,首先维护出来横纵坐标位于家和上班地点之间的事务点的坐标。排序后,利用二分查找到到达xi坐标时,yi的最小值。以此来找出最长不降子序列的长度。

注意:家和上班地点的坐标是随机的。所以应当通过变换或者注意排序方法来区分不同的情况。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
int T;

const int MAXN = 1e5+10;
int ans[MAXN];

struct Node{
    int x;
    int y;
}sav[MAXN];

int cmp1(Node a,Node b){
    if (a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}

int cmp2(Node a,Node b){
    if (a.x==b.x) return a.y<b.y;
    return a.x>b.x;
}

int main() {
    int T;
    scanf("%d",&T);
    int kind=0;
    int a,b,c,d;
    int tot = -1;
    scanf("%d %d %d %d",&a,&b,&c,&d);
    if(b>d){
        swap(a,c),swap(b,d);
    }
    if(c>a) kind = 1;
    else    kind = 2;
    int xl = min(a,c),xr = max(a,c),yl=min(b,d),yr=max(b,d);
    for(int i=0;i<T;i++){
        int curx,cury;
        scanf("%d %d",&curx,&cury);
        if(xl<=curx&&curx<=xr&&yl<=cury&&cury<=yr) {
            tot++; 
            sav[tot].x=curx;  
            sav[tot].y=cury;
            
        }
    }
    if(tot==-1){
        printf("0\n");
        return 0;
    }else{
        if(kind==1) sort(sav,sav+tot+1,cmp1);
        if(kind==2) sort(sav,sav+tot+1,cmp2);
        int len = 0;
        ans[++len] = sav[0].y;
        for(int i=1;i<tot+1;i++){
            if(sav[i].y>=ans[len]) ans[++len]=sav[i].y;
            else{
                int pos=upper_bound(ans+1,ans+len+1,sav[i].y)-ans;
                ans[pos]=sav[i].y;
            }
        }
        printf("%d\n",len);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/caomingpei/p/9694289.html
今日推荐