#C7296. Bike Rental Status

    ID: 51151 Type: Default 1000ms 256MiB

Bike Rental Status

Bike Rental Status

You are given a simulation problem about a bike rental system. There are S bike shops and each shop initially has a certain number of bikes. There are P people waiting to rent a bike. Every person has a preferred bike shop. When a person arrives:

  • If the preferred shop has at least one bike available, the person rents a bike from that shop.
  • If not, the person searches the shops sequentially (from shop 1 to shop S) and rents a bike from the first shop that has one available.

Your task is to compute for each test case:

  • The number of people who managed to rent a bike from their preferred shop.
  • The total number of people who successfully rented a bike.
  • </p>

    Formally, for each test case with shops indexed by i (1 ≤ i ≤ S), let bikes[i] be the initial number of bikes at shop i, and a list of P integers representing the preferred shop of each person in order. For each person, if bikes[p] (with p as the preferred shop) is positive, then a bike is taken from that shop; otherwise, the person takes a bike from the first shop (in increasing order) that still has bikes available. Output the count of people who used their preferred shop and the total number of successful rentals.

    The mathematical process can be formalized as follows: \[ \text{for each person: }~~ \textbf{if } bikes[p-1] > 0 \textbf{ then } \begin{cases}\text{preferred}\mathrel{+}=1\\\text{total}\mathrel{+}=1\end{cases} \quad \textbf{else: search for first } i \text{ with } bikes[i]>0 \text{ and do } bikes[i]\mathrel{-}=1, ~ total\mathrel{+}=1 \]

    inputFormat

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

    T
    S₁ P₁
    a₁ a₂ ... a₍S₁₎
    p₁ p₂ ... p₍P₁₎
    S₂ P₂
    b₁ b₂ ... b₍S₂₎
    q₁ q₂ ... q₍P₂₎
    ...
    

    Where:

    • T is the number of test cases.
    • For each test case, the first line contains two integers: S (the number of bike shops) and P (the number of people).
    • The second line contains S integers representing the initial number of bikes at each shop.
    • The third line contains P integers representing the preferred bike shop for each person (shops are 1-indexed).
    • </p>

      outputFormat

      For each test case, output a single line containing two integers separated by a space:

      • The first integer is the number of people who rented a bike from their preferred shop.
      • The second integer is the total number of people who successfully rented a bike.

      The output is written to standard output (stdout).

      ## sample
      2
      3 6
      5 3 4
      1 2 1 3 2 3
      2 5
      4 5
      1 2 2 1 2
      6 6
      

      5 5

      </p>