|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.Comparator; |
| 4 | +import java.util.PriorityQueue; |
| 5 | + |
| 6 | +public class wk327 { |
| 7 | + //ranking: |
| 8 | + |
| 9 | + //过 |
| 10 | + public int maximumCount(int[] nums) { |
| 11 | + int neg = 0, pos = 0; |
| 12 | + for (int num : nums) { |
| 13 | + if (num > 0) { |
| 14 | + pos++; |
| 15 | + } else if (num < 0) { |
| 16 | + neg++; |
| 17 | + } |
| 18 | + } |
| 19 | + return Math.max(neg, pos); |
| 20 | + } |
| 21 | + |
| 22 | + //模拟 |
| 23 | + public long maxKelements(int[] nums, int k) { |
| 24 | + PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Comparator.reverseOrder()); |
| 25 | + for (int num : nums) { |
| 26 | + priorityQueue.add(num); |
| 27 | + } |
| 28 | + long res = 0; |
| 29 | + while (k-- > 0) { |
| 30 | + Integer poll = priorityQueue.poll(); |
| 31 | + if (poll == 0) { |
| 32 | + break; |
| 33 | + } |
| 34 | + res += poll; |
| 35 | + priorityQueue.add((poll + 2) / 3); |
| 36 | + } |
| 37 | + return res; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + //统计次数 枚举讨论 |
| 42 | + static public boolean isItPossible(String word1, String word2) { |
| 43 | + int[] c1 = new int[26]; |
| 44 | + int[] c2 = new int[26]; |
| 45 | + int diff1 = 0, diff2 = 0; |
| 46 | + for (char c : word1.toCharArray()) { |
| 47 | + c1[c - 'a']++; |
| 48 | + if (c1[c - 'a'] == 1) { |
| 49 | + diff1++; |
| 50 | + } |
| 51 | + } |
| 52 | + for (char c : word2.toCharArray()) { |
| 53 | + c2[c - 'a']++; |
| 54 | + if (c2[c - 'a'] == 1) { |
| 55 | + diff2++; |
| 56 | + } |
| 57 | + } |
| 58 | + //测试交换 |
| 59 | + for (int i = 0; i < c1.length; i++) { |
| 60 | + for (int j = 0; j < c2.length; j++) { |
| 61 | + //有一个不存在此字母 |
| 62 | + if (c1[i] == 0 || c2[j] == 0) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + //相同的字母 |
| 66 | + if (i == j) { |
| 67 | + if (diff1 == diff2) { |
| 68 | + return true; |
| 69 | + } else { |
| 70 | + continue; |
| 71 | + } |
| 72 | + } |
| 73 | + //不同的字母 |
| 74 | + int d1 = diff1, d2 = diff2; |
| 75 | + //只有一个此字母 |
| 76 | + if (c1[i] == 1) { |
| 77 | + d1--; |
| 78 | + } |
| 79 | + //没有此字母 |
| 80 | + if (c1[j] == 0) { |
| 81 | + d1++; |
| 82 | + } |
| 83 | + //只有一个此字母 |
| 84 | + if (c2[j] == 1) { |
| 85 | + d2--; |
| 86 | + } |
| 87 | + //没有此字母 |
| 88 | + if (c2[i] == 0) { |
| 89 | + d2++; |
| 90 | + } |
| 91 | + if (d1 == d2) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + //记住一次一次模拟 不要用while |
| 102 | + static public int findCrossingTime(int n, int k, int[][] time) { |
| 103 | + PriorityQueue<int[]> left = new PriorityQueue<>((a, b) -> a[0] + a[2] == b[0] + b[2] ? b[4] - a[4] : b[0] + b[2] - (a[0] + a[2])); |
| 104 | + PriorityQueue<int[]> right = new PriorityQueue<>((a, b) -> a[0] + a[2] == b[0] + b[2] ? b[4] - a[4] : b[0] + b[2] - (a[0] + a[2])); |
| 105 | + PriorityQueue<int[]> leftWarehouse = new PriorityQueue<>((a, b) -> a[5] - b[5]); |
| 106 | + PriorityQueue<int[]> rightWarehouse = new PriorityQueue<>((a, b) -> a[5] - b[5]); |
| 107 | + for (int i = 0; i < time.length; i++) { |
| 108 | + int[] t = time[i]; |
| 109 | + left.add(new int[]{t[0], t[1], t[2], t[3], i, 0}); |
| 110 | + } |
| 111 | + |
| 112 | + int cur = 0; |
| 113 | + while (n > 0) { |
| 114 | + |
| 115 | + while (!rightWarehouse.isEmpty() && rightWarehouse.peek()[5] <= cur) { |
| 116 | + int[] poll = rightWarehouse.poll(); |
| 117 | + right.add(poll); |
| 118 | + } |
| 119 | + while (!leftWarehouse.isEmpty() && leftWarehouse.peek()[5] <= cur) { |
| 120 | + int[] poll = leftWarehouse.poll(); |
| 121 | + left.add(poll); |
| 122 | + } |
| 123 | + // 右边的先过河 |
| 124 | + if (!right.isEmpty()) { |
| 125 | + int[] poll = right.poll(); |
| 126 | + cur += poll[2]; |
| 127 | + poll[5] = cur + poll[3]; |
| 128 | + leftWarehouse.add(poll); |
| 129 | + //右边空左边可以过 |
| 130 | + } else if (!left.isEmpty()) { |
| 131 | + int[] poll = left.poll(); |
| 132 | + cur += poll[0]; |
| 133 | + poll[5] = cur + poll[1]; |
| 134 | + rightWarehouse.add(poll); |
| 135 | + n--; |
| 136 | + // 右边房子有人 |
| 137 | + } else if (leftWarehouse.isEmpty() && !rightWarehouse.isEmpty()) { |
| 138 | + cur = rightWarehouse.peek()[5]; |
| 139 | + //左边房子有人 |
| 140 | + } else if (!leftWarehouse.isEmpty() && rightWarehouse.isEmpty()) { |
| 141 | + cur = leftWarehouse.peek()[5]; |
| 142 | + //都有人取最小 |
| 143 | + } else { |
| 144 | + cur = Math.min(leftWarehouse.peek()[5], rightWarehouse.peek()[5]); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + //将右边的全部过河 |
| 149 | + while (!rightWarehouse.isEmpty()) { |
| 150 | + int[] poll = rightWarehouse.poll(); |
| 151 | + cur = Math.max(poll[5],cur)+poll[2]; |
| 152 | + } |
| 153 | + |
| 154 | + return cur; |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + public static void main(String[] args) { |
| 160 | + findCrossingTime(3, 2, new int[][]{ |
| 161 | + {1, 9, 1, 8}, {10, 10, 10, 10} |
| 162 | + }); |
| 163 | + } |
| 164 | +} |
0 commit comments