Repository navigation
Expand file tree
/
Copy pathBestTimeToBuyAndSellStockFinal.java
More file actions
188 lines (160 loc) · 6.56 KB
/
Copy pathBestTimeToBuyAndSellStockFinal.java
File metadata and controls
188 lines (160 loc) · 6.56 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.util.*;
import java.io.*;
public class BestTimeToBuyAndSellStockFinal {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
String[] input = br.readLine().split(" ");
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = Long.parseLong(input[i]);
}
System.out.println(solve(a, n));
}
}
public static long solve(long[] a, int n) {
// Game theory problem: Hao minimizes beauty, Alex maximizes beauty
// Both play optimally, exactly n/2 elements remain locked
if (n == 2) {
// Special case: only one element will be locked, beauty is 0 if < 2 elements
return 0;
}
if (n <= 8) {
// For small arrays, use exact minimax with pruning
return minimaxSolution(a, n);
} else {
// For larger arrays, use strategic heuristic
return heuristicSolution(a, n);
}
}
public static long minimaxSolution(long[] a, int n) {
// Use minimax algorithm for exact solution on small inputs
boolean[] removed = new boolean[n];
boolean[] locked = new boolean[n];
return minimax(a, removed, locked, 0, true, n);
}
public static long minimax(long[] a, boolean[] removed, boolean[] locked, int turn, boolean haoTurn, int n) {
if (turn == n) {
return calculateBeauty(a, locked, n);
}
if (haoTurn) {
// Hao's turn - minimize beauty
long minResult = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (!removed[i] && !locked[i]) {
removed[i] = true;
long result = minimax(a, removed, locked, turn + 1, false, n);
minResult = Math.min(minResult, result);
removed[i] = false;
}
}
return minResult == Long.MAX_VALUE ? calculateBeauty(a, locked, n) : minResult;
} else {
// Alex's turn - maximize beauty
long maxResult = Long.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (!removed[i] && !locked[i]) {
locked[i] = true;
long result = minimax(a, removed, locked, turn + 1, true, n);
maxResult = Math.max(maxResult, result);
locked[i] = false;
}
}
return maxResult == Long.MIN_VALUE ? calculateBeauty(a, locked, n) : maxResult;
}
}
public static long heuristicSolution(long[] a, int n) {
// Strategic approach for larger arrays
boolean[] removed = new boolean[n];
boolean[] locked = new boolean[n];
// Simulate optimal gameplay
for (int turn = 0; turn < n; turn++) {
if (turn % 2 == 0) {
// Hao's turn: remove element that hurts Alex's potential the most
haoOptimalMove(a, removed, locked, n);
} else {
// Alex's turn: lock element that gives best beauty potential
alexOptimalMove(a, removed, locked, n);
}
}
return calculateBeauty(a, locked, n);
}
public static void haoOptimalMove(long[] a, boolean[] removed, boolean[] locked, int n) {
int bestMove = -1;
long maxDamage = Long.MIN_VALUE;
// Find element whose removal maximally reduces potential beauty
for (int i = 0; i < n; i++) {
if (!removed[i] && !locked[i]) {
long damage = 0;
// Calculate potential beauty reduction from removing element i
for (int j = 0; j < n; j++) {
if (j != i && !removed[j] && !locked[j]) {
if (i < j) {
damage += Math.max(0, a[j] - a[i]);
} else {
damage += Math.max(0, a[i] - a[j]);
}
}
}
if (damage > maxDamage) {
maxDamage = damage;
bestMove = i;
}
}
}
if (bestMove != -1) {
removed[bestMove] = true;
}
}
public static void alexOptimalMove(long[] a, boolean[] removed, boolean[] locked, int n) {
int bestMove = -1;
long maxContribution = Long.MIN_VALUE;
// Find element that contributes most to potential beauty when locked
for (int i = 0; i < n; i++) {
if (!removed[i] && !locked[i]) {
long contribution = 0;
// Calculate maximum beauty this element can contribute to
for (int j = 0; j < n; j++) {
if (j != i && !removed[j]) {
if (i < j) {
contribution = Math.max(contribution, a[j] - a[i]);
} else {
contribution = Math.max(contribution, a[i] - a[j]);
}
}
}
if (contribution > maxContribution) {
maxContribution = contribution;
bestMove = i;
}
}
}
if (bestMove != -1) {
locked[bestMove] = true;
}
}
public static long calculateBeauty(long[] a, boolean[] locked, int n) {
// Find all locked elements and calculate maximum beauty
List<Integer> lockedIndices = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (locked[i]) {
lockedIndices.add(i);
}
}
if (lockedIndices.size() < 2) {
return 0;
}
long maxBeauty = Long.MIN_VALUE;
for (int i = 0; i < lockedIndices.size(); i++) {
for (int j = i + 1; j < lockedIndices.size(); j++) {
int idx1 = lockedIndices.get(i);
int idx2 = lockedIndices.get(j);
// Beauty is max difference where j > i
maxBeauty = Math.max(maxBeauty, a[idx2] - a[idx1]);
}
}
return maxBeauty;
}
}