|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.LinkedList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.PriorityQueue; |
| 11 | +import java.util.Queue; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.TreeMap; |
| 14 | + |
| 15 | +public class wk323 { |
| 16 | + //ranking: 1203 / 4671 菜死 |
| 17 | + |
| 18 | + |
| 19 | + //排序 |
| 20 | + public int deleteGreatestValue(int[][] grid) { |
| 21 | + int ans = 0; |
| 22 | + for (int i = 0; i < grid.length; i++) { |
| 23 | + Arrays.sort(grid[i]); |
| 24 | + } |
| 25 | + for (int i = 0; i < grid[0].length; i++) { |
| 26 | + int max = Integer.MIN_VALUE; |
| 27 | + for (int j = 0; j < grid.length; j++) { |
| 28 | + max = Math.max(max, grid[j][i]); |
| 29 | + } |
| 30 | + ans += max; |
| 31 | + } |
| 32 | + return ans; |
| 33 | + } |
| 34 | + |
| 35 | + //哈希 |
| 36 | + public int longestSquareStreak(int[] nums) { |
| 37 | + Set<Integer> set = new HashSet<>(); |
| 38 | + for (int num : nums) { |
| 39 | + set.add(num); |
| 40 | + } |
| 41 | + int ans = -1; |
| 42 | + boolean[] visited = new boolean[nums.length]; |
| 43 | + for (int i = 0; i < nums.length; i++) { |
| 44 | + int num = nums[i] * nums[i]; |
| 45 | + int count = 1; |
| 46 | + while (set.contains(num)) { |
| 47 | + count++; |
| 48 | + num = num * num; |
| 49 | + } |
| 50 | + if (count >= 2) { |
| 51 | + ans = Math.max(count, ans); |
| 52 | + } |
| 53 | + } |
| 54 | + return ans; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + //模拟分配 |
| 59 | + class Allocator { |
| 60 | + int[] memo; |
| 61 | + |
| 62 | + public Allocator(int n) { |
| 63 | + memo = new int[n]; |
| 64 | + } |
| 65 | + |
| 66 | + public int allocate(int size, int mID) { |
| 67 | + int count = 0; |
| 68 | + for (int i = 0; i < memo.length; i++) { |
| 69 | + if (memo[i] != 0) { |
| 70 | + count = 0; |
| 71 | + } else { |
| 72 | + count++; |
| 73 | + if (count == size) { |
| 74 | + for (int j = i; j > i - size; j--) { |
| 75 | + memo[j] = mID; |
| 76 | + } |
| 77 | + return i - size + 1; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return -1; |
| 82 | + } |
| 83 | + |
| 84 | + public int free(int mID) { |
| 85 | + int count = 0; |
| 86 | + for (int i = 0; i < memo.length; i++) { |
| 87 | + if (memo[i] == mID) { |
| 88 | + memo[i] = 0; |
| 89 | + count++; |
| 90 | + } |
| 91 | + } |
| 92 | + return count; |
| 93 | + } |
| 94 | + /* public int allocate(int size, int mID) { |
| 95 | + int index = -1; |
| 96 | + for (Map.Entry<Integer, Integer> entry : treeMap.entrySet()) { |
| 97 | + //全分配 |
| 98 | + if (entry.getValue() == size) { |
| 99 | + treeMap.remove(entry.getKey()); |
| 100 | + index = entry.getKey(); |
| 101 | + //半分配 |
| 102 | + } else if (entry.getValue() > size) { |
| 103 | + treeMap.remove(entry.getKey()); |
| 104 | + treeMap.put(entry.getKey() + size, entry.getValue() - size); |
| 105 | + index = entry.getKey(); |
| 106 | + } |
| 107 | + } |
| 108 | + if (index > -1) { |
| 109 | + if (!map.containsKey(mID)) map.put(mID, new ArrayList<>()); |
| 110 | + map.get(mID).add(index); |
| 111 | + } |
| 112 | + return index; |
| 113 | + }*/ |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + //优先队列可降低复杂度 |
| 118 | + //离线查询,排序查询,从小的开始处理 |
| 119 | + /* public int[] maxPoints(int[][] grid, int[] queries) { |
| 120 | + int[][] qq = new int[queries.length][2]; |
| 121 | + for (int i = 0; i < queries.length; i++) { |
| 122 | + qq[i] = new int[]{queries[i], i}; |
| 123 | + } |
| 124 | + Arrays.sort(qq, (a, b) -> a[0] - b[0]); |
| 125 | + int m = grid.length, n = grid[0].length; |
| 126 | +
|
| 127 | + int[][] move = new int[][]{ |
| 128 | + {0, 1}, {0, -1}, {1, 0}, {-1, 0} |
| 129 | + }; |
| 130 | + int[] ans = new int[queries.length]; |
| 131 | + Set<Integer> set = new HashSet<>(); |
| 132 | + PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]); |
| 133 | + priorityQueue.add(new int[]{grid[0][0], 0}); |
| 134 | + for (int i = 0; i < qq.length; i++) { |
| 135 | + int[] a = qq[i]; |
| 136 | +
|
| 137 | + while (!priorityQueue.isEmpty() && priorityQueue.peek()[0] < a[0]) { |
| 138 | + int[] poll = priorityQueue.poll(); |
| 139 | + int index = poll[1]; |
| 140 | + int x = index / n; |
| 141 | + int y = index % n; |
| 142 | + set.add(index); |
| 143 | + for (int[] next : move) { |
| 144 | + int nx = next[0] + x; |
| 145 | + int ny = next[1] + y; |
| 146 | + int s = nx * n + ny % n; |
| 147 | + if (nx >= 0 && nx < m && ny >= 0 && ny < n && !set.contains(s)) { |
| 148 | + priorityQueue.add(new int[]{grid[nx][ny], s});//这次可以检查 |
| 149 | + if (grid[nx][ny] < a[0]) { |
| 150 | + set.add(s); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + ans[a[1]] = set.size(); |
| 156 | + } |
| 157 | + return ans; |
| 158 | + }*/ |
| 159 | + |
| 160 | + |
| 161 | + //转化成Dijkstra找最短路径问题 |
| 162 | + public int[] maxPoints(int[][] grid, int[] queries) { |
| 163 | + int m = grid.length, n = grid[0].length; |
| 164 | + int[][] d = new int[m][n]; |
| 165 | + PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]); |
| 166 | + priorityQueue.add(new int[]{grid[0][0], 0, 0}); |
| 167 | + d[0][0]=grid[0][0]; |
| 168 | + int[][] move = new int[][]{ |
| 169 | + {0, 1}, {0, -1}, {1, 0}, {-1, 0} |
| 170 | + }; |
| 171 | + while (!priorityQueue.isEmpty()) { |
| 172 | + int[] poll = priorityQueue.poll(); |
| 173 | + int val = poll[0]; |
| 174 | + int x = poll[1]; |
| 175 | + int y = poll[2]; |
| 176 | + for (int[] ints : move) { |
| 177 | + int nx = ints[0] + x; |
| 178 | + int ny = ints[1] + y; |
| 179 | + if (nx >= 0 && nx < m && ny >= 0 && ny < n && d[nx][ny] == 0) { |
| 180 | + d[nx][ny] = Math.max(val, grid[nx][ny]); |
| 181 | + priorityQueue.add(new int[]{d[nx][ny],nx,ny}); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + int []sum=new int[(int)1e6]; |
| 186 | + for (int i = 0; i < d.length; i++) { |
| 187 | + for (int j = 0; j < d[0].length; j++) { |
| 188 | + System.out.print(d[i][j]+" "); |
| 189 | + sum[d[i][j]]++; |
| 190 | + } |
| 191 | + System.out.println(); |
| 192 | + } |
| 193 | + for (int i = 1; i < sum.length; i++) { |
| 194 | + sum[i]+=sum[i-1]; |
| 195 | + } |
| 196 | + for (int i = 0; i < queries.length; i++) { |
| 197 | + queries[i]=sum[queries[i]-1]; |
| 198 | + } |
| 199 | + return queries; |
| 200 | + } |
| 201 | +} |
0 commit comments