AT_arc219_f.Range Division

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a sequence of non-negative integers A=(A1,A2,…,AN)A=(A_1,A_2,\ldots,A_N) of length NN.

You can perform the following operation on AA zero or more times. In one operation, perform the following steps in order:

  • Choose a pair of integers (l,r)(l,r) satisfying all of the following conditions:
    • 1≤l≤r≤N1\le l\le r\le N
    • Al,Al+1,…,ArA_l,A_{l+1},\ldots,A_r all have the same parity (that is, they are all even or all odd).
  • For each k=l,l+1,…,rk=l,l+1,\ldots,r, replace AkA_k with ⌊Ak2⌋\displaystyle\left\lfloor\frac{A_k}2 \right\rfloor.

Find the minimum number of operations needed to make all elements of AA equal to 00.

You are given TT test cases; solve each of them.

给你一个长度为 NN 的非负整数序列 A=(A1,A2,…,AN)A=(A_1,A_2,\ldots,A_N)。

你可以对 AA 执行零次或多次如下操作。每次操作按顺序执行以下步骤:

  • 选择一对满足以下所有条件的整数 (l,r)(l,r):
    • 1≤l≤r≤N1\le l\le r\le N
    • Al,Al+1,…,ArA_l,A_{l+1},\ldots,A_r 具有相同的奇偶性(即全为偶数或全为奇数)。
  • 对每个 k=l,l+1,…,rk=l,l+1,\ldots,r,将 AkA_k 替换为 ⌊Ak2⌋\displaystyle\left\lfloor\frac{A_k}2 \right\rfloor。

求使 AA 的所有元素均变为 00 所需的最少操作次数。

你将得到 TT 组测试数据;请分别求解每组数据。

输入格式

The input is given from Standard Input in the following format:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

Each test case is given in the following format:

NN
A1A_1 A2A_2 …\ldots ANA_N

输入从标准输入中按以下格式给出:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

每个测试用例按以下格式给出:

NN
A1A_1 A2A_2 …\ldots ANA_N

输出格式

Output the answers for the test cases in order, separated by newlines.

按顺序输出测试用例的答案,答案之间用换行符分隔。

输入输出样例

  • 输入#1

    4
    2
    9 12
    3
    3 1 2
    1
    0
    7
    6 28 24 18 12 22 1

    输出#1

    5
    3
    0
    10

说明/提示

Sample 1 Explanation:
Consider the first test case.

By performing operations as follows, all elements of AA can be made 00 in five operations:

  • Choose (l,r)=(1,1)(l,r)=(1,1). AA becomes (4,12)(4,12).
  • Choose (l,r)=(1,2)(l,r)=(1,2). AA becomes (2,6)(2,6).
  • Choose (l,r)=(1,2)(l,r)=(1,2). AA becomes (1,3)(1,3).
  • Choose (l,r)=(1,2)(l,r)=(1,2). AA becomes (0,1)(0,1).
  • Choose (l,r)=(2,2)(l,r)=(2,2). AA becomes (0,0)(0,0).

It is impossible to make all elements of AA equal to 00 in fewer than five operations, so output 55.

Constraints

  • 1≤T1\le T
  • 1≤N≤301\le N\le 30
  • 0≤Ai<2600\le A_i< 2^{60}
  • The sum of NN over all test cases is at most 3030.
  • All input values are integers.

样例 1 解释:
考虑第一个测试用例。

通过执行如下操作,可在五次操作内将数组 AA 的所有元素变为 00:

  • 选择 (l,r)=(1,1)(l,r)=(1,1),此时 AA 变为 (4,12)(4,12)。
  • 选择 (l,r)=(1,2)(l,r)=(1,2),此时 AA 变为 (2,6)(2,6)。
  • 选择 (l,r)=(1,2)(l,r)=(1,2),此时 AA 变为 (1,3)(1,3)。
  • 选择 (l,r)=(1,2)(l,r)=(1,2),此时 AA 变为 (0,1)(0,1)。
  • 选择 (l,r)=(2,2)(l,r)=(2,2),此时 AA 变为 (0,0)(0,0)。

无法用少于五次操作使 AA 的所有元素变为 00,因此输出 55。

限制条件

  • 1≤T1\le T
  • 1≤N≤301\le N\le 30
  • 0≤Ai<2600\le A_i< 2^{60}
  • 所有测试用例的 NN 之和不超过 3030。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页