This C++ program is to solve the B problem during the 2012 CUMCM. The goal is to make the most use of the outside floor of a house to place the solar cells so that to maximize the profit.


12
0,12SM

a,b,c
SMSM

1 Matlab
2 Matlab
3 C/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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
 
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <fstream>
#include <queue>
 
using namespace std;
 
#define eps 0.1
 
struct buttery{         //
    int sty;            //123
    double length;
    double width;
    double s;           //
    double thr;         //
    double eff;         //()
    double price;
    double elec;        //
    double val;         //
    double av_val;      //
    int ori_ind;
}but[100];
 
int cnt_pbut = 0;             //
struct put_but{          //
    int index;           //
    double len, wid;
    double x, y;
}pbut[100];
 
/////////////////////////////////////////////////////////////////////////////////////////////////
 
int but_n;                 //
double min_wid = 100000.0;
double min_len = 100000.0;
void init_but(){           //
    scanf("%d", &but_n);
    for(int i = 0; i < but_n; i ++){
        but[i].ori_ind = i;
        scanf("%d", &but[i].sty);
        scanf("%lf %lf %lf %lf %lf", &but[i].length, &but[i].width, &but[i].price, &but[i].thr, &but[i].eff);
        but[i].length /= 1000;
        but[i].width /= 1000;
        but[i].s = but[i].length * but[i].width;
        min_wid = min(min_wid, but[i].width);
        min_len = min(min_len, but[i].length);
        but[i].elec = 0;
        but[i].val = 0;
        //printf("z");
    }
}
 
double w_len, w_wid;
void init_wall(){            //
    w_len = 7.1;
    w_wid = 3.2;
}
 
int day_num;
int hour_num;void print_ans(){
    int i, j, k;
    printf("\n");
    for(i = 1; i
double light[100][100];
void init_light(){         //
    int i, j, k;
    scanf("%d %d", &day_num, &hour_num);
    for(i = 1; i <= hour_num; i ++){
        for(j = 1; j <= day_num; j ++){
            scanf("%lf", &light[j][i]);   //
        }
    }
}
 
////////////////////////////////////////////////////////////////////////////////////////////////////////
 
double cal_electricity(){ //
    int i, j, k;void print_ans(){
    int i, j, k;
    printf("\n");
    for(i = 1; i
    for(i = 0; i < but_n; i ++){
        for(j = 1; j <= day_num; j ++){
            for(k = 1; k <= hour_num; k ++){
                if(light[j][k] > but[i].thr){
                    double ttmp = but[i].s * light[j][k] * but[i].eff;
                    if(but[i].sty == 1){
                        if(light[j][k] <= 200)
                        ttmp *= 0.05 * 0.001;
                    }
                    but[i].elec += ttmp;
                }
                //printf("i=%d\nj=%d\nk=%d\n", i, j, k);
            }
        }
        //printf("b");
        but[i].val = but[i].elec * 0.5;
        but[i].av_val = but[i].val / but[i].s;
    }
}
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////
 
bool cmp(buttery A, buttery B){      //
    return A.av_val >= B.av_val;
}
 
struct cpoint{
    double x;
    double y;
    int flag;                                //10
    double w;
    double l;
}cp[1000];   //
int cp_num = 0;  //
cpoint c_tmp;
 
bool cmp2(cpoint A, cpoint B){         //x
    return A.x <= B.x;
}
 
bool ok_need(buttery bb, double cw_max, double cl_max){             //
    if(bb.width - cw_max > eps || bb.length - cl_max > eps)
        return 0;
    return 1;
}
 
bool ok_put(buttery bb, cpoint cc){   //
    if(cc.flag)
        return 0;
    if(bb.length + cc.x - w_len > eps || bb.width + cc.y - w_wid > eps)
        return 0;
    cc.flag = 1;
    return 1;
}
 
struct okpla{          //
    double x;
    double y;
    int sm;            //
    int index;         //
}ok_pla[1000];
okpla okpla_tmp;
int okpla_num = 0;         //
bool cmp3(okpla A, okpla B){
    if(A.sm != B.sm)
        return A.sm < B.sm;
    else
        return A.x < B.x;
}
 
int but_ind;
 
void RP(){              //
    sort(but, but + but_n, cmp);
    int i, j, k;
    double cl_max = 0;      //
    double cw_max = 0;
    c_tmp.x = 0;
    c_tmp.y = 0;
    c_tmp.flag = 0;
    c_tmp.w = w_wid;
    c_tmp.l = w_len;
    cl_max = w_len;
    cw_max = w_wid;
    cp[cp_num] = c_tmp;
    cp_num ++;
 //   while(cp[cp_num - 1].x - (w_len - min_len) < eps && cp[cp_num - 1].y -(w_wid - min_wid) < eps){
    while(1){              //
        int have_put = 0;  //1
        printf("Z");
        for(i = 0; i < but_n; i ++){           //
            okpla_num = 0;
            if(!ok_need(but[i], cw_max, cl_max))
                continue;
            printf("i=%d\n", i);
            for(j = 0; j < cp_num; j ++){      //
                if(cp[j].flag)
                    continue;
                if(!ok_put(but[i], cp[j])){
                    continue;
                }
                if(but[i].length <= cp[j].l && but[i].width <= cp[j].w){
                    have_put = 1;
                    but_ind = i;
                    printf("\nbut_ind = %d\n", but_ind);
                    okpla_tmp.sm = 2;
                    if(cp[j].l - but[i].length < eps)
                        okpla_tmp.sm --;
                    if(cp[j].w - but[i].width < eps)
                        okpla_tmp.sm --;
                    okpla_tmp.x = cp[j].x;
                    okpla_tmp.y = cp[j].y;
                    okpla_tmp.index = j;
                    ok_pla[okpla_num ++] = okpla_tmp;
                }
            }
            if(have_put){           //
                int indtmp = ok_pla[0].index;
                pbut[cnt_pbut].len = but[but_ind].length;
                pbut[cnt_pbut].wid = but[but_ind].width;
                pbut[cnt_pbut].x = cp[indtmp].x;
                pbut[cnt_pbut].y = cp[indtmp].y;
                pbut[cnt_pbut ++].index = but_ind;
                sort(ok_pla, ok_pla + okpla_num, cmp3);
                cp[indtmp].flag = 1;
                printf("sm=%d\n",ok_pla[0].sm);
                if(ok_pla[0].sm == 2){
                        c_tmp.x = cp[indtmp].x + but[but_ind].length;
                        c_tmp.y = cp[indtmp].y;
                        c_tmp.flag = 0;
                        c_tmp.w = but[but_ind].width;
                        c_tmp.l = cp[indtmp].l - but[but_ind].length;
                        if(c_tmp.w > min_wid && c_tmp.l > min_len){
                            cp[cp_num ++] = c_tmp;
                            printf("c_tmp.x=%lf, c_tmp.y=%lf", c_tmp.x, c_tmp.y);
                        }
                        c_tmp.x = cp[indtmp].x;
                        c_tmp.y = cp[indtmp].y + but[but_ind].width;
                        c_tmp.flag = 0;
                        c_tmp.l = but[but_ind].length;
                        c_tmp.w = cp[indtmp].w - but[but_ind].width;
                        if(c_tmp.w > min_wid && c_tmp.l > min_len){
                            cp[cp_num ++] = c_tmp;
                            printf("c_tmp.x=%lf, c_tmp.y=%lf", c_tmp.x, c_tmp.y);
                        }
                }
                else if(ok_pla[0].sm == 1){
                    if(but[but_ind].width == cp[indtmp].w){
                        c_tmp.x = cp[indtmp].x + but[but_ind].length;
                        c_tmp.y = cp[indtmp].y;
                        c_tmp.flag = 0;
                        c_tmp.w = cp[indtmp].w;
                        c_tmp.l = cp[indtmp].l - but[but_ind].length;
                        if(c_tmp.w > min_wid && c_tmp.l > min_len){
                            cp[cp_num ++] = c_tmp;
                            printf("c_tmp.x=%lf, c_tmp.y=%lf", c_tmp.x, c_tmp.y);
                        }
                    }
                    else{
                        c_tmp.x = cp[indtmp].x;
                        c_tmp.y = cp[indtmp].y + but[but_ind].width;
                        c_tmp.flag = 0;
                        c_tmp.l = cp[indtmp].l;
                        c_tmp.w = cp[indtmp].w - but[but_ind].width;
                        if(c_tmp.w > min_wid && c_tmp.l > min_len){
                            cp[cp_num ++] = c_tmp;
                            printf("c_tmp.x=%lf, c_tmp.y=%lf", c_tmp.x, c_tmp.y);
                        }
                    }
                }
                break;
            }
        }
        if(have_put == 0)
            break;
        sort(cp, cp + cp_num, cmp2);
        for(int zz = 0; zz < cp_num; zz ++){
            if(cp[zz].flag)
                continue;
            cl_max = max(cl_max, cp[zz].l);
            cw_max = max(cw_max, cp[zz].w);
        }
    }
}
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
void print_ans(){
    int i, j, k;
    printf("\n");
    for(i = 1; i <= but_n; i ++){
        printf("%d   %lf   %lf\n", i, but[i].elec, but[i].val);
    }
    printf("\n");
    for(i = 0; i < cnt_pbut; i ++){
        printf("%d\n",but[0].ori_ind);
        printf("%d   %lf   %lf   (%lf, %lf)\n ", pbut[i].index, pbut[i].len, pbut[i].wid, pbut[i].x, pbut[i].y);
    }
}
 
int main(){
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    init_but();
    init_wall();
    init_light();
    //printf("daynum=%d\n",day_num);
    cal_electricity();
    RP();
    print_ans();
    printf("%lf %lf %d\n",min_len, min_wid, cp_num);
    printf("%d\n%d\n", but_n, cnt_pbut);
    for(int i = 0; i < cp_num; i ++){
        printf("x=%lf, y=%lf\n", cp[i].x, cp[i].y);
    }
    return 0;
}