友好城市
解题思路:不交叉,则将北岸的坐标从小到大排,找南岸的最长上升子序列
AC_Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <algorithm> 5 #include <bits/stdc++.h> 6 using namespace std; 7 typedef long long ll; 8 const int maxn = 5010; 9 10 int dp[maxn]; 11 struct node{ 12 int north; 13 int south; 14 }a[maxn]; 15 16 bool cmp(node a, node b){ 17 return a.north<b.north; 18 } 19 20 int main() 21 { 22 int n,maxx=0; 23 scanf("%d",&n); 24 for(int i=0;i<n;i++){ 25 scanf("%d %d",&a[i].south, &a[i].north); 26 } 27 sort(a,a+n,cmp); 28 for(int i=0;i<n;i++){ 29 for(int j=0;j<i;j++){ 30 if( a[i].south>a[j].south ){ 31 dp[i] = max(dp[j]+1,dp[i]); 32 maxx = max(maxx,dp[i]); 33 } 34 } 35 } 36 printf("%d ",maxx+1); 37 return 0; 38 }