|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.TreeMap; |
| 9 | + |
| 10 | +public class wk370 { |
| 11 | + |
| 12 | + //求入度为0的点 |
| 13 | + /* public int findChampion(int[][] grid) { |
| 14 | + int []in=new int[grid.length]; |
| 15 | + for (int i = 0; i < grid.length; i++) { |
| 16 | + for (int j = 0; j < grid[0].length; j++) { |
| 17 | + in[j]+=grid[i][j]; |
| 18 | + } |
| 19 | + } |
| 20 | + for (int i = 0; i < in.length; i++) { |
| 21 | + if(in[i]==0) return i; |
| 22 | + } |
| 23 | + return -1; |
| 24 | + }*/ |
| 25 | + |
| 26 | + //求入度为0的点 |
| 27 | + public int findChampion(int n, int[][] edges) { |
| 28 | + int[] in = new int[n]; |
| 29 | + for (int i = 0; i < edges.length; i++) { |
| 30 | + in[edges[i][1]]++; |
| 31 | + } |
| 32 | + int ans = 0; |
| 33 | + for (int i = 0; i < in.length; i++) { |
| 34 | + if (in[i] == 0) { |
| 35 | + ans++; |
| 36 | + } |
| 37 | + } |
| 38 | + if (ans == 0 || ans > 1) return -1; |
| 39 | + for (int i = 0; i < in.length; i++) { |
| 40 | + if (in[i] == 0) { |
| 41 | + return i; |
| 42 | + } |
| 43 | + } |
| 44 | + return -1; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + //dfs 逆向思维 要么选根节点 要么继续往下走 |
| 49 | + public long maximumScoreAfterOperations(int[][] edges, int[] values) { |
| 50 | + Map<Integer, List<Integer>> map = new HashMap<>(); |
| 51 | + for (int[] edge : edges) { |
| 52 | + if (!map.containsKey(edge[0])) map.put(edge[0], new ArrayList<>()); |
| 53 | + if (!map.containsKey(edge[1])) map.put(edge[1], new ArrayList<>()); |
| 54 | + map.get(edge[0]).add(edge[1]); |
| 55 | + map.get(edge[1]).add(edge[0]); |
| 56 | + } |
| 57 | + long sum = 0; |
| 58 | + for (int value : values) { |
| 59 | + sum += value; |
| 60 | + } |
| 61 | + return sum - dfs(0, -1, map, values); |
| 62 | + } |
| 63 | + |
| 64 | + long dfs(int cur, int parent, Map<Integer, List<Integer>> map, int[] values) { |
| 65 | + |
| 66 | + long val = values[cur]; |
| 67 | + long childMinVal = 0; |
| 68 | + |
| 69 | + for (Integer child : map.get(cur)) { |
| 70 | + if (child == parent) continue; |
| 71 | + childMinVal += dfs(child, cur, map, values); |
| 72 | + } |
| 73 | + //没有孩子 |
| 74 | + if (childMinVal == 0) { |
| 75 | + childMinVal = Integer.MAX_VALUE; |
| 76 | + } |
| 77 | + |
| 78 | + return Math.min(val, childMinVal); |
| 79 | + } |
| 80 | + |
| 81 | + // 化简+线段树or树状数组 |
| 82 | + public long maxBalancedSubsequenceSum(int[] nums) { |
| 83 | + int n = nums.length; |
| 84 | + int[] b = new int[n]; |
| 85 | + for (int i = 0; i < n; i++) { |
| 86 | + b[i] = nums[i] - i; |
| 87 | + } |
| 88 | + Arrays.sort(b); |
| 89 | + |
| 90 | + BIT t = new BIT(b.length + 1); |
| 91 | + for (int i = 0; i < n; i++) { |
| 92 | + // j 为 nums[i]-i 离散化后的值(从 1 开始) |
| 93 | + int j = Arrays.binarySearch(b, nums[i] - i) + 1; |
| 94 | + long f = Math.max(t.preMax(j), 0) + nums[i]; |
| 95 | + t.update(j, f); |
| 96 | + } |
| 97 | + return t.preMax(b.length); |
| 98 | + } |
| 99 | + |
| 100 | + // 树状数组模板(维护前缀最大值) |
| 101 | + class BIT { |
| 102 | + private long[] tree; |
| 103 | + |
| 104 | + public BIT(int n) { |
| 105 | + tree = new long[n]; |
| 106 | + Arrays.fill(tree, Long.MIN_VALUE); |
| 107 | + } |
| 108 | + |
| 109 | + public void update(int i, long val) { |
| 110 | + while (i < tree.length) { |
| 111 | + tree[i] = Math.max(tree[i], val); |
| 112 | + i += i & -i; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public long preMax(int i) { |
| 117 | + long res = Long.MIN_VALUE; |
| 118 | + while (i > 0) { |
| 119 | + res = Math.max(res, tree[i]); |
| 120 | + i &= i - 1; |
| 121 | + } |
| 122 | + return res; |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | +} |
0 commit comments