No.2844 Birthday Party Decoration
タグ : / 解いたユーザー数 85
作問者 : 👑
Ayuna
torisasami4
Problem Statement
On an infinitely long number line, there are Alice's house and shops. Alice's house is at coordinate $X$ on the number line. In addition, there is $1$ shop at every non-negative integer coordinate.
Alice is going to hold a birthday party at her house, so she has decided to buy decorations.
There are $N$ types of decorations, and the conditions on the shops where each decoration can be purchased are as follows.
- The $i$-th decoration can be purchased at the shop at coordinate $x$ if and only if $x \& 2^{c_i} \neq 0$.
Here, $a \& b$ denotes the bitwise AND of non-negative integers $a$ and $b$.
Alice can perform the following actions any number of times, $0$ or more, in any order.
- Move: Choose any integer $y$. Letting $x$ be Alice's current coordinate, change Alice's coordinate to $x + y$.
- Purchase: This can be performed only when Alice's current coordinate is a non-negative integer. Choose $1$ item that can be purchased at the shop at the current coordinate, and purchase it.
Here, the travel distance is defined as the sum of the absolute values of the chosen values over all Moves.
Initially, Alice is at her house. Find the minimum possible travel distance for Alice, by repeating actions, to purchase $1$ decoration of every type and return to her house.
You are given $T$ test cases; answer each of them.
What is bitwise AND?
The bitwise AND $a \& b$ of $2$ non-negative integers $a$ and $b$ is defined as follows.
- When $a \& b$ is written in base $2$, the digit in its $2^k$ place is $1$ if the digits in the $2^k$ place of $a, b$, each written in base $2$, are both $1$, and $0$ otherwise.
- For example, $3 \& 5 = 1$. (In base $2$: $011 \& 101 = 001$)
Constraints
- All input values are integers.
- $1 \leq T \leq 1000$
- $1 \leq N \leq 60$
- $0 \leq c_1 < c_2 < \dots < c_N < 60$
- $0 \leq X < 2^{60}$
Input
The input is given from Standard Input in the following format.
$T$
$\mathrm{case}_1$
$\mathrm{case}_2$
$\vdots$
$\mathrm{case}_T$
Each test case is given in the following format.
$N$ $X$ $c_1$ $c_2$ $\dots$ $c_N$
Output
Print $T$ lines. On the $i(1 \leq i \leq T)$-th line, print the answer to the $i$-th test case.
Sample
Sample 1
Input
4 2 10 0 2 1 0 3 4 15 0 1 2 3 5 20240822 0 5 7 8 18
Output
4 16 0 2
For the $1$-st test case, it is optimal to act as follows.
- First, move from the house to coordinate $11$.
- Next, purchase the $1$-st type of decoration at the shop at coordinate $11$.
- Next, move from coordinate $11$ to coordinate $12$.
- Next, purchase the $2$-nd type of decoration at the shop at coordinate $12$.
- Finally, return from coordinate $12$ to the house.
The travel distance in this case is $4$. This sequence of actions is illustrated in the figure below.
For the $2$-nd test case, the closest shop to the house at which the decoration can be purchased is the shop at coordinate $8$. Therefore, it is optimal to move from coordinate $0$ to coordinate $8$, purchase the decoration at coordinate $8$, and move from coordinate $8$ to coordinate $0$. The travel distance in this case is $16$.
For the $3$-rd test case, all the decorations can be purchased at the shop at the same coordinate as Alice's house. Therefore, the minimum travel distance is $0$.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。