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

Commit f0714c0f by majexh

uncomplete second

parent 7eb7ce2c
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<component name="JavaScriptSettings"> <component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" /> <option name="languageLevel" value="ES6" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="false" project-jdk-name="11" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="false" project-jdk-name="11.0.4" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>
\ No newline at end of file
package second; package second;
package second; package second;
import javafx.util.Pair;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
public class Second { public class Second {
public void swap(List<Pair<Integer, Integer>> list, int child, int father) {
Integer timeTemp = list.get(child).getKey(), weightTemp = list.get(child).getValue();
list.set(child, new Pair<>(list.get(father).getKey(), list.get(father).getValue()));
list.set(father, new Pair<>(timeTemp, weightTemp));
}
public void add(List<Pair<Integer, Integer>> list, int time, int weight) {
list.add(new Pair<>(time, weight));
// list 已经是一个小顶堆 重新排序
for (int i = list.size() - 1; i >= 0;) {
// 父节点的序号
int father = i % 2 == 0 ? (i - 2) / 2 : i / 2;
// 与父节点比较
if (list.get(i).getKey() < list.get(father).getKey()) {
swap(list, i, father);
} else {
break;
}
i = father;
}
}
/**
* 弹出值 并且在弹出之后 重新从上向下构建 小顶堆
*/
public void pop() {
}
public static void main(String[] args) { public static void main(String[] args) {
...@@ -18,19 +48,17 @@ public class Second { ...@@ -18,19 +48,17 @@ public class Second {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
weights[i] = scanner.nextInt(); weights[i] = scanner.nextInt();
} }
Second second = new Second();
List<Pair<Integer, Integer>> heap = new LinkedList<>();
List<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 (currentWeight >= 0) {
if (index >= n) break; if (index >= n) break;
// 入堆 // 入堆 并调整
heap.add(currentTime + t[index]); second.add(heap, currentTime + t[index], weights[index]);
currentWeight -= weights[index]; currentWeight -= weights[index];
index++; index++;
// TODO: 调整堆成为最小堆
} }
// 出堆 直到下一个车能够入堆为止 // 出堆 直到下一个车能够入堆为止
// while () { // while () {
......
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