A12811 | Make Product Equal One
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given $n$ numbers $a_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 $1$ 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 $1$ , in other words, we want $a_1 \cdot a_2$ $\dots$ $\cdot a_n = 1$ .
For example, for $n = 3$ and numbers $[1, -3, 0]$ we can make product equal to $1$ in $3$ coins: add $1$ to second element, add $1$ to second element again, subtract $1$ from third element, so that array becomes $[1, -1, -1]$ . And $1\cdot (-1) \cdot (-1) = 1$ .
What is the minimum cost we will have to pay to do that?
Choose one of these numbers and add or subtract $1$ 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 $1$ , in other words, we want $a_1 \cdot a_2$ $\dots$ $\cdot a_n = 1$ .
For example, for $n = 3$ and numbers $[1, -3, 0]$ we can make product equal to $1$ in $3$ coins: add $1$ to second element, add $1$ to second element again, subtract $1$ from third element, so that array becomes $[1, -1, -1]$ . And $1\cdot (-1) \cdot (-1) = 1$ .
What is the minimum cost we will have to pay to do that?
输入格式
The first line contains a single integer $n$ ( $1 \le n \le 10^5$ ) — the number of numbers.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $-10^9 \le a_i \le 10^9$ ) — the numbers.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $-10^9 \le a_i \le 10^9$ ) — the numbers.
输出格式
Output a single number — the minimal number of coins you need to pay to make the product equal to $1$ .
输入输出样例
输入 #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 $1$ to $-1$ or $-1$ to $1$ in $2$ coins.
In the second example, you have to apply at least $4$ operations for the product not to be $0$ .
In the third example, you can change $-5$ to $-1$ in $4$ coins, $-3$ to $-1$ in $2$ coins, $5$ to $1$ in $4$ coins, $3$ to $1$ in $2$ coins, $0$ to $1$ in $1$ coin.
In the second example, you have to apply at least $4$ operations for the product not to be $0$ .
In the third example, you can change $-5$ to $-1$ in $4$ coins, $-3$ to $-1$ in $2$ coins, $5$ to $1$ in $4$ coins, $3$ to $1$ in $2$ coins, $0$ to $1$ in $1$ coin.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted