~~~~
两个题目大致类似,NYOJ上面那道题就是小白上的矩形嵌套啦。
都是先对长宽进行排序,然后逐层更新最大值(边更新边记录)。
说白了,不过也都是二维的最长上升子序列。
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1093
http://acm.nyist.net/JudgeOnline/problem.php?pid=16
~~~~
ZOJ1093:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 33
using namespace std;
struct node
{
int l,w,h;
}s[N*3];
//按长宽大小排序
bool cmp(node a,node b)
{
if(a.l==b.l)
return a.w<b.w;
return a.l<b.l;
}
int main()
{
int n,T=0;
while(~scanf("%d",&n),n)
{
int cnt=0;
//对于每个石头,都有三种放置的方法(其实是六种)。
//这里写的好挫,勿喷。。
for(int i=0;i<n;i++)
{
int a,b,c,ta,tb,tc;
scanf("%d %d %d",&a,&b,&c);
ta=a;tb=b;tc=c;
s[cnt].h=a;
if(b<c) swap(tb,tc);
s[cnt].l=tb;
s[cnt++].w=tc;
ta=a;tb=b;tc=c;
s[cnt].h=b;
if(a<c) swap(ta,tc);
s[cnt].l=ta;
s[cnt++].w=tc;
ta=a;tb=b;tc=c;
s[cnt].h=c;
if(a<b) swap(ta,tb);
s[cnt].l=ta;
s[cnt++].w=tb;
}
sort(s,s+cnt,cmp);
int ans=0;
for(int i=0;i<cnt;i++)
{
int tm=0;
for(int j=0;j<i;j++)
{
//最小的石头上面可以放置的石头的最大高度。
//然后一层一层的更新。
if(s[i].l>s[j].l && s[i].w>s[j].w && s[j].h>tm)
tm=s[j].h;
}
s[i].h+=tm;
ans=max(ans,s[i].h);
}
printf("Case %d: maximum height = %d\n",++T,ans);
}
return 0;
}
~~~~
NYOJ16:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 1111
using namespace std;
int dp[N];
struct node
{
int l;
int w;
}tri[N];
bool cmp(node a,node b)
{
if(a.l==b.l)
return a.w>b.w;
return a.l>b.l;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
if(u<v) swap(u,v);
tri[i].l=u;
tri[i].w=v;
}
sort(tri,tri+n,cmp);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
{
dp[i]=1;
for(int j=0;j<i;j++)
{
if(tri[i].l<tri[j].l && tri[i].w<tri[j].w && dp[i]<dp[j]+1)
dp[i]=dp[j]+1;
}
}
int ans=dp[0];
for(int i=1;i<n;i++)
ans=max(ans,dp[i]);
printf("%d\n",ans);
}
return 0;
}