第一题
import heapq
def divingBoard(a, b, k):
result = []
heapq.heapify(result)
for i in range(k + 1):
heapq.heappush(result, i * a + (k - i) * b)
start = heapq.heappop(result)
out = '[' + str(start)
for _ in range(k):
cur = heapq.heappop(result)
if start < cur:
out += (',' + str(cur))
start = cur
out += ']'
return out
_a = int(input())
_b = int(input())
_k = int(input())
if _k == 0:
print('[]')
else:
print(divingBoard(_a, _b, _k))
第二题
- 递归求解,过了55%,不知道哪错了
- Java给写了这么多代码,输入都不用处理
- Python几行就没了,气抖冷,Python什么时候才能站起来
import java.util.*;
import java.util.stream.Collectors;
class WorkflowNode {
String nodeId;
int timeoutMillis;
List<WorkflowNode> nextNodes;
boolean initialised;
public static WorkflowNode load(String value) {
// Create head node;
Map<String, WorkflowNode> map = new HashMap<>();
WorkflowNode head = new WorkflowNode("HEAD", 0, null);
map.put(head.nodeId, head);
try {
for (String nodeValue : value.split("\\|")) {
String[] properties = nodeValue.split("\\`");
// if (!map.containsKey(properties[0])) return null;
WorkflowNode node = map.get(properties[0]);
node.timeoutMillis = Integer.parseInt(properties[1]);
node.initialised = true;
// Check next nodes
if (properties[2].equals("END")) {
continue;
}
node.nextNodes = Arrays.stream(properties[2].split(","))
.map(p -> new WorkflowNode(p, 0, null))
.collect(Collectors.toList());
node.nextNodes.forEach(p -> map.put(p.nodeId, p));
map.put(node.nodeId, node);
}
} catch (Exception e) {
return null;
}
return head;
}
public WorkflowNode(String nodeId, int timeoutMillis, List<WorkflowNode> nextNodes) {
this.nodeId = nodeId;
this.timeoutMillis = timeoutMillis;
this.nextNodes = nextNodes;
}
}
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
StringBuilder input = new StringBuilder();
while (cin.hasNext()) {
input.append(cin.next());
}
WorkflowNode node = WorkflowNode.load(input.toString());
if (node == null) System.out.println(-1);
else System.out.println(calculate(node));
}
public static int calculate(WorkflowNode head) {
if (head.nextNodes == null || head.nextNodes.size() == 0) return head.timeoutMillis;
int maxTime = 0;
for (WorkflowNode next : head.nextNodes) {
maxTime = Math.max(maxTime, calculate(next));
}
return maxTime + head.timeoutMillis;
}
}
全部评论
(5) 回帖