标签:
Given N (4 <= N <= 100) rectangles and the lengths of their sides ( integers in the range 1..1,000), write a program that finds the maximum K for which there is a sequence of K of the given rectangles that can "nest", (i.e., some sequence P1, P2, ..., Pk, such that P1 can completely fit into P2, P2 can completely fit into P3, etc.).
A rectangle fits inside another rectangle if one of its sides is strictly smaller than the other rectangle‘s and the remaining side is no larger. If two rectangles are identical they are considered not to fit into each other. For example, a 2*1 rectangle fits in a 2*2 rectangle, but not in another 2*1 rectangle.
The list can be created from rectangles in any order and in either orientation.
1 4 8 14 16 28 29 12 14 8
2
一个DP题 类似于单调递增最长子序列 队友做的。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct Node { int x,y; }node[105]; bool cmp(Node a,Node b) { if(a.x!=b.x) return a.x<b.x; else return a.y<b.y; } int dp[105]; int vis[1005][1005]; int main() { int T; scanf("%d",&T); while(T--) { memset(node,0,sizeof(node)); memset(dp,0,sizeof(dp)); memset(vis,0,sizeof(vis)); int N; scanf("%d",&N); int n=0; for(int i=0;i<N;i++) { int a,b; scanf("%d%d",&a,&b); if(!vis[a][b]) { node[n].x=a<b?a:b; node[n++].y=a>b?a:b; vis[a][b]=1; vis[b][a]=1; } } sort(node,node+n,cmp); // for(int i=0;i<n;i++) // printf("%d %d\n",node[i].x,node[i].y); int max=0; for(int i=1;i<n;i++) for(int j=0;j<i;j++) { if(node[i].x>=node[j].x&&node[i].y>=node[j].y&&dp[i]<dp[j]+1) dp[i]=dp[j]+1; max=max<dp[i]?dp[i]:max; } printf("%d\n",max+1); } return 0; }
nyoj1255 Rectangles(第七届河南省程序设计大赛)
标签:
原文地址:http://blog.csdn.net/su20145104009/article/details/51519885