CF1120B.Once in a casino
省选/NOI-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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。玩家每次可投入一枚硬币,然后对任意两个相邻的数字分别执行“加 1”或“减 1”操作(即:对某一对相邻数字,一个加 1,另一个减 1)。执行该操作后,所有数字必须仍为 0 至 9 之间的整数,且最高位数字不能为 0。换言之,禁止对数字 9 执行加 1 操作,禁止对数字 0 执行减 1 操作,也禁止对最高位的 1 执行减 1 操作。一旦屏幕上的数字变为 b,玩家即赢得头奖。已知 a 和 b 的位数相同。
请帮助玩家确定赢得头奖所需的最少硬币数,并说明具体操作方法。
输入格式
The first line contains a single integer n (2≤n≤105) standing for the length of numbers a and b.
The next two lines contain numbers a and b, each one on a separate line (10n−1≤a,b<10n).
第一行包含一个整数 n(2≤n≤105),表示数字 a 和 b 的位数。
接下来两行每行各包含一个数字,分别为 a 和 b(10n−1≤a,b<10n)。
输出格式
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,105) lines should follow, i-th of them containing two integers di and si (1≤di≤n−1, si=±1) denoting that on the i-th step the player should add si to the di-th and (di+1)-st digits from the left (e. g. di=1 means that two leading digits change while di=n−1 means that there are two trailing digits which change).
Please notice that the answer may be very big and in case c>105 you should print only the first 105 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。
否则,第一行必须输出玩家需要花费的最少硬币数 c。
接下来应输出 min(c,105) 行,其中第 i 行包含两个整数 di 和 si(满足 1≤di≤n−1,且 si=±1),表示在第 i 步中,玩家应对从左往右数第 di 位和第 (di+1) 位数字加上 si(例如,di=1 表示改变最前面两位数字,而 di=n−1 表示改变最后两位数字)。
请注意,答案可能非常大;当 c>105 时,你只需输出前 105 步操作。只要在你所输出的操作之后,仍能以最少硬币数完成头奖,你的答案即被视为正确。特别地,若存在多种可行方案,你可输出其中任意一种。
输入输出样例
输入#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 223 into 333, and then make a -1 operation on the last two digits, transforming 333 into 322.
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.
在第一个例子中,我们可以对前两位数字执行一次 +1 操作,将数字 223 变为 333;然后对后两位数字执行一次 −1 操作,将 333 变为 322。
也可以按相反顺序执行这些操作,这同样构成一个正确答案。
在最后一个例子中,可以证明无法将 35 变为 44。
输入解题思路,AI测评打分。不知道怎么写?