-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreedyFractionalKnapsack.java
63 lines (51 loc) · 1.81 KB
/
GreedyFractionalKnapsack.java
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
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;
public class GreedyFractionalKnapsack {
class Item implements Comparable<Item> {
int value, weight;
Item(int value, int weight) {
this.value = value;
this.weight = weight;
}
// comparison function to sort input arrays according to value/weight
// ratio
@Override
public int compareTo(Item o) {
double r1 = (double) this.value / this.weight;
double r2 = (double) o.value / o.weight;
return Double.compare(r2, r1);
}
}
private static double getOptimalValue(int capacity, Item[] items) {
Arrays.sort(items);
double value = 0;
int index = 0;
while (capacity > 0 && index < items.length) {
if (items[index].weight <= capacity) {
capacity -= items[index].weight;
value += items[index].value;
index++;
} else {
value += capacity * ((double) items[index].value / items[index].weight);
capacity = 0;
}
}
return value;
}
public static void main(String args[]) {
GreedyFractionalKnapsack sack = new GreedyFractionalKnapsack();
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int capacity = scanner.nextInt();
Item[] items = new Item[n];
for (int i = 0; i < n; i++) {
items[i] = sack.new Item(scanner.nextInt(), scanner.nextInt());
}
scanner.close();
DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.CEILING);
System.out.println(df.format(getOptimalValue(capacity, items)));
}
}