-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path103.c
214 lines (168 loc) · 4.15 KB
/
103.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#undef DEBUG
#define MAX_BOX_DIMENSION (10)
#define MAX_BOX_NUM (30)
struct boxStackRecord
{
int previousBox;
int stackCount;
};
struct boxDataRecord
{
int dimension[MAX_BOX_DIMENSION];
int index;
};
static struct boxDataRecord boxData[MAX_BOX_NUM];
static int boxDimension, boxNum;
static struct boxStackRecord boxStack[MAX_BOX_NUM];
static int maxStack, maxStackBox;
static int compareBoxDimension(const void *a, const void *b)
{
int dimA = *(int *)a, dimB = *(int *)b;
/*
* Return positive while latter is larger, so we can get descending order
*/
return (dimB - dimA);
}
static int compareBox(const void *a, const void *b)
{
const struct boxDataRecord *boxA = (const struct boxDataRecord *)a,
*boxB = (const struct boxDataRecord *)b;
const int *boxDimA = boxA->dimension, *boxDimB = boxB->dimension;
int i;
/*
* Return negative while latter is larger, so we can get ascending order
*/
for ( i = 0; i < boxDimension; i++ ) {
if ( boxDimA[i] > boxDimB[i] )
return 1;
else if ( boxDimA[i] < boxDimB[i] )
return -1;
}
return 0;
}
static int isBoxLarger( struct boxDataRecord *boxA, struct boxDataRecord *boxB )
{
int *boxDimA = boxA->dimension, *boxDimB = boxB->dimension;
int i;
for ( i = 0; i < boxDimension; i++ ) {
if( boxDimA[i] <= boxDimB[i] )
return -1;
}
return 1;
}
static int getBoxProfile(void)
{
int i, j, ret;
if ( scanf( "%d %d", &boxNum, &boxDimension ) <= 0 ) {
return 0;
}
memset( boxData, 0, sizeof(boxData) );
for ( i = 0; i < boxNum; i++ ) {
boxData[i].index = i;
for ( j = 0; j < boxDimension; j++ ) {
scanf( "%d", &boxData[i].dimension[j] );
}
/*
* Sort dimensions of box in descending order
*/
qsort( boxData[i].dimension, boxDimension, sizeof(int), compareBoxDimension );
}
/*
* Order boxes in dimension ascending order
*/
qsort( boxData, boxNum, sizeof(struct boxDataRecord), compareBox );
return 1;
}
static void findMaxStack(void)
{
int i, j;
maxStack = 0;
maxStackBox = -1;
for ( i = 0; i < boxNum; i++ ) {
boxStack[i].previousBox = -1;
boxStack[i].stackCount = 0;
for ( j = 0; j < i; j++ ) {
if ( isBoxLarger( &boxData[i], &boxData[j] ) > 0 ) {
#ifdef DEBUG
printf( "Box %d can put in Box %d\n", j, i );
#endif
/*
* box[j] can be stack into box[i]
*/
if( boxStack[j].stackCount > boxStack[i].stackCount ) {
boxStack[i].stackCount = boxStack[j].stackCount;
boxStack[i].previousBox = j;
#ifdef DEBUG
printf( "Box %d, stack count %d, previous Box %d\n", i, boxStack[i].stackCount, boxStack[i].previousBox );
#endif
}
}
}
boxStack[i].stackCount++;
if ( boxStack[i].stackCount > maxStack ) {
maxStack = boxStack[i].stackCount;
maxStackBox = i;
#ifdef DEBUG
printf( "max Stack %d, maxStackBox %d\n", maxStack, maxStackBox );
#endif
}
}
}
static void dumpMaxStack( int currentBoxIndex )
{
if( currentBoxIndex < 0 )
return;
dumpMaxStack( boxStack[currentBoxIndex].previousBox );
printf( "%d ", boxData[currentBoxIndex].index + 1 );
}
static void printResult(void)
{
printf( "%d\n", maxStack );
#ifdef DEBUG
printf( "Max Stack Box %d\n", maxStackBox );
#endif
dumpMaxStack( maxStackBox );
printf( "\n" );
}
#ifdef DEBUG
static void printBoxProfile(void)
{
int i, j;
printf( "======= Box Profile =======\n" );
printf( "%d boxes of dimension %d\n", boxNum, boxDimension );
for ( i = 0; i < boxNum; i++ ) {
printf( "Box %2d, index %2d : ", i, boxData[i].index );
for ( j = 0; j < boxDimension; j++ ) {
printf( "%4d ", boxData[i].dimension[j] );
}
printf( "\n" );
}
printf( "==========================\n" );
}
static int printStackInfo(void)
{
int i, j;
printf( "======= Stack Info =======\n" );
for ( i = 0; i < boxNum; i++ ) {
printf( "Box %2d, index %d, previous box %2d, stack count %2d\n", i, boxData[i].index, boxStack[i].previousBox, boxStack[i].stackCount );
}
printf( "==========================\n" );
}
#endif
int main( int argc, char *argv[] )
{
while ( getBoxProfile() ) {
#ifdef DEBUG
printBoxProfile();
#endif
findMaxStack();
#ifdef DEBUG
printStackInfo();
#endif
printResult();
}
return 0;
}