|
| 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 | + |
| 9 | +public class wk402 { |
| 10 | + |
| 11 | + |
| 12 | + // 哈希 |
| 13 | + public long countCompleteDayPairs(int[] hours) { |
| 14 | + int[] count = new int[24]; |
| 15 | + for (int hour : hours) { |
| 16 | + count[hour % 24]++; |
| 17 | + } |
| 18 | + |
| 19 | + long ans = 0; |
| 20 | + for (int i = 0; i <= count.length / 2; i++) { |
| 21 | + int sub = 24 - i; |
| 22 | + if (sub == 24 || sub == 12) { |
| 23 | + ans += (long) count[i] * (count[i] - 1) / 2; |
| 24 | + } else { |
| 25 | + ans += (long) count[i] * (count[sub]); |
| 26 | + } |
| 27 | + } |
| 28 | + return ans; |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + // 排序+dp |
| 34 | + static public long maximumTotalDamage(int[] power) { |
| 35 | + |
| 36 | + Arrays.sort(power); |
| 37 | + Map<Integer, Long> map = new HashMap<>(); |
| 38 | + for (int i : power) { |
| 39 | + map.put(i, map.getOrDefault(i, 0L) + i); |
| 40 | + } |
| 41 | + |
| 42 | + List<Integer> list = new ArrayList<>(); |
| 43 | + list.add(-100); |
| 44 | + for (int i = 0; i < power.length; i++) { |
| 45 | + if (i == 0 || power[i] != power[i - 1]) { |
| 46 | + list.add(power[i]); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + long[][] dp = new long[list.size()][2]; |
| 51 | + for (int i = 1; i < list.size(); i++) { |
| 52 | + int left = i; |
| 53 | + while (left >= 0 && list.get(left) >= list.get(i) - 2) { |
| 54 | + left--; |
| 55 | + } |
| 56 | + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]); |
| 57 | + dp[i][1] = Math.max(dp[left][1], dp[left][0]) + map.get(list.get(i)); |
| 58 | + } |
| 59 | + return Math.max(dp[list.size() - 1][0], dp[list.size() - 1][1]); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + public static void main(String[] args) { |
| 64 | + wk402 w = new wk402(); |
| 65 | + w.countOfPeaks(new int[]{4, 9, 4, 10, 7}, new int[][]{ |
| 66 | + {2, 3, 2}, {2, 1, 3}, {1, 2, 3}} |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + //树状数组 |
| 71 | + public List<Integer> countOfPeaks(int[] nums, int[][] queries) { |
| 72 | + |
| 73 | + |
| 74 | + FenwickTree f = new FenwickTree(nums.length + 1); |
| 75 | + List<Integer> ans = new ArrayList<>(); |
| 76 | + for (int i = 0; i < nums.length; i++) { |
| 77 | + help(i, nums, f); |
| 78 | + } |
| 79 | + for (int i = 0; i < queries.length; i++) { |
| 80 | + int[] query = queries[i]; |
| 81 | + if (query[0] == 1) { |
| 82 | + if (query[2] - query[1] < 2) { |
| 83 | + ans.add(0); |
| 84 | + continue; |
| 85 | + } |
| 86 | + ans.add(f.query(query[2]) - f.query(query[1] + 1)); |
| 87 | + } else { |
| 88 | + int index = query[1]; |
| 89 | + nums[index] = query[2]; |
| 90 | + help(index, nums, f); |
| 91 | + help(index + 1, nums, f); |
| 92 | + help(index - 1, nums, f); |
| 93 | + } |
| 94 | + } |
| 95 | + return ans; |
| 96 | + } |
| 97 | + |
| 98 | + void help(int index, int[] nums, FenwickTree f) { |
| 99 | + if (index <= 0 || index >= nums.length - 1) return; |
| 100 | + // 之前是峰值元素 |
| 101 | + if (f.query(index + 1) - f.query(index) == 1) { |
| 102 | + //依旧是峰值 |
| 103 | + if (nums[index] > nums[index - 1] && nums[index] > nums[index + 1]) { |
| 104 | + |
| 105 | + } else { |
| 106 | + //不是峰值 |
| 107 | + f.update(index + 1, -1); |
| 108 | + } |
| 109 | + |
| 110 | + } else { |
| 111 | + //之前不是 |
| 112 | + // 现在是了 |
| 113 | + if (nums[index] > nums[index - 1] && nums[index] > nums[index + 1]) { |
| 114 | + f.update(index + 1, 1); |
| 115 | + } else { |
| 116 | + //现在也不是 |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + //树状数组板子 |
| 122 | + public class FenwickTree { |
| 123 | + |
| 124 | + /** |
| 125 | + * 预处理数组 |
| 126 | + */ |
| 127 | + private int[] tree; |
| 128 | + private int len; |
| 129 | + |
| 130 | + public FenwickTree(int n) { |
| 131 | + this.len = n; |
| 132 | + tree = new int[n + 1]; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * 单点更新 |
| 137 | + * |
| 138 | + * @param i 原始数组索引 i |
| 139 | + * @param delta 变化值 = 更新以后的值 - 原始值 |
| 140 | + */ |
| 141 | + public void update(int i, int delta) { |
| 142 | + // 从下到上更新,注意,预处理数组,比原始数组的 len 大 1,故 预处理索引的最大值为 len |
| 143 | + while (i <= len) { |
| 144 | + tree[i] += delta; |
| 145 | + i += lowbit(i); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + //区间更新 |
| 150 | + void update(int x, int y, int k) { |
| 151 | + update(x, k); |
| 152 | + update(y + 1, -k); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * 查询前缀和 |
| 157 | + * |
| 158 | + * @param i 前缀的最大索引,即查询区间 [0, i] 的所有元素之和 |
| 159 | + */ |
| 160 | + public int query(int i) { |
| 161 | + // 从右到左查询 |
| 162 | + int sum = 0; |
| 163 | + while (i > 0) { |
| 164 | + sum += tree[i]; |
| 165 | + i -= lowbit(i); |
| 166 | + } |
| 167 | + return sum; |
| 168 | + } |
| 169 | + |
| 170 | + public int lowbit(int x) { |
| 171 | + return x & (-x); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments