CF1206B.Make Product Equal One

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given nn numbers a1,a2,…,ana_1, a_2, \dots, a_n. With a cost of one coin you can perform the following operation:

Choose one of these numbers and add or subtract 11 from it.

In particular, we can apply this operation to the same number several times.

We want to make the product of all these numbers equal to 11, in other words, we want a1⋅a2a_1 \cdot a_2 …\dots ⋅an=1\cdot a_n = 1.

For example, for n=3n = 3 and numbers [1,−3,0][1, -3, 0] we can make product equal to 11 in 33 coins: add 11 to second element, add 11 to second element again, subtract 11 from third element, so that array becomes [1,−1,−1][1, -1, -1]. And 1⋅(−1)⋅(−1)=11\cdot (-1) \cdot (-1) = 1.

What is the minimum cost we will have to pay to do that?

给你 nn 个数 a1,a2,…,ana_1, a_2, \dots, a_n。每次操作花费一枚硬币,可执行以下操作之一:

  • 任选其中一个数,将其加 11 或减 11。

特别地,同一个数可以被多次执行该操作。

我们的目标是使所有这些数的乘积等于 11,即满足 a1⋅a2⋯⋅an=1a_1 \cdot a_2 \dots \cdot a_n = 1。

例如,当 n=3n = 3 且数组为 [1,−3,0][1, -3, 0] 时,我们可以在花费 33 枚硬币的情况下使乘积变为 11:对第二个元素加 11(两次),对第三个元素减 11,得到数组 [1,−1,−1][1, -1, -1],此时 1⋅(−1)⋅(−1)=11\cdot (-1) \cdot (-1) = 1。

实现该目标所需的最小花费是多少?

输入格式

The first line contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the number of numbers.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (−109≤ai≤109-10^9 \le a_i \le 10^9) — the numbers.

第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)—— 数字的个数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(−109≤ai≤109-10^9 \le a_i \le 10^9)—— 这些数字。

输出格式

Output a single number — the minimal number of coins you need to pay to make the product equal to 11.

输出一个整数——使乘积等于 11 所需的最少硬币数量。

输入输出样例

  • 输入#1

    2
    -1 1

    输出#1

    2
  • 输入#2

    4
    0 0 0 0

    输出#2

    4
  • 输入#3

    5
    -5 -3 5 3 0

    输出#3

    13

说明/提示

In the first example, you can change 11 to −1-1 or −1-1 to 11 in 22 coins.

In the second example, you have to apply at least 44 operations for the product not to be 00.

In the third example, you can change −5-5 to −1-1 in 44 coins, −3-3 to −1-1 in 22 coins, 55 to 11 in 44 coins, 33 to 11 in 22 coins, 00 to 11 in 11 coin.

在第一个例子中,你可以将 11 改为 −1-1,或将 −1-1 改为 11,每次操作花费 22 枚硬币。

在第二个例子中,为使乘积不为 00,你至少需要执行 44 次操作。

在第三个例子中,你可以将 −5-5 改为 −1-1,花费 44 枚硬币;将 −3-3 改为 −1-1,花费 22 枚硬币;将 55 改为 11,花费 44 枚硬币;将 33 改为 11,花费 22 枚硬币;将 00 改为 11,花费 11 枚硬币。

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

首页