uva 103 Stacking Boxes

10 篇文章 0 订阅

Stacking Boxes 

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 tex2html_wrap_inline40 (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 tex2html_wrap_inline44 such that each box tex2html_wrap_inline46 nests in box tex2html_wrap_inline48 ( tex2html_wrap_inline50 .

A box D = ( tex2html_wrap_inline52 ) nests in a box E = ( tex2html_wrap_inline54 )if there is some rearrangement of the tex2html_wrap_inline56 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 = ( tex2html_wrap_inline52 ) nests in box E = ( tex2html_wrap_inline54 ) if there is a permutation tex2html_wrap_inline62 of tex2html_wrap_inline64 such that ( tex2html_wrap_inline66 ) ``fits'' in ( tex2html_wrap_inline54 ) i.e., if tex2html_wrap_inline70 for all tex2html_wrap_inline72 .

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 tex2html_wrap_inline82 line in thesequence ( tex2html_wrap_inline84 ) gives the measurements for the tex2html_wrap_inline82 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

思路:转化为LIS。先对每一行排序,再对整体排序,最后LIS。


源代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>

using namespace std;
#define maxn 35
#define inf 0x7ffffff
int n,m;
struct Node{
    int arr[maxn];
    int id;
};
int dp[maxn];
Node node[maxn];
int ans[maxn];
int cmp(Node a,Node b){
    for(int i = 0; i < m; i++){
        if(a.arr[i] < b.arr[i]){
            return 1;
        }
        if(a.arr[i] > b.arr[i]){
            return 0;
        }
    }
}
bool check(int x,int y);
int main()
{
    while(scanf("%d%d",&n,&m) != EOF ){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                scanf("%d",&node[i].arr[j]);
            }
            node[i].id = i+1;
            sort(node[i].arr,node[i].arr + m);
        }
        sort(node,node+n,cmp);

        fill(dp,dp+n+1,1);
        for(int i = 1; i < n; i++){
            for(int j = i - 1;j >= 0; j--){
                if(check(j,i)){
                    dp[i] = max(dp[i],dp[j]+1);
                }
            }
        }
        for(int i = 0; i < n; i++){
            dp[n] = max(dp[n],dp[i]);
        }
        printf("%d\n",dp[n]);
        int cnt = dp[n];
        int cur = n -1 ;
        for(int i = n -1; i >= 0; i--){
            if(cnt == 0){
                break;
            }
            if(dp[i] == cnt && (dp[n] == cnt || check(i,cur))){
                ans[cnt] = node[i].id;
                cnt --;
                cur = i;
            }
        }
        for(int i = 1; i <= dp[n]; i++){
            printf("%d ",ans[i]);
        }
        printf("\n");
    }
    return 0;
}
bool check(int x,int y){
    for(int i = 0; i < m; i++){
        if(node[x].arr[i] >= node[y].arr[i]){
            return false;
        }
    }
    return true;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值