标签:
1 10 1 2 2 4 5 8 6 10 7 9 3 1 5 8 12 10 9 7 2 2
5
哈哈,没想到一次提交就AC了。恩,这一题思考了一下便想到这不就是最长上升子序列的变形吗?于是便很理所应当的套一下模型,然而这一题又和最长上升子序列不一样,因为它不是固定的序列,即求最多的矩形嵌套,所以先要排序啦,写一个cmp+sort就AC啦,cmp函数见代码,所以经典的题目都要做一遍啊.
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; #define N 1001 int dp[N]; struct point { int x, y; }a[N]; int cmp(point xx,point yy) { return(min(xx.x, xx.y) < min(yy.x, yy.y)); } int main() { int t,n; scanf("%d", &t); while (t--) { memset(dp, 0, sizeof(dp)); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", &a[i].x, &a[i].y); sort(a, a + n, cmp); int ans = 0; for (int i = 0; i < n; i++) { dp[i] = 1; for (int j = 0; j < i; j++) { if ((a[j].x < a[i].x&&a[j].y < a[i].y || a[j].x < a[i].y&&a[j].y < a[i].x) && dp[i] < dp[j] + 1) { dp[i] = dp[j] + 1; } } if (ans < dp[i]) ans = dp[i]; } printf("%d\n", ans); } return 0; }
标签:
原文地址:http://blog.csdn.net/qq_24489717/article/details/45482817