P2782 友好城市

题面:https://www.luogu.org/problemnew/show/P2782

本题可以以南岸或是北岸为关键字先从小到大排序,再在另一岸找最长上升子序列即可。

Code:

#include<iostream>
#include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm> #include<ctime> using namespace std; const int N=200005; int m,b[N],len,d[N]; struct Node{ int n,s; }a[N]; bool cmp(Node p,Node q){ if(p.n==q.n){ return p.s<q.s; } return p.n<q.n; } int main(){ scanf("%d",&m); for(int i=1;i<=m;i++){ scanf("%d%d",&a[i].n,&a[i].s); } sort(a+1,a+1+m,cmp); d[++len]=a[1].s; for(int i=2;i<=m;i++){ if(a[i].s>d[len]){ d[++len]=a[i].s; } else{ int j=upper_bound(d+1,d+1+len,a[i].s)-d; d[j]=a[i].s; } } printf("%d\n",len); return 0; } 

猜你喜欢

转载自www.cnblogs.com/ukcxrtjr/p/11119987.html