题解 | #信封嵌套#
信封嵌套
https://www.nowcoder.com/practice/25fe1fc89c4c4e82bbc63df04bc6ca30
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<pair<int, int>> a(n); for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; if(n==1) { puts("1"); return 0; } // 转化为最长上升子序列问题 sort(a.begin(), a.end()); vector<int> dp(n + 1, 1); int flag = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (a[i].first > a[j].first && a[i].second > a[j].second) { dp[i] = max(dp[i], dp[j] + 1); flag = max(flag,dp[i]); } } } cout << flag << endl; return 0; }
算法常用解题技巧 文章被收录于专栏
算法常用解题技巧