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<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 typedef long long LL;
10
11 int k,n;
12 int dp[33],pre[33];
13 struct node
14 {
15     int an[13];
16     int id;
17     friend bool operator < (node a,node b)
18     {
19         for (int i=0 ;i<n ;i++)
20         {
21             if (a.an[i] != b.an[i]) return a.an[i] <  b.an[i];
22         }
23     }
24 }arr[33];
25
26 void printOut(int u)
27 {
28     if (pre[u]!=-1) printOut(pre[u]);
29     if (pre[u]==-1) printf("%d",arr[u].id+1 );
30     else printf(" %d",arr[u].id+1 );
31 }
32
33 int main()
34 {
35     while (scanf("%d%d",&k,&n)!=EOF)
36     {
37         memset(dp,0,sizeof(dp));
38         memset(pre,-1,sizeof(pre));
39         for (int i=0 ;i<k ;i++)
40         {
41             for (int j=0 ;j<n ;j++)
42                 scanf("%d",&arr[i].an[j]);
43             arr[i].id=i;
44             sort(arr[i].an,arr[i].an+n);
45         }
46         sort(arr,arr+k);
47 //        for (int i=0 ;i<k ;i++)
48 //        {
49 //            for (int j=0 ;j<n ;j++)
50 //                cout<<arr[i].an[j]<<" ";
51 //            cout<<endl;
52 //        }
53         for (int i=0 ;i<k ;i++)
54         {
55             int temp=0;
56             for (int j=0 ;j<i ;j++)
57             {
58                 int flag=0;
59                 for (int u=0 ;u<n ;u++)
60                     if (arr[i].an[u]<=arr[j].an[u]) {flag=1;break; }
61                 if (!flag && dp[j]>temp)
62                 {
63                     temp=dp[j];
64                     pre[i]=j;
65                 }
66             }
67             dp[i]=temp+1;
68         }
69         int maxlen=-1,num=0;
70         for (int i=0 ;i<k ;i++)
71         {
72             if (dp[i]>maxlen)
73             {
74                 maxlen=dp[i];
75                 num=i;
76             }
77         }
78         printf("%d\n",maxlen);
79         printOut(num);
80         printf("\n");
81     }
82     return 0;
83 }
时间: 2024-10-12 01:49:29

UVA 103 Stacking Boxes n维最长上升子序列的相关文章

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 (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

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

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

SGU 199 Beautiful People 二维最长递增子序列

题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20885 题意: 求二维最长严格递增子序列. 题解: O(n^2)的算法很好想,不过这里会t掉,只能O(nlogn) 于是用二分来维护: 先把所有的数按x递增排序,x相同的按y递减排序(这里之所以要按y递减排序是因为为了写代码方便,递减的话你后面基本就只要考虑y的大小,如果不递减,你还要考虑x的大小的,具体的可以自己思考一下) 排完序之后我们接下来就只考虑y的大小

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a