首页 > 编程语言 > 详细

友好城市(LIS+结构体排序)

时间:2020-01-30 23:56:23      阅读:120      评论:0      收藏:0      [点我收藏+]

友好城市

技术分享图片

 

 解题思路:不交叉,则将北岸的坐标从小到大排,找南岸的最长上升子序列

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\n",maxx+1);
37     return 0;
38 }

 

友好城市(LIS+结构体排序)

原文:https://www.cnblogs.com/wsy107316/p/12244299.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!