-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.3.cpp
More file actions
72 lines (68 loc) · 1.98 KB
/
3.3.cpp
File metadata and controls
72 lines (68 loc) · 1.98 KB
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
/*A conveyor belt has packages that must be shipped from one port to another within days days.
The ith package on the conveyor belt has a weight of weights[i].
Each day, we load the ship with packages on the conveyor belt (in the order given by weights).
We may not load more weight than the maximum weight capacity of the ship.
Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.*/
#include<bits/stdc++.h>
#include<climits>
using namespace std;
class A{
public:
int ship(int weights[], int n, int D) {
if(n <= 0)
return 0;
int unit = 0;
if(n % D == 0){
unit = n/D;
}else{
unit = n/D+1;
}
int max = 0;
int first;
for(int i = 0; i < n && i+unit-1<n;i++){
first = 0;
for(int j = 0; j < unit;j++){
first += weights[i+j];
}
if(first > max)max = first;
}
int maxValue = 0;
for(int i = 0; i<n;i++){
if(weights[i] > maxValue){
maxValue = weights[i];
}
}
int temp = max -1;
int days;
bool flag = true;
int sum;
while(flag && temp >= maxValue){
days = 0;
sum = 0;
for(int i = 0; i < n; i++){
sum += weights[i];
if(sum > temp){
days++;
sum = weights[i];
}else if(sum == temp){
days++;
sum = 0;
}
}
if(sum > 0)days++;
if(days > D) flag = false;
else{
temp--;
}
}
return temp + 1;
}
};
int main()
{
A a;
int n, days = 5, arr[] = { 1,2,3,4,5,6,7,8,9,10 };
n = sizeof(arr)/sizeof(arr[0]);
cout<<a.ship(arr,n,days);
return 0;
}