Title Link:UVA-103
Test instructions: The existing K-box, each box can be represented by an n-dimensional vector. If the n-dimensional vectors of a box are larger than the n-dimensional vectors of another box, then they can be nested together, and the n-dimensional vectors of each box can exchange values, such as chests (2,6) that can be nested with chests (7,3). Find out the maximum number of boxed boxes under any one of the solutions.
Algorithm: Throw open n-dimensional does not look, the topic is a DP of the longest ascending sub-sequence problem, now add the n-dimensional limit, think it is not difficult, in the DP process to judge each dimension to meet the conditions can be.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7 #defineINF 0x7fffffff8 using namespacestd;9typedefLong LongLL;Ten One intK,n; A intdp[ -],pre[ -]; - structnode - { the intan[ -]; - intID; -FriendBOOL operator<(node A,node b) - { + for(intI=0; i<n; i++.) - { + if(A.an[i]! = B.an[i])returnA.an[i] <B.an[i]; A } at } -}arr[ -]; - - voidPrintOut (intu) - { - if(pre[u]!=-1) PrintOut (Pre[u]); in if(pre[u]==-1) printf ("%d", arr[u].id+1 ); - Elseprintf"%d", arr[u].id+1 ); to } + - intMain () the { * while(SCANF ("%d%d", &k,&n)! =EOF) $ {Panax NotoginsengMemset (DP,0,sizeof(DP)); -memset (pre,-1,sizeof(pre)); the for(intI=0; i<k; i++.) + { A for(intj=0; j<n; j + +) thescanf"%d",&arr[i].an[j]); +Arr[i].id=i; -Sort (arr[i].an,arr[i].an+n); $ } $Sort (arr,arr+k); - //for (int i=0; i<k; i++) - // { the //for (int j=0; j<n; j + +) - //cout<<arr[i].an[j]<< "";Wuyi //cout<<endl; the // } - for(intI=0; i<k; i++.) Wu { - inttemp=0; About for(intj=0; j<i; j + +) $ { - intflag=0; - for(intu=0; u<n; u++.) - if(Arr[i].an[u]<=arr[j].an[u]) {flag=1; Break; } A if(!flag && dp[j]>temp) + { thetemp=Dp[j]; -pre[i]=J; $ } the } thedp[i]=temp+1; the } the intmaxlen=-1, num=0; - for(intI=0; i<k; i++.) in { the if(dp[i]>maxlen) the { Aboutmaxlen=Dp[i]; thenum=i; the } the } +printf"%d\n", maxlen); - printOut (num); theprintf"\ n");Bayi } the return 0; the}
UVA 103 Stacking Boxes n-dimensional longest ascending subsequence