题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=16
1 10 1 2 2 4 5 8 6 10 7 9 3 1 5 8 12 10 9 7 2 2
5
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #define PI acos(-1.0) #define mem(a,b) memset(a,b,sizeof(a)) #define sca(a) scanf("%d",&a) #define sc(a,b) scanf("%d%d",&a,&b) #define pri(a) printf("%d\n",a) #define lson i<<1,l,mid #define rson i<<1|1,mid+1,r #define MM 204 #define MN 1008 #define INF 2000000000 #define eps 1e-8 using namespace std; typedef long long ll; typedef unsigned long long ULL; int n,dp[MN],head[MN*10],cnt; struct node { int x,y; }e[MN]; struct no { int v,next; }ee[MN*10]; void add(int u,int v) { ee[cnt].v=v,ee[cnt].next=head[u],head[u]=cnt++; } bool cmp(node a,node b) { if(a.x==b.x) return a.y<b.y; return a.x<b.x; } int dfs(int u) { if(dp[u]) return dp[u]; int i,ans=1; for(i=head[u];i!=-1;i=ee[i].next) ans=max(ans,dfs(ee[i].v)+1); //深搜下去,找到点就是嵌套的矩形+1了 return ans; } int main() { int t; sca(t); while(t--) { int i,j,x,y,Max=-INF; mem(dp,0); mem(head,-1); cnt=0; sca(n); for(i=1;i<=n;i++) { sc(x,y); if(x>y) swap(x,y); e[i].x=x,e[i].y=y; } sort(e+1,e+n+1,cmp); for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) if(e[i].x<e[j].x&&e[i].y<e[j].y) add(i,j); for(i=1;i<=n;i++) Max=max(Max,dfs(i)); pri(Max); } return 0; }
嵌套矩形 DAG上的dp(深搜+dp),布布扣,bubuko.com
原文:http://blog.csdn.net/u011466175/article/details/25512925