50089. Buckets and Balls, Again

難度:3/5

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
#include <stdio.h>
#include <limits.h>
#include "placement.h"

void first_fit(int bucket[1024], int n, int ball[16384], int m, int result[16384]){
for(int s = 0; s < m; s++){ int now_ball = ball[s];
int putted = 0;
for(int i = 0; i < n; i++){
if(bucket[i] >= now_ball){
result[s] = i;
bucket[i] -= now_ball;
putted = 1;
break;
}
}
if(!putted) result[s] = -1;
}
}

void best_fit(int bucket[1024], int n, int ball[16384], int m, int result[16384]){
for(int s = 0; s < m; s++){ int now_ball = ball[s];
int min_idx = -1, min_cap = INT_MAX;
for(int i = 0; i < n; i++){
if(bucket[i] - now_ball >= 0 && bucket[i] - now_ball < min_cap){
min_idx = i;
min_cap = bucket[i] - now_ball;
}
}
result[s] = min_idx;
if(min_idx != -1) bucket[min_idx] -= now_ball;
}
}

void worst_fit(int bucket[1024], int n, int ball[16384], int m, int result[16384]){
for(int s = 0; s < m; s++){ int now_ball = ball[s];
int max_idx = -1, max_cap = INT_MIN;
for(int i = 0; i < n; i++){
if(bucket[i] - now_ball >= 0 && bucket[i] - now_ball >= max_cap){
max_idx = i;
max_cap = bucket[i] - now_ball;
}
}
result[s] = max_idx;
if(max_idx != -1) bucket[max_idx] -= now_ball;
}
}

void place(int bucket[1024], int n, int ball[16384], int m, int method, int result[16384]){
if(method == 0) first_fit(bucket, n, ball, m, result);
else if(method == 1) best_fit(bucket, n, ball, m, result);
else if(method == 2) worst_fit(bucket, n, ball, m, result);
}


50089. Buckets and Balls, Again
https://aaronlin1229.github.io/judgegirl_50089/
Author
Akizumi
Posted on
July 17, 2023
Licensed under