文档服务地址:http://47.92.0.57:3000/ 周报索引地址:http://47.92.0.57:3000/s/NruNXRYmV

Commit 329c989c by majexh

添加 度小满 第二题的解法

parent f0714c0f
package second; package second;
...@@ -17,7 +17,7 @@ public class Second { ...@@ -17,7 +17,7 @@ public class Second {
public void add(List<Pair<Integer, Integer>> list, int time, int weight) { public void add(List<Pair<Integer, Integer>> list, int time, int weight) {
list.add(new Pair<>(time, weight)); list.add(new Pair<>(time, weight));
// list 已经是一个小顶堆 重新排序 // list 已经是一个小顶堆 重新排序
for (int i = list.size() - 1; i >= 0;) { for (int i = list.size() - 1; i > 0;) {
// 父节点的序号 // 父节点的序号
int father = i % 2 == 0 ? (i - 2) / 2 : i / 2; int father = i % 2 == 0 ? (i - 2) / 2 : i / 2;
// 与父节点比较 // 与父节点比较
...@@ -31,10 +31,25 @@ public class Second { ...@@ -31,10 +31,25 @@ public class Second {
} }
/** /**
* 弹出值 并且在弹出之后 重新从上向下构建 小顶堆 * 弹出最小值 并且在弹出之后 重新从上向下构建 小顶堆
*/ */
public void pop() { public Pair<Integer, Integer> pop(List<Pair<Integer, Integer>> list) {
swap(list, 0, list.size() - 1);
Pair<Integer, Integer> temp = list.remove(list.size() - 1);
int i = 0;
// 从上向下 重新构建小顶堆
while (i < list.size()) {
// 子树的左边那个
int k = 2 * i + 1;
// 左子树大于右子树
if (k < list.size() - 1 && list.get(k).getKey() > list.get(k + 1).getKey()) k++;
if (k >= list.size() || list.get(i).getKey() < list.get(k).getKey()) break;
swap(list, i, k);
i = k;
}
return temp;
} }
...@@ -43,27 +58,38 @@ public class Second { ...@@ -43,27 +58,38 @@ public class Second {
int n = scanner.nextInt(), w = scanner.nextInt(); int n = scanner.nextInt(), w = scanner.nextInt();
int[] t = new int[n], weights = new int[n]; int[] t = new int[n], weights = new int[n];
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
t[i] = scanner.nextInt(); weights[i] = scanner.nextInt();
} }
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
weights[i] = scanner.nextInt(); t[i] = scanner.nextInt();
} }
Second second = new Second(); Second second = new Second();
List<Pair<Integer, Integer>> heap = new LinkedList<>(); List<Pair<Integer, Integer>> heap = new LinkedList<>();
// 用堆排序的话就需要在出去的时候 // 用堆排序的话就需要在出去的时候
int currentTime = 0, currentWeight = w, index = 0; int currentTime = 0, currentWeight = w, index = 0;
while (true) { while (true) {
while (currentWeight >= 0) { while (index < n && currentWeight - weights[index] >= 0) {
if (index >= n) break;
// 入堆 并调整 // 入堆 并调整
second.add(heap, currentTime + t[index], weights[index]); second.add(heap, currentTime + t[index], weights[index]);
currentWeight -= weights[index]; currentWeight -= weights[index];
index++; index++;
} }
if (index == n) {
// 等于n的时候 表示已经全部入完 这个时候需要全部出完即可
while (heap.size() != 0) {
currentTime = second.pop(heap).getKey();
}
break;
} else {
// 出堆 直到下一个车能够入堆为止 // 出堆 直到下一个车能够入堆为止
// while () { while (currentWeight < weights[index]) {
// Pair<Integer, Integer> temp = second.pop(heap);
// } currentWeight += temp.getValue();
currentTime = temp.getKey();
}
}
} }
System.out.println(currentTime);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment