A12420 | Once in a casino
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
One player came to a casino and found a slot machine where everything depends only on how he plays. The rules follow.
A positive integer $a$ is initially on the screen. The player can put a coin into the machine and then add $1$ to or subtract $1$ from any two adjacent digits. All digits must remain from $0$ to $9$ after this operation, and the leading digit must not equal zero. In other words, it is forbidden to add $1$ to $9$ , to subtract $1$ from $0$ and to subtract $1$ from the leading $1$ . Once the number on the screen becomes equal to $b$ , the player wins the jackpot. $a$ and $b$ have the same number of digits.
Help the player to determine the minimal number of coins he needs to spend in order to win the jackpot and tell how to play.
A positive integer $a$ is initially on the screen. The player can put a coin into the machine and then add $1$ to or subtract $1$ from any two adjacent digits. All digits must remain from $0$ to $9$ after this operation, and the leading digit must not equal zero. In other words, it is forbidden to add $1$ to $9$ , to subtract $1$ from $0$ and to subtract $1$ from the leading $1$ . Once the number on the screen becomes equal to $b$ , the player wins the jackpot. $a$ and $b$ have the same number of digits.
Help the player to determine the minimal number of coins he needs to spend in order to win the jackpot and tell how to play.
输入格式
The first line contains a single integer $n$ ( $2 \le n \le 10^5$ ) standing for the length of numbers $a$ and $b$ .
The next two lines contain numbers $a$ and $b$ , each one on a separate line ( $10^{n-1} \le a, b < 10^n$ ).
The next two lines contain numbers $a$ and $b$ , each one on a separate line ( $10^{n-1} \le a, b < 10^n$ ).
输出格式
If it is impossible to win the jackpot, print a single integer $-1$ .
Otherwise, the first line must contain the minimal possible number $c$ of coins the player has to spend.
$\min(c, 10^5)$ lines should follow, $i$ -th of them containing two integers $d_i$ and $s_i$ ( $1\le d_i\le n - 1$ , $s_i = \pm 1$ ) denoting that on the $i$ -th step the player should add $s_i$ to the $d_i$ -th and $(d_i + 1)$ -st digits from the left (e. g. $d_i = 1$ means that two leading digits change while $d_i = n - 1$ means that there are two trailing digits which change).
Please notice that the answer may be very big and in case $c > 10^5$ you should print only the first $10^5$ moves. Your answer is considered correct if it is possible to finish your printed moves to win the jackpot in the minimal possible number of coins. In particular, if there are multiple ways to do this, you can output any of them.
Otherwise, the first line must contain the minimal possible number $c$ of coins the player has to spend.
$\min(c, 10^5)$ lines should follow, $i$ -th of them containing two integers $d_i$ and $s_i$ ( $1\le d_i\le n - 1$ , $s_i = \pm 1$ ) denoting that on the $i$ -th step the player should add $s_i$ to the $d_i$ -th and $(d_i + 1)$ -st digits from the left (e. g. $d_i = 1$ means that two leading digits change while $d_i = n - 1$ means that there are two trailing digits which change).
Please notice that the answer may be very big and in case $c > 10^5$ you should print only the first $10^5$ moves. Your answer is considered correct if it is possible to finish your printed moves to win the jackpot in the minimal possible number of coins. In particular, if there are multiple ways to do this, you can output any of them.
输入输出样例
输入 #1
3 223 322
输出 #1
2 1 1 2 -1
输入 #2
2 20 42
输出 #2
2 1 1 1 1
输入 #3
2 35 44
输出 #3
-1
In the first example, we can make a +1 operation on the two first digits, transforming number $\textbf{22}3$ into $\textbf{33}3$ , and then make a -1 operation on the last two digits, transforming $3\textbf{33}$ into $3\textbf{22}$ .
It's also possible to do these operations in reverse order, which makes another correct answer.
In the last example, one can show that it's impossible to transform $35$ into $44$ .
It's also possible to do these operations in reverse order, which makes another correct answer.
In the last example, one can show that it's impossible to transform $35$ into $44$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted