This repository has been archived by the owner on May 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp103.c
127 lines (105 loc) · 2.39 KB
/
p103.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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#define IN "p103.in"
#define OUT "p103.out"
typedef struct box {
int d[10];
} box;
void printbox(box ins, int ndim) {
int i;
for(i = 0; i < ndim; i++)
printf("%i ", ins.d[i]);
printf("\n");
}
int compar(const void *a, const void *b) {
return *((int*)a) - *((int*)b);
}
int nest(const box *a, const box *b, const int dim) {
int i;
for(i = 0; i < dim; i++)
if (a->d[i] >= b->d[i]) return 0;
return 1;
}
int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
close (0); open(IN, O_RDONLY);
/* close (1); open(OUT, O_WRONLY | O_CREAT, 0600); */
#endif
int nbox, ndim;
box instancia[30];
int matriz[30][30];
int nestcont[30];
int i, j;
int best;
int nbest;
int resp[30];
int respt;
while (!feof(stdin)) {
/* instancia */
if (scanf("%i %i", &nbox, &ndim) != 2) break;
/* printf("%i caixas com %i dimensoes\n", nbox, ndim); */
for(i = 0; i < nbox; i++) {
for(j = 0; j < ndim; j++)
scanf("%i", &instancia[i].d[j]);
/* printbox(instancia[i], ndim); */
}
/* ordena dimensoes das caixas */
for(i = 0; i < nbox; i++) {
qsort(instancia[i].d, ndim, sizeof(int), &compar);
/* printbox(instancia[i], ndim);*/
}
/* constroi matriz de encaixe */
best = 0;
for(i = 0; i < nbox; i++) {
nestcont[i] = 1;
for(j = 0; j < nbox; j++) {
if (i==j) {
matriz[i][j] = 0;
continue; /* mesma caixa */
}
if (nest(&instancia[i], &instancia[j], ndim)) {
/*printf("%i entra em %i\n", i, j); */
nestcont[i]++;
matriz[i][j] = 1;
} else {
/*printf("%i nao entra em %i\n", i, j);*/
matriz[i][j] = 0;
}
}
if (nestcont[best] < nestcont[i])
best = i;
}
/*for(i = 0; i < nbox; i++) {
for(j = 0; j < nbox; j++) {
printf("%i ", matriz[i][j]);
}
printf("| %i\n", nestcont[i]);
}*/
/*printf("melhor %i\n", best);*/
respt = 0;
resp[respt++] = best;
while (nestcont[best] != 1) {
nbest = -1;
for(i = 0; i < nbox; i++) {
if (matriz[best][i] == 1) {
if ((nbest == -1) || (nestcont[i] > nestcont[nbest]))
nbest = i; /* proxima da lista*/
}
}
best = nbest;
/*printf("melhor %i\n", best);*/
resp[respt++] = best;
}
/* Resposta!!!*/
printf("%i\n", respt);
for(i = 0; i < respt; i++)
if (i == 0)
printf("%i", resp[i]+1);
else
printf(" %i", resp[i]+1);
printf("\n");
}
return 0;
}