Huawei OA Interview Coding Question: Balanced Task Migration

19 Views
No Comments

A data center has K computing nodes. Each node has a current load T and C CPU cores. The maximum load a single CPU core can handle is 200, so the total capacity of a node is C * 200. The CPU utilization of a node is defined as T / (C * 200).

A sudden spike of tasks occurs on one node. To keep all nodes balanced, you need to migrate tasks from the overloaded node to the other nodes so that the CPU utilizations of all nodes become as equal as possible, while preserving the relative load levels across nodes.

Given the overloaded node’s extra load N, output the amount of additional load that should be assigned to each computing node.

Notes:

  • If the overloaded node’s load exceeds the total load capacity of all nodes, no redistribution is needed and the additional load for every node is 0.
  • If the overloaded node’s load does not exceed the total capacity of all nodes, the final allocation should be completely balanced, meaning the CPU utilizations of all computing nodes should be equal, with precision no less than 0.001.

Input

  • The first line contains N, K.
  • The second line contains K integers C[1..K].
  • The third line contains K integers T[1..K].

Output

Print the additional load for each node.

Example

Input:
1180
3
45 28 45
6750 4200 6750

Output:
450 280 450

Explanation: The three nodes have 45, 28, and 45 CPU cores, and current loads 6750, 4200, and 6750. After migrating 450, 280, and 450 units of load respectively, their CPU utilizations become equal.

This Huawei OA question is about balanced load migration across computing nodes with different CPU capacities. Since each node’s capacity is proportional to its core count times 200, the extra load should be distributed according to capacity ratios so that all nodes end up with the same CPU utilization when possible. If the total load already exceeds the system capacity, the answer is simply all zeros. The sample illustrates a proportional allocation: 45, 28, and 45 cores receive 450, 280, and 450 units respectively.

END
 0