题目传送门
解题思路:
将题目给的每对城市做一个pair,按照北岸城市从小到大排序,求南岸城市的最长不下降子序列.
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 5 using namespace std; 6 7 int n,f[200001],tot,g[200001]; 8 struct kkk{ 9 int a,b; 10 }e[200001]; 11 12 inline bool cmp(kkk s,kkk d) { 13 return s.a < d.a; 14 } 15 16 int main() { 17 scanf("%d",&n); 18 for(int i = 1;i <= n; i++) 19 scanf("%d%d",&e[i].a,&e[i].b); 20 sort(e+1,e+1+n,cmp); 21 f[++tot] = e[1].b; 22 for(int i = 1;i <= n; i++) 23 g[i] = e[i].b; 24 for(int i = 2;i <= n; i++) { 25 if(g[i] >= f[tot]) 26 f[++tot] = g[i]; 27 else { 28 int u = upper_bound(f+1,f+1+tot,g[i]) - f; 29 f[u] = g[i]; 30 } 31 } 32 printf("%d",tot); 33 return 0; 34 }