forked from loveneeshdhir/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
containerShip.cpp
76 lines (53 loc) · 1.38 KB
/
containerShip.cpp
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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main()
{
const int CAPACITY = 500;
vector<int> weights;
int weights_add = 0;
int num_containers;
for (int i = 0; i < 1000; i++)
{
weights.push_back( rand() % 100 + 1);
}
sort(weights.begin(), weights.end());
int i = 0;
bool continuo = true;
while (continuo)
{
if (weights_add + weights[i] > CAPACITY)
continuo = false;
if (weights_add + weights[i] <= CAPACITY)
{
weights_add += weights[i];
num_containers++;
i++;
}
}
cout << "\n\tMAXIMAZING CONTAINERS NUMBER WITH GREEDY" << endl;
cout << "Number of inserted containers = " << num_containers << endl;
cout << "Weight of our ship = " << weights_add << endl;
/****************************************************************************/
i = 999;
continuo = true;
num_containers = 0;
weights_add = 0;
while (continuo)
{
if (weights_add + weights[i] > CAPACITY)
continuo = false;
if (weights_add + weights[i] <= CAPACITY)
{
weights_add += weights[i];
num_containers++;
i--;
}
}
cout << endl << "MAXIMIZING THE WEIGHT WITH GREEDY" << endl;
cout << "Number of inserted containers =" << num_containers << endl;
cout << "Weight of our ship =" << weights_add << endl;
return 0;
}