-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwk357.java
386 lines (334 loc) · 11.6 KB
/
wk357.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package weekly;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class wk357 {
//模拟
public String finalString(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'i') {
sb = sb.reverse();
} else {
sb.append(c);
}
}
return sb.toString();
}
//可贪心
public boolean canSplitArray(List<Integer> nums, int m) {
int n = nums.size();
if (n <= 2) return true;
for (int i = 1; i < n; i++)
if (nums.get(i - 1) + nums.get(i) >= m)
return true;
return false;
}
/*public boolean canSplitArray(List<Integer> nums, int m) {
int[] sum = new int[nums.size() + 1];
for (int i = 0; i < nums.size(); i++) {
sum[i + 1] += sum[i] + nums.get(i);
}
memo = new int[nums.size()][nums.size()];
for (int[] ints : memo) {
Arrays.fill(ints, -1);
}
return dfs(0, nums.size() - 1, sum, m);
}
int[][] memo;
boolean dfs(int begin, int end, int[] presum, int m) {
if (begin == end) return true;
if (begin + 1 == end) return true;
if (memo[begin][end] != -1) {
return memo[begin][end] == 1;
}
boolean ans = false;
for (int i = begin + 1; i <= end; i++) {
boolean left = (i - begin == 1) || (presum[i] - presum[begin] >= m);
boolean right = (end - i + 1 == 1) || (presum[end + 1] - presum[i] >= m);
if (left && right) {
ans = dfs(begin, i - 1, presum, m) && dfs(i, end, presum, m);
if (ans) break;
}
}
memo[begin][end] = ans ? 1 : 0;
return ans;
}*/
/* public int maximumSafenessFactor(List<List<Integer>> grid) {
int m = grid.size(), n = grid.get(0).size();
List<int[]> g = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid.get(i).get(j) == 1) {
g.add(new int[]{i, j});
}
}
}
int[][] dis = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int ans = Integer.MAX_VALUE;
for (int[] ints : g) {
ans = Math.min(ans, Math.abs(i - ints[0]) + Math.abs(j - ints[1]));
}
dis[i][j] = ans;
}
}
int left = 0, right = Math.max(m, n);
while (left < right) {
int mid = (left + right + 1) / 2;
if (check(dis, mid)) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
int[][] moves = new int[][]{
{0, -1}, {-1, 0}, {1, 0}, {0, 1}
};
boolean check(int[][] distance, int dis) {
boolean[][] visited = new boolean[distance.length][distance[0].length];
return dfs(0, 0, dis, distance, visited);
}
boolean dfs(int x, int y, int dis, int[][] distance, boolean[][] visited) {
if (distance[x][y] < dis) {
return false;
}
if (x == distance.length - 1 && y == distance.length[] - 1) {
return true;
}
if (visited[x][y]) {
return false;
}
visited[x][y] = true;
for (int[] move : moves) {
int nx = move[0] + x;
int ny = move[1] + y;
if (nx >= 0 && ny >= 0 && nx < distance.length && ny < distance[0].length) {
if (dfs(nx, ny, dis, distance, visited)) {
return true;
}
}
}
return false;
}*/
int[][] moves = new int[][]{
{0, -1}, {-1, 0}, {1, 0}, {0, 1}
};
//并查集
/* public int maximumSafenessFactor(List<List<Integer>> grid) {
int m = grid.size(), n = grid.get(0).size();
Queue<int[]> queue = new LinkedList<>();
int[][] dis = new int[m][n];
for (int[] di : dis) {
Arrays.fill(di, -1);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid.get(i).get(j) == 1) {
queue.add(new int[]{i, j});
dis[i][j]=0;
}
}
}
TreeMap<Integer, List<int[]>> treeMap = new TreeMap<>((a, b) -> b - a);
int step = 0;
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
int[] poll = queue.poll();
if (!treeMap.containsKey(step)) {
treeMap.put(step, new ArrayList<>());
}
treeMap.get(step).add(poll);
for (int[] move : moves) {
int nx = move[0] + poll[0];
int ny = move[1] + poll[1];
if (nx >= 0 && ny >= 0 && nx < m && ny < n && dis[nx][ny] == -1) {
queue.add(new int[]{nx, ny});
dis[nx][ny]=step+1;
}
}
}
step++;
}
UnionFind uf = new UnionFind(m * n);
for (Map.Entry<Integer, List<int[]>> entry : treeMap.entrySet()) {
for (int[] ints : entry.getValue()) {
int x = ints[0], y = ints[1];
for (int[] move : moves) {
int nx = move[0] + x;
int ny = move[1] + y;
if (nx >= 0 && ny >= 0 && nx < m && ny < n && dis[x][y] <= dis[nx][ny]) {
uf.union(x * n + y, nx * n + ny);
}
}
}
if (uf.find(0) == uf.find((m - 1) * n + n - 1)) return entry.getKey();
}
return 0;
}*/
//并查集
class UnionFind {
public final int[] parents;
public int count;
public UnionFind(int n) {
this.parents = new int[n];
reset();
}
public void reset() {
for (int i = 0; i < parents.length; i++) {
parents[i] = i;
}
count = parents.length - 1;
}
public int find(int i) {
int parent = parents[i];
if (parent == i) {
return i;
} else {
int root = find(parent);
parents[i] = root;
return root;
}
}
public boolean union(int i, int j) {
int r1 = find(i);
int r2 = find(j);
if (r1 != r2) {
count--;
parents[r1] = r2;
return true;
} else {
return false;
}
}
/* void isolate(int x) {
if (x != parents[x]) {
parents[x] = x;
count++;
}
}*/
}
//迪杰斯塔拉
public int maximumSafenessFactor(List<List<Integer>> grid) {
int m = grid.size(), n = grid.get(0).size();
Queue<int[]> queue = new LinkedList<>();
int[][] dis = new int[m][n];
for (int[] di : dis) {
Arrays.fill(di, -1);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid.get(i).get(j) == 1) {
queue.add(new int[]{i, j});
dis[i][j] = 0;
}
}
}
TreeMap<Integer, List<int[]>> treeMap = new TreeMap<>((a, b) -> b - a);
int step = 0;
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
int[] poll = queue.poll();
if (!treeMap.containsKey(step)) {
treeMap.put(step, new ArrayList<>());
}
treeMap.get(step).add(poll);
for (int[] move : moves) {
int nx = move[0] + poll[0];
int ny = move[1] + poll[1];
if (nx >= 0 && ny >= 0 && nx < m && ny < n && dis[nx][ny] == -1) {
queue.add(new int[]{nx, ny});
dis[nx][ny] = step + 1;
}
}
}
step++;
}
PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> b[0] - a[0]);
priorityQueue.add(new int[]{dis[0][0], 0, 0});
boolean[][] vis = new boolean[m][n];
vis[0][0] = true;
while (!priorityQueue.isEmpty()) {
int[] poll = priorityQueue.poll();
if (poll[1] == m - 1 && poll[2] == n - 1) {
return poll[0];
}
for (int[] move : moves) {
int nx = move[0] + poll[1];
int ny = move[1] + poll[2];
if (nx >= 0 && ny >= 0 && nx < m && ny < n && !vis[nx][ny]) {
priorityQueue.add(new int[]{Math.min(dis[nx][ny],poll[0]), nx, ny});
vis[nx][ny] = true;
}
}
}
return 0;
}
// int dfs(int x, int y, int[][] distance, int[][] dp, boolean[][] visited) {
// if (x == distance.length - 1 && y == distance[0].length - 1) {
// return distance[x][y];
// }
// if (dp[x][y] != -1) {
// return dp[x][y];
// }
// if (visited[x][y]) {
// return 0;
// }
// visited[x][y] = true;
// int max = 0;
// for (int[] move : moves) {
// int nx = move[0] + x;
// int ny = move[1] + y;
// if (nx >= 0 && ny >= 0 && nx < distance.length && ny < distance[0].length) {
// max = Math.max(max, dfs(nx, ny, distance, dp, visited));
// }
// }
// max = Math.min(distance[x][y], max);
// dp[x][y] = max;
// visited[x][y] = false;
// return max;
// }
//贪心
public long findMaximumElegance(int[][] items, int k) {
// 把利润从大到小排序
Arrays.sort(items, (a, b) -> b[0] - a[0]);
long ans = 0, totalProfit = 0;
Set<Integer> vis = new HashSet<>();
ArrayDeque<Integer> duplicate = new ArrayDeque<Integer>(); // 重复类别的利润
for (int i = 0; i < items.length; i++) {
int profit = items[i][0], category = items[i][1];
if (i < k) {
totalProfit += profit;
if (!vis.add(category)) // 重复类别
duplicate.push(profit);
} else if (!duplicate.isEmpty() && vis.add(category)) {
totalProfit += profit - duplicate.pop(); // 选一个重复类别中的最小利润替换
} // else:比前面的利润小,而且类别还重复了,选它只会让 totalProfit 变小,vis.size() 不变,优雅度不会变大
ans = Math.max(ans, totalProfit + (long) vis.size() * vis.size()); // 注意 1e5*1e5 会溢出
}
return ans;
}
public static void main(String[] args) {
wk357 w = new wk357();
List<List<Integer>> list = new ArrayList<>();
list.add(Arrays.asList(0, 1, 1));
list.add(Arrays.asList(0, 0, 1));
list.add(Arrays.asList(1, 0, 0));
w.maximumSafenessFactor(list);
}
}