Background
Some concepts in Mathematics and Computer Science are simple in one ortwo dimensions butbecome more complex when extended to arbitrary dimensions. Considersolving differential equations in several dimensions and analyzing thetopology of an n-dimensional hypercube. The former is much morecomplicated than its one dimensional relative while the latter bears aremarkable resemblance to its ``lower-class'' cousin.
The Problem
Consider an n-dimensional ``box'' given by its dimensions. In twodimensions the box (2,3) might represent a box with length 2 units andwidth 3 units. In three dimensions the box (4,8,9) can represent a box (length, width, and height). In 6 dimensions itis, perhaps, unclear what the box (4,5,6,7,8,9) represents; but we cananalyze properties of the box such as the sum of its dimensions.
In this problem you willanalyze a property of a group of n-dimensional boxes.You are to determine the longest nesting string of boxes, thatis a sequence of boxes such that each box nests in box ( .
A box D = ( ) nests in a box E = ( )if there is some rearrangement of the such that when rearrangedeach dimension is less than the corresponding dimension in box E. This loosely corresponds to turning box D to see if it will fit in boxE. However, since any rearrangement suffices, box D can be contorted, not justturned (see examples below).
For example, the box D = (2,6) nests in the box E = (7,3) since D can berearranged as (6,2) so that each dimension is less than thecorresponding dimension in E. The box D = (9,5,7,3) does NOT nest in thebox E = (2,10,6,8) since no rearrangement of D results in a box thatsatisfies the nesting property, but F = (9,5,7,1) does nest in box E sinceF can be rearranged as (1,9,5,7) which nests in E.
Formally, we define nesting as follows:box D = ( ) nests in box E = ( ) if there is a permutation of such that ( ) ``fits'' in ( ) i.e., if for all .
The Input
The input consists of a series of box sequences. Each box sequencebegins with a line consisting of the the number of boxes kin the sequencefollowed by the dimensionality of the boxes, n (on the same line.)
This line is followed by k lines, one line per boxwith the n measurements of each box onone line separated by one or more spaces. The line in thesequence ( ) gives the measurements for the box.
There may be several box sequences inthe input file. Your program should process all of them and determine,for each sequence, which of the k boxes determine the longest nestingstring and the length of that nesting string (the number ofboxes in the string).
In this problem the maximum dimensionality is 10 and the minimumdimensionality is 1. The maximum number of boxes in a sequence is 30.
The Output
For each box sequence in the input file, output the length of thelongest nesting string on one line followed on the next lineby a list of the boxesthat comprise this string in order. The ``smallest'' or ``innermost'' box of the nesting string should belisted first, the next box (if there is one) should be listed second,etc.
The boxes should benumbered according to the order in which they appeared in the input file(first box is box 1, etc.).
If there is more than one longest nesting string then any oneof them can be output.
Sample Input
5 2 3 7 8 10 5 2 9 11 21 18 8 6 5 2 20 1 30 10 23 15 7 9 11 3 40 50 34 24 14 4 9 10 11 12 13 14 31 4 18 8 27 17 44 32 13 19 41 19 1 2 3 4 5 6 80 37 47 18 21 9
Sample Output
5 3 1 2 4 5 4 7 2 5 6
DAG上的动态规划,求固定终点的最长路径
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef struct{
int a[11];
}node;
node p[33];
int G[33][33];
int dist[33];
int n, k;
int dp(int i){
int &ans = dist[i];
if(ans > 0)
return ans;
ans = 1;
for(int j = 1; j <= n; ++j)
{
if(G[i][j])
{
if(ans < dp(j) + 1)
{
ans = dp(j) + 1;
}
}
}
return ans;
}
void print_path(int i){
printf("%d ", i);
for(int j = 1; j <= n; ++j)
{
if(G[i][j] && dist[i] == dist[j] + 1)
{
print_path(j);
break;
}
}
}
int main(void){
while(scanf("%d %d", &n, &k) != EOF)
{
memset(G, 0, sizeof(G));
memset(dist, 0, sizeof(dist));
for(int i = 1; i <= n; ++i)
{
for(int j = 0; j < k; ++j)
{
scanf("%d", &p[i].a[j]);
}
sort(p[i].a, p[i].a + k);
}
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
int ok = 1;
for(int t = 0; t < k; ++t)
{
if(p[i].a[t] >= p[j].a[t])
{
ok = 0;
break;
}
}
if(ok)
G[i][j] = 1;
}
}
// for(int i = 1; i <= n; ++i)
// {
// for(int j = 1; j <= n; ++j)
// {
// printf("%d", G[i][j]);
// }
// printf("\n");
// }
int mmax = -1;
int minpos = -1;
for(int i = 1; i <= n; ++i)
{
if(mmax < dp(i))
{
mmax = dp(i);
minpos = i;
}
}
printf("%d\n", mmax);
print_path(minpos);
printf("\n");
}
return 0;
}