50148. Stack Rectangles, Again
難度:4.5/5
Second Try: 4/5 Used Time 18:401
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#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
typedef struct{
int l;
int w;
} s_box;
int n;
s_box box[128];
int max_size = 0;
int target_area;
int can_place(s_box* a, s_box* b){
#ifdef LARGEONSMALL
if(a->l >= b->l && a->w >= b->w) return 1;
else if(a->l >= b->w && a->w >= b->l) return 1;
else return 0;
#endif
#ifdef SMALLONLARGE
if(a->l <= b->l && a->w <= b->w) return 1;
else if(a->l <= b->w && a->w <= b->l) return 1;
else return 0;
#endif
}
int get_area_sum(int arr[], int arr_size){
int ans = 0;
for(int i = 0; i < arr_size; i++){
int idx = arr[i];
ans += (box[idx].l * box[idx].w);
}
return ans;
}
void dfs(int arr[], int arr_size, int now_idx){
if(now_idx == n){
if(arr_size < max_size) return;
int this_area_sum = get_area_sum(arr, arr_size);
if(arr_size > max_size){
max_size = arr_size;
target_area = this_area_sum;
}
else{
#ifdef MAXAREASUM
target_area = max(target_area, this_area_sum);
#endif
#ifdef MINAREASUM
target_area = min(target_area, this_area_sum);
#endif
}
}
else{
// choose (if possible)
if(arr_size == 0 || can_place(&box[now_idx], &box[arr[arr_size - 1]])){
arr[arr_size] = now_idx;
dfs(arr, arr_size + 1, now_idx + 1);
arr[arr_size] = -1;
}
// no choose
dfs(arr, arr_size, now_idx + 1);
}
}
int main(){
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d %d", &box[i].l, &box[i].w);
}
#ifdef MAXAREASUM
target_area = INT_MIN;
#endif
#ifdef MINAREASUM
target_area = INT_MAX;
#endif
int arr[128] = {0};
dfs(arr, 0, 0);
printf("%d %d\n", max_size, target_area);
}