NYOJ-16-矩形嵌套 记忆化搜索
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #define rep(i,j,k) for(int i=j;i<=k;i++) using namespace std; const int maxx =1005; int maps[maxx][maxx]; int d[maxx]; struct node{ int x,y; }a[maxx]; int n; int dp(int i){ int& ans=d[i]; if(ans>0)return ans; ans=1; for(int j=1;j<=n;j++){ if (maps[j][i])ans=max(ans,dp(j)+1); } return ans; } int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&n); memset(maps,0,sizeof(maps)); for (int i=1;i<=n;i++){ scanf("%d%d",&a[i].x,&a[i].y); if (a[i].x<a[i].y)swap(a[i].x,a[i].y); } rep(i,1,n) { rep(j,1,n){ if ((a[i].x>a[j].x) && (a[i].y>a[j].y)){ maps[i][j]=1; } } } int ans=0; memset(d,0,sizeof(d)); rep(i,1,n){ ans=max(ans,dp(i)); } printf("%d\n",ans); } return 0; }
有不懂欢迎咨询
QQ:1326487164(添加时记得备注)