嵌套矩形问题(最长路及其字典序)
有n个举行,选出尽量多的矩阵排成一排,使得除了最后一个之外,每一个矩形可以嵌套在下一个矩形内,并且打印

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <queue>
#include <map>
#include <set>

using namespace std;

const int INF = 0xffffff;
const double esp = 10e-8;
const double Pi = 4 * atan(1.0);
const int maxn =  100+ 10;
const long long mod =  1000000007;
const int dr[] = {1,0,-1,0,-1,1,-1,1};
const int dc[] = {0,1,0,-1,1,-1,-1,1};
typedef long long LL;

LL gac(LL a,LL b){
    return b?gac(b,a%b):a;
}
int n;
bool graph[maxn][maxn];
struct G{
    int a,b;
};
G g[maxn];
int d[maxn][maxn];
bool judge(int x,int y){
    if(g[x].a < g[y].a && g[x].b < g[y].b)
        return 1;
    if(g[x].a < g[y].b && g[x].b < g[y].a)
        return 1;
    return 0;
}
int dp(int i,int tt){
    if(d[tt][i] > 0)
        return d[tt][i];
    d[tt][i] = 1;
    for(int j = 1;j <= n;j++){
        if(graph[i][j]){
            d[tt][i] = max(d[tt][i],dp(j,tt)+1);
        }
    }
    return d[tt][i];
}
void print_ans(int i,int tt){
    printf("%d ",i);
    for(int j = 1;j <= n;j++){
        if(graph[i][j] && d[tt][i] == d[tt][j] + 1){
            print_ans(j,tt);
            break;
        }
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
   // freopen("output.txt","w",stdout);
#endif
    int t;
    scanf("%d",&t);
    while(t--){
    scanf("%d",&n);
    for(int i = 1;i <= n;i++){
        getchar();
        char ch;
        scanf("%c%d%d",&ch,&g[i].a,&g[i].b);
        //int fin = -1;
        for(int j = 1;j < i;j++){
            if(judge(i,j)){
                graph[i][j] = 1;
            }
            if(judge(j,i)){
                graph[j][i] = 1;
            }
        }
    }
    int ans = -1;
    int m;
    memset(d,0,sizeof(d));
    for(int i = 1;i <= n;i++){
        int tmp = dp(i,i);
        if(tmp > ans){
            m = i;
            ans = tmp;
        }
    }
    printf("%d\n",ans);
    print_ans(m,m);
    }
    return 0;
}
View Code

相关文章:

  • 2022-01-22
  • 2021-09-08
  • 2021-07-02
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2021-04-04
  • 2022-03-10
猜你喜欢
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2021-10-08
  • 2021-06-13
相关资源
相似解决方案