-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOferta.java
More file actions
139 lines (122 loc) · 3.92 KB
/
Oferta.java
File metadata and controls
139 lines (122 loc) · 3.92 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
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
public class Oferta {
private static class MyScanner {
private BufferedReader br;
private StringTokenizer st;
public MyScanner(Reader reader) {
br = new BufferedReader(reader);
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
public static void main(String[] args) {
try {
String inFile = "oferta.in";
String outFile = "oferta.out";
MyScanner scanner = new MyScanner(new FileReader(inFile));
int N = scanner.nextInt();
int k = scanner.nextInt();
List<Integer> oferte = new ArrayList<>();
for (int i = 0; i < N; i++) {
oferte.add(scanner.nextInt());
}
int sum = listSum(oferte);
HashMap<List<Integer>, Double> combinations = generateCombinations(oferte);
// Sort the HashMap by values in descending order
LinkedHashMap<List<Integer>, Double> sortedCombinations = sort(combinations);
double sol = findMaxSum(sortedCombinations);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outFile))) {
String formattedMaxPower = String.format("%.1f", sum - sol);
writer.write(formattedMaxPower + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static HashMap<List<Integer>, Double> generateCombinations(List<Integer> nums) {
HashMap<List<Integer>, Double> combinations = new HashMap<>();
// Generate pairs
for (int i = 0; i < nums.size() - 1; i++) {
List<Integer> pair = new ArrayList<>();
pair.add(nums.get(i));
pair.add(nums.get(i + 1));
double minValueHalf = Math.min(nums.get(i), nums.get(i + 1)) / 2.0;
combinations.put(pair, minValueHalf);
}
// Generate triplets
for (int i = 0; i < nums.size() - 2; i++) {
List<Integer> triplet = new ArrayList<>();
triplet.add(nums.get(i));
triplet.add(nums.get(i + 1));
triplet.add(nums.get(i + 2));
double minValue = Math.min(nums.get(i), Math.min(nums.get(i + 1), nums.get(i + 2)));
combinations.put(triplet, minValue);
}
return combinations;
}
public static LinkedHashMap<List<Integer>, Double> sort(HashMap<List<Integer>, Double> map) {
List<Map.Entry<List<Integer>, Double>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<List<Integer>, Double>>() {
public int compare(Map.Entry<List<Integer>,
Double> o1,Map.Entry<List<Integer>, Double> o2) {
return Double.compare(o2.getValue(), o1.getValue());
}
});
LinkedHashMap<List<Integer>, Double> sortedMap = new LinkedHashMap<>();
for (Map.Entry<List<Integer>, Double> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
public static double findMaxSum(LinkedHashMap<List<Integer>, Double> sortedCombinations) {
double sum = 0;
List<Integer> blacklist = new ArrayList<>();
for (List<Integer> combination : sortedCombinations.keySet()) {
boolean canUse = true;
for (int num : combination) {
if (blacklist.contains(num)) {
canUse = false;
break;
}
}
if (canUse) {
sum += sortedCombinations.get(combination);
for (int num : combination) {
blacklist.add(num);
}
}
}
return sum;
}
public static int listSum(List<Integer> nums) {
int sum = 0;
for (int num : nums) {
sum += num;
}
return sum;
}
}