Deleting arrays

2.6

14 votes
Basic Programming, Algorithms, Basics of Greedy Algorithms, Greedy Algorithms
Problem

You are given two integers \(N\) and \(K\) denoting the number of elements in the array and an arbitrary integer respectively. You can perform the following operations:

  1. Select an index \(i\).
  2. Delete the \(i^{th}\) element.
  3. Delete \(K\) elements to the left of it if they exist or you can delete all the elements if the number of elements to the left of it is less than \(K\).
  4. Delete \(K\) elements to the right of it if they exist or you can delete all if the number of elements to the right of it is less than \(K\).

Your task is to delete all the elements of the array using the minimum number of provided operations.

Input format

  • The first line contains an integer \(T\) denoting the number of test cases.
  • The first line of each test case contains a single line containing two space-separated integers \(N\) and \(K\).

Output format

Print \(i^{th}\) \(T\) lines denoting the answer to the \(i^{th}\) test case.

Constraints

\( 1\leq T \leq 20000\)

\(1 \leq N,K \leq 1e9\)

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

There are 4 elements if we choose 2nd index we can remove 1st,2nd and 3rd in first operation. In second operation we can choose 4th index and remove it.

Editor Image

?