UVa 103 - Stacking Boxes (LIS,打印路径)

链接:UVa 103

题意:给n维图形,它们的边长是{d1,d2,d3...dn},  对于两个n维图形,求满足其中一个的所有边长

按照任意顺序都一一对应小于另一个的边长,这样的最长序列的个数,并且打印任意一个最长子串的路径,

例如:a(9,5,7,3),b(6,10,8,2),c(9,7,5,1),a和b不满足,但c和b满足

分析:首先对没组边长从小到大排序,再对各组图形按最小边排序,再求最大子串,

对于打印路径,可以逆序循环,也可递归求解

#include<cstdio>
#include<algorithm>
using namespace std;
int dp[35],path[35],num,m,k;
struct stu
{
    int a[12],id;
}s[35];
int cmp(struct stu s1,struct stu s2)
{
    return s1.a[1]<s2.a[1];
}
/*void back_path1(int i)
{
    if(path[i]!=i)
        back_path1(path[i]);
    printf("%d",s[i].id);
    num++;
    if(num!=m)
        printf(" ");
    else
        printf("\n");
}*/
/*void back_path2(int i)
{
    if(k--){
        back_path2(path[i]);
        printf("%d",s[i].id);
        num++;
        if(num!=m)
            printf(" ");
        else
            printf("\n");
    }
}*/
int main()
{
    int i,j,n,pos,b[1005];
    while(scanf("%d%d",&m,&n)!=EOF){
        for(i=1;i<=m;i++){
            s[i].id=i;
            for(j=1;j<=n;j++)
                scanf("%d",&s[i].a[j]);
            sort(s[i].a+1,s[i].a+n+1);      //对每个图形的边长排序
        }
        sort(s+1,s+m+1,cmp);   //对各个图形之间,按最小边长的大小排序
        for(i=1;i<=m;i++){
            dp[i]=1;
            path[i]=i;
            for(j=1;j<i;j++){
                for(k=1;k<=n;k++)
                    if(s[j].a[k]>=s[i].a[k])
                        break;
                if(k==n+1&&dp[j]+1>dp[i]){
                    dp[i]=dp[j]+1;
                    path[i]=j;
                }
            }
        }
        pos=1;
        for(i=2;i<=m;i++)
            if(dp[i]>dp[pos])
                pos=i;
        m=dp[pos];
        printf("%d\n",m);
        b[1]=s[pos].id;       //先把最后一个编号加入
        i=2;
        for(j=pos-1;j>=1;j--){          //逆序循环求路径
            for(k=1;k<=n;k++)
                if(s[j].a[k]>=s[pos].a[k])
                    break;
            if(k==n+1&&dp[j]+1==dp[pos]){
                b[i++]=s[j].id;
                dp[pos]--;
            }
            if(dp[pos]==1)
                break;
        }
        for(j=i-1;j>1;j--)
            printf("%d ",b[j]);
        printf("%d\n",b[1]);
        /*num=0;            //递归方法1
        back_path1(pos);*/
        /*num=0;            //递归方法2
        k=m;
        back_path2(pos);*/
    }
    return 0;
}

UVa 103 - Stacking Boxes (LIS,打印路径)

时间: 2024-09-30 10:37:05

UVa 103 - Stacking Boxes (LIS,打印路径)的相关文章

uva 103 Stacking Boxes(最长上升子序列)

Description  Stacking Boxes  Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions

UVa 103 Stacking Boxes --- DAG上的动态规划

UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最多能套几个箱子. 第一行输入为n,m,之后是n行m维的箱子 解题思路:嵌套关系是二元关系,因此这题即在DAG上做动态规划, 只不过将二维的判断改成了n维,其他不变. 详细看考:DAG上的动态规划之嵌套矩形  (ps:这题可以理解成嵌套m边形) /* UVa 103 Stacking Boxes --

UVA - 103 - Stacking Boxes (动态规划)

UVA - 103 Stacking Boxes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when

uva 103 Stacking Boxes (DAG)

uva 103 Stacking Boxes Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and a

UVA 103 Stacking Boxes n维最长上升子序列

题目链接:UVA - 103 题意:现有k个箱子,每个箱子可以用n维向量表示.如果一个箱子的n维向量均比另一个箱子的n维向量大,那么它们可以套接在一起,每个箱子的n维向量可以互相交换值,如箱子(2,6)可以和箱子(7,3)套接在一起.求出套接的箱子最多的个数前提下任意一种解决方案. 算法:抛开n维不看,本题就是一个DP的最长上升子序列问题,现在加上了n维的限制,想想也不是很难吧,在DP过程中判断每一维都满足条件即可. 1 #include<iostream> 2 #include<cst

UVa - 103 - Stacking Boxes

Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and analyzing the topology o

UVA 103 Stacking Boxes (DP)

Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and analyzing the topology o

Uva 10131 Is Bigger Smarter? (LIS,打印路径)

option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意:给定若干大象的体重及智商值.求满足大象体重严格递增,智商严格递减的序列的最大个数. 并打印随意一组取得最大值的序列的大象编号 分析:这个是LIS的应用,仅仅只是推断条件有两个,能够先对大象的体重排序,可是要打印路径. 那就必须得回溯求路径.能够直接逆序循环求,当然递归也是一个好的选择 #include<

Uva 642-CD(0-1背包+打印路径)

题目链接:点击打开链接 裸01背包 ,此题中 价值即体积... 打印路径..不多说了 一维的没看懂..上个二维的 #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio>