|
| 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 wk399 { |
| 10 | +// public int numberOfPairs(int[] nums1, int[] nums2, int k) { |
| 11 | +// |
| 12 | +// } |
| 13 | + |
| 14 | + |
| 15 | + // 遍历 |
| 16 | + public String compressedString(String word) { |
| 17 | + char pre = word.charAt(0); |
| 18 | + int count = 1; |
| 19 | + StringBuilder sb = new StringBuilder(); |
| 20 | + for (int i = 1; i < word.length(); i++) { |
| 21 | + char c = word.charAt(i); |
| 22 | + if (c != pre || count == 9) { |
| 23 | + sb.append(count); |
| 24 | + sb.append(pre); |
| 25 | + pre = c; |
| 26 | + count = 1; |
| 27 | + } else { |
| 28 | + count++; |
| 29 | + } |
| 30 | + } |
| 31 | + sb.append(count); |
| 32 | + sb.append(pre); |
| 33 | + return sb.toString(); |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + // |
| 38 | + public long numberOfPairs(int[] nums1, int[] nums2, int k) { |
| 39 | + int max = 0; |
| 40 | + Map<Integer, Integer> map = new HashMap<>(); |
| 41 | + for (int i : nums1) { |
| 42 | + max = Math.max(i, max); |
| 43 | + map.put(i, map.getOrDefault(i, 0) + 1); |
| 44 | + } |
| 45 | + Map<Integer, Integer> map1 = new HashMap<>(); |
| 46 | + for (int i : nums2) { |
| 47 | + map1.put(i * k, map1.getOrDefault(i * k, 0) + 1); |
| 48 | + } |
| 49 | + |
| 50 | + long ans = 0; |
| 51 | + for (Map.Entry<Integer, Integer> entry : map1.entrySet()) { |
| 52 | + int num = entry.getKey(); |
| 53 | + int count = entry.getValue(); |
| 54 | + for (int i = 1; i * num <= max; i++) { |
| 55 | + if (map.containsKey(i * num)) { |
| 56 | + ans += ((long) map.get(i * num)) * count; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + return ans; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + static final int MOD = 1000000007; |
| 65 | + |
| 66 | + //线段树 |
| 67 | + public int maximumSumSubsequence(int[] nums, int[][] queries) { |
| 68 | + int n = nums.length; |
| 69 | + SegTree tree = new SegTree(nums); |
| 70 | + long sum = 0; |
| 71 | + for (int[] q : queries) { |
| 72 | + tree.update(q[0], q[1]); |
| 73 | + sum = (sum + tree.query()) % MOD; |
| 74 | + } |
| 75 | + return (int) sum; |
| 76 | + } |
| 77 | + |
| 78 | + public class SegTree { |
| 79 | + long[] max;// 最优子序列和 |
| 80 | + long[] pre;// 不包含右端点最优子序列和 |
| 81 | + long[] suf;// 不包含左端点最优子序列和 |
| 82 | + long[] not;// 既不包含左端点又不包含右端点最优子序列和 |
| 83 | + int N; |
| 84 | + |
| 85 | + public SegTree(int[] nums) { |
| 86 | + N = nums.length; |
| 87 | + max = new long[N << 2]; |
| 88 | + pre = new long[N << 2]; |
| 89 | + suf = new long[N << 2]; |
| 90 | + not = new long[N << 2]; |
| 91 | + build(nums, 1, N, 1); |
| 92 | + } |
| 93 | + |
| 94 | + // 初始化 |
| 95 | + private void build(int[] nums, int l, int r, int i) { |
| 96 | + if (l == r) { |
| 97 | + max[i] = Math.max(nums[l - 1], 0); |
| 98 | + return; |
| 99 | + } |
| 100 | + int m = (l + r) >> 1; |
| 101 | + build(nums, l, m, i << 1); |
| 102 | + build(nums, m + 1, r, i << 1 | 1); |
| 103 | + up(i); |
| 104 | + } |
| 105 | + |
| 106 | + // 区间合并 |
| 107 | + private void up(int i) { |
| 108 | + int l = i << 1, r = i << 1 | 1; |
| 109 | + max[i] = Math.max(pre[l] + max[r], max[l] + suf[r]); |
| 110 | + pre[i] = Math.max(max[l] + not[r], pre[l] + pre[r]); |
| 111 | + suf[i] = Math.max(not[l] + max[r], suf[l] + suf[r]); |
| 112 | + not[i] = Math.max(suf[l] + not[r], not[l] + pre[r]); |
| 113 | + } |
| 114 | + |
| 115 | + // 单点更新 |
| 116 | + public void update(int index, int value) { |
| 117 | + update(index + 1, value, 1, N, 1); |
| 118 | + } |
| 119 | + |
| 120 | + private void update(int idx, int v, int l, int r, int i) { |
| 121 | + if (l == r) { |
| 122 | + max[i] = Math.max(v, 0); |
| 123 | + return; |
| 124 | + } |
| 125 | + int m = (l + r) >> 1; |
| 126 | + if (idx <= m) { |
| 127 | + update(idx, v, l, m, i << 1); |
| 128 | + } else { |
| 129 | + update(idx, v, m + 1, r, i << 1 | 1); |
| 130 | + } |
| 131 | + up(i); |
| 132 | + } |
| 133 | + |
| 134 | + // 1 代表区间 [0, n - 1] |
| 135 | + public long query() { |
| 136 | + return max[1]; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + public static void main(String[] args) { |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments