【动态规划】最长子序列问题:友好城市_蓝桥杯

 

【动态规划】最长子序列问题:友好城市_ci_02

(1)sort排序结构体 

 (2)初始化一行为1

#include<iostream>
#include<algorithm>
using namespace std;
int dp[5001];
struct city{
    int x,y;
    city(){};
    city(int _x,int _y){
        x=_x;
        y=_y;
    }
}c[5001];
 bool cmp(city c1,city c2){
     return c1.x<c2.x;
 }
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>c[i].x>>c[i].y;
    }
     sort(c,c+n,cmp);
    //  for(int i=0;i<n;i++){
    //      cout<<c[i].x<<" "<<c[i].y<<endl;
    //  }
   for(int i=0;i<n;i++){
       dp[i]=1;
   }
   int res=0;
   for(int i=1;i<n;i++){
       for(int j=0;j<i;j++){
           if(c[j].y<c[i].y){
               dp[i]=max(dp[i],dp[j]+1);
               res=max(res,dp[i]);
           }
       }
   }
   cout<<res;
   return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.