Toasts#510

Toasts (Editorial)

If n = 0, the answer is .

If the number of toasts does not exceed the pan's capacity k~(n \le k), then toasting all the toasts on both sides will take minutes.

Since each toast must be toasted on both sides, a total of toasting rounds is required. Each toasting round (placing up to toasts on the pan at the same time) takes minutes.

Therefore, if n > k, the minimum time required to toast all the toasts is 2 \cdot \lceil 2n / k \rceil minutes.

Example

Suppose there are n = 3 toasts, and the pan can hold k = 2 toasts at a time. Let us label the sides of the toasts as 1a, 1b, 2a, 2b, 3a, 3b.

Then all sides can be toasted in minutes as follows:

Total: toasting rounds of minutes each, which makes minutes.

Algorithm implementation

Read the input data.

scanf("%d %d",&n,&k);

If n = 0, the answer is .

if (!n) res = 0; else

If the number of toasts does not exceed the capacity of the pan, it will take minutes to toast them.

if (n <= k) res = 4; else
{

Each toast must be toasted on both sides, which means that the total number of sides to toast is . Each toasting session (regardless of the number of toasts on the pan, as long as it does not exceed its capacity) takes minutes.

  res = 2 * n / k;
  if (2 * n % k) res++;
  res *= 2;
}

Print the answer.

printf("%d\n",res);

Java implementation

import java.util.*;

public class Main
{
  public static void main(String []args)
  {
    Scanner con = new Scanner(System.in);
    int n = con.nextInt();
    int k = con.nextInt();
    int res;
  
    if (n == 0) res = 0; else    
    if (n <= k) res = 4; else
    {
      res = 2 * n / k;
      if (2 * n % k > 0) res++;
      res *= 2;
    }        
    System.out.println(res);
    con.close();
  }
}