#K3236. Load Balancer Simulation

    ID: 24914 Type: Default 1000ms 256MiB

Load Balancer Simulation

Load Balancer Simulation

You are required to implement a simple load balancing simulation. In this problem, there are n worker nodes and a sequence of data packets arriving one after another. Each packet has an integer identifier (packet_id) and a payload string. When a packet arrives, if it has been seen before, it must be processed by the same worker node as before. Otherwise, the worker node is chosen by the formula \(worker\_id = packet\_id \mod n\). Each worker, upon receiving a packet, processes it by outputting a message in the format:

Worker <worker_id> processing packet <packet_id>

The payload is given but is not used in determining the worker node. Your program should read from standard input and output the processing messages to standard output.

inputFormat

The input is read from standard input and has the following format:

  • The first line contains two integers: n (the number of worker nodes) and m (the number of packets).
  • The next m lines each contain a packet description in the format: packet_id payload where packet_id is an integer and payload is a string (without spaces).

outputFormat

Output exactly m lines to standard output. Each line should display the message returned from processing the corresponding packet. The format for each line is:

Worker <worker_id> processing packet <packet_id>

Here, worker_id is computed as follows: if the packet with packet_id has been seen before, maintain the same worker_id as when it was first processed. Otherwise, compute worker_id = packet_id \mod n (where \(n\) is the number of worker nodes).

## sample
3 4
1 payload1
2 payload2
1 payload3
3 payload4
Worker 1 processing packet 1

Worker 2 processing packet 2 Worker 1 processing packet 1 Worker 0 processing packet 3

</p>