标签:表示 cpp 微软雅黑 条件 pad accept article dfs font
矩形X(a,b)能够嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度)。比如(1,5)能够嵌套在(6,2)内,但不能嵌套在(3,4)中。
你的任务是选出尽可能多的矩形排成一行,使得除最后一个外,每个矩形都能够嵌套在下一个矩形内。
1 10 1 2 2 4 5 8 6 10 7 9 3 1 5 8 12 10 9 7 2 2
5
/* Author: 2486 Memory: 240 KB Time: 0 MS Language: C++ Result: Accepted */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=1000+5; int T,n,dp[maxn]; struct rect { int x,y; } rs[maxn]; bool judge(rect &a,rect &b) { if((a.x<b.x&&a.y<b.y)||(a.y<b.x&&a.x<b.y))return true;//推断能否够嵌入 return false; }; int dfs(int v) { int ans=1; if(dp[v]!=-1)return dp[v]; for(int i=0; i<n; i++) { if(judge(rs[i],rs[v])) {//满足条件向下递归 ans=max(dfs(i)+1,ans); } } return dp[v]=ans; } int main() { scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%d%d",&rs[i].x,&rs[i].y); } memset(dp,-1,sizeof(dp)); int Max=0; for(int i=0; i<n; i++) { Max=max(dfs(i),Max); } printf("%d\n",Max); } return 0; }/* Author: 2486 Memory: 240 KB Time: 0 MS Language: C++ Result: Accepted */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1000 + 5; struct rect { int l,r; bool operator < (const rect &object)const { if(object.r == r) return l > object.l; return r > object.r; } } rs[maxn]; int dp[maxn]; int n, T; int main() { scanf("%d", &T); while(T --) { scanf("%d", &n); for(int i = 0; i < n; i ++) { scanf("%d%d", &rs[i].l, &rs[i].r); if(rs[i].l > rs[i].r) swap(rs[i].l, rs[i].r); } sort(rs, rs + n); memset(dp, 0, sizeof(dp)); int Max = 1; for(int i = 0; i < n; i ++) { dp[i] = 1; for(int j = 0; j < i; j ++) { if(rs[j].l > rs[i].l && rs[j].r > rs[i].r) dp[i] = max(dp[i], dp[j] + 1); Max = max(Max, dp[i]); } } printf("%d\n", Max); } return 0; }
标签:表示 cpp 微软雅黑 条件 pad accept article dfs font
原文地址:http://www.cnblogs.com/llguanli/p/6850443.html