아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Unlock the Padlock

시간 제한30초메모리 제한1024 MB

요약
크기 D인 N개의 다이얼을 모두 0으로 만들기 위해 필요한 중첩 범위 회전의 최소 횟수를 구한다.
난이도

보통10점 중 6점

유형
그리디, 구현
정답자
아직 제출이 없습니다

문제

Imagine you have a padlock, which is a combination lock consisting of NN dials, set initially to a random combination. The dials of the padlock are of size DD, which means that they can have values between 00 and D−1D-1, inclusive, and can be rotated upwards or downwards. They are also ordered from left to right, with the leftmost and rightmost dials at positions 11 and NN, respectively. The padlock can be unlocked by setting the values of all its dials to 00.

You can perform zero or more operations of this kind:

  • Pick any range \[l,r]\[l,r] such that 1≤l≤r≤N1≤l≤r≤N and rotate all the dials in \[l,r]\[l,r] together, upwards or downwards. Rotating up increases the value of each dial in the range \[l,r]\[l,r] by 11, and rotating down decreases its value by 11. Note that a dial with value D−1D-1 becomes 00 when increased (rotated up) and a dial with value 00 becomes D−1D-1 when decreased (rotated down).

The series of operations must satisfy the following condition:

  • The range \[l_i−1,r_i−1]\[l\_{i-1}, r\_{i-1}] chosen in the (i−1)(i-1)-th operation needs to be completely contained within the range \[l_i,r_i]\[l\_i,r\_i] chosen in the ii-th operation; that is, l_i≤l_i−1≤r_i−1≤r_il\_i≤l\_{i-1}≤r\_{i-1}≤r\_i. The initial range (\[l_1,r_1]\[l\_1,r\_1]) can be chosen arbitrarily.

Example of a valid sequence of operations to unlock a padlock with initial combination \[1,1,2,2,3,3]\[1,1,2,2,3,3]:

  1. Rotate range \[5,6]\[5,6] downwards.
  2. Rotate range \[3,6]\[3,6] downwards.
  3. Rotate range \[1,6]\[1,6] downwards.

The following are some operations that cannot be performed:

  1. Rotating range \[1,4]\[1,4] after \[6,9]\[6,9], because \[6,9]\[6,9] is not completely contained in \[1,4]\[1,4] (does not satisfy r_i−1≤r_ir\_{i-1}≤r\_i where r_i−1=9r\_{i-1}=9 and r_i=4r\_i=4).
  2. Rotating range \[3,6]\[3,6] after \[2,7]\[2,7].

The goal for you is to output the minimum number of valid operations needed to make all dials in the padlock set to 00.

입력

The first line of the input contains the number of test cases, TT. TT test cases follow.

Each test case consists of two lines.

The first line of each test case contains two integers NN and DD, representing the number of dials in the padlock and the size of the dials, respectively.

The second line of each test case contains NN integers V_1,V_2,…,V_NV\_1,V\_2,\dots ,V\_N, where the ii-th integer represents the value of the ii-th dial in the initial combination of the padlock.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yy is the minimum number of operations needed to unlock the padlock as described in the statement.

제한

  • 1≤T≤1001≤T≤100.
  • 0≤V_i≤D−10≤V\_i≤D-1, for all ii.

예제2

  1. 예제 1

    입력
    2
    6 2
    1 1 0 1 0 1
    6 2
    0 1 0 0 1 1
    
    예상 출력
    Case #1: 3
    Case #2: 2
    
  2. 예제 2

    입력
    2
    6 10
    1 1 2 2 3 3
    6 10
    1 1 9 9 1 1
    
    예상 출력
    Case #1: 3
    Case #2: 3