传送门
Description
With powerful decision-making skills, ZYB can solve complex problems
ZYB has a decimal string {s}s of length {n}n. He wants to split the string into several (at least 2) non-empty continuous substrings and minimize the difference between the maximum value and the minimum value of the gifts. The value of a continuous substring is the value of it when it’s considered as a decimal number.
For instance, when the string ‘1230’ is split into ‘12’ and ‘30’, the values of the two parts are 12 and 30 separately. Notice that the substrings are not allowed to contain leading zero(s). For example, the only feasible way of splitting ‘001’ is ‘0’, ‘0’ and ‘1’.
Can you help ZYB find the fairest splitting?
简洁题意
- 把一个数字串划成若干段(至少两段),使得最大值和最小值的差尽量的小。注意不能有前导0
一个显然而重要的性质
- a n s ≤ 9 ans\leq 9 ans≤9,因为把字符串分成单个数字后数字 x x x必为 0 ≤ x ≤ 9 0\leq x\leq 9 0≤x≤9
优化
Case 1
- 划成的每一段长度相等
- 因为 N N N 只有 1 0 5 10^5 105,我们可以枚举 N N N 的所有约数。对于给定约数,线性扫一遍判断答案、复杂度 O ( N √ N ) O(N√N) O(N√N)(而且跑不到)
Case 2
- 划成的段里最大段和最小段的长度差为1
- 答案若要小于 9 9 9,形式只可能是 99 … 9 x 99…9x