友好城市

题目描述

有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市。北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同。每对友好城市都向政府申请在河上开辟一条直线航道连接两个城市,但是由于河上雾太大,政府决定避免任意两条航道交叉,以避免事故。编程帮助政府做出一些批准和拒绝申请的决定,使得在保证任意两条航道不相交的情况下,被批准的申请尽量多。

输入格式

第1行,一个整数N,表示城市数。

第2行到第n+1行,每行两个整数,中间用一个空格隔开,分别表示南岸和北岸的一对友好城市的坐标。

输出格式

仅一行,输出一个整数,表示政府所能批准的最多申请数。

输入输出样例

输入 #1
7
22 4
2 6
10 3
15 12
9 8
17 17
4 2
输出 #1
4

说明/提示

50% 1<=N<=5000,0<=xi<=10000

100% 1<=N<=2e5,0<=xi<=1e6

【解题思路】

最长上升子序列

【code】

 1 #include <cstdio>
 2 #include <cmath> 
 3 #include <algorithm>
 4 #include <iostream>
 5 using namespace std;
 6 struct Node{
 7     int n;
 8     int s; 
 9 }a[200005];
10 bool operator<(const Node &a,const Node &b){
11     return a.n<b.n;
12 }
13 int n,len,d[200005];
14 int main(){
15     scanf("%d",&n);
16     for(register int i=1;i<=n;i++)
17         scanf("%d%d",&a[i].n,&a[i].s);
18     sort(a+1,a+n+1);
19     d[++len]=a[1].s;
20     for(register int i=2;i<=n;i++){
21         int tmp=lower_bound(d+1,d+len+1,a[i].s)-d;
22         d[tmp]=a[i].s;
23         if(tmp>len)len++;
24     }
25     printf("%d\n",len);
26     return 0;    
27 }

 

posted @ 2019-07-27 23:29  GTR_PaulFrank  阅读(80)  评论(0编辑  收藏  举报