 # DEFACING - Editorial

Practice

Contest

EASY

## PREREQUISITES

Brute force, Greedy

## PROBLEM

We are given an integer S represented in a 7-segment display. Add some segments or digits into it to create the as large number as possible not larger than M.

## QUICK EXPLANATION

Since we want to maximize the number, it is always optimal to make S contain the same number of digits as M (assume that we allow leading zeros). Hence, there are two steps in the solution:

• Add some digits before S and some digits after S in such a way that the resulting length is equal to M's length.
• Add some segments in such a way to maximize the resulting number.

We can perform all possible ways of the first step using brute force. For each intermediate result after the first step, the second step can be performed using greedy. Finally, we output the largest resulting number over all ways.

## EXPLANATION

First, let’s introduce a compatibility matrix valid[i][j] = whether digit i can be transformed into digit j by adding some segments. The value of each cell of the matrix can be computed by hand. Here are the values:

```\ 0123456789
+----------
0|1000000010
1|1101100111
2|0010000010
3|0001000011
4|0000100011
5|0000011011
6|0000001010
7|1001000111
8|0000000010
9|0000000011
```

Let’s go through the steps in more details.

### Step 1

Let |X| be the number of digits of X. As mentioned before, in an optimal solution the number of digits of M and S must be equal. There are exactly |M| - |S| + 1 possible ways to add new digits. For example, suppose S = 89375 and M = 9247529. Then, there are 3 ways:

• Add 0 digits before S and 2 digits after S: 89375XX
• Add 1 digits before S and 1 digits after S: X89375X
• Add 2 digits before S and 0 digits after S: XX89375

Here, the new digits are represented by X’s for convenience. Let’s call the new integer after adding some digits as S*.

### Step 2

Note that the restriction that the resulting number must not have leading zeros is so that we cannot have the length resulting number is greater than |M| but that difference of length consists of all leading zeros. If that is allowed, then for test case like S = 0, M = 9 we can have 09 as the resulting number, hence making the problem trickier. Since we have in our solution that |S| = |M|, we can safely ignore that restriction.

For each possible intermediate result of Step 1, we must replace each X by an actual digit and optionally add some segments to the existing digits. We can use a greedy method here. We will iterate S* from the most significant digit through the least significant digit. For each digit, we try to transform it into the largest digit, while maintaining that the final result is not greater than M. This way, it can be proved that the result is the largest possible.

When iterating the digits from the most significant digit through the least significant digit, the choice of the current digit depends on the current prefixes of M and S*. Suppose we are now considering the i-th most significant digit. The current state would be like this:

```M  = Pp...
S* = Qq...
^
i-th most significant digit
```

where p and q are M anp S*'s i-th most significant digits, respectively, while P and Q are M and S*'s current prefixes, respectively. The (i+1)-th through the |M|-th digits are not considered yet. For example, let M = 9247529, the currently built S* = 89375XX, and i = 4, then the current state is:

```M  = 9247...
S* = 8937...
^
4-th most significant digit
```

where P = 924, Q = 893, p = 7, and q = 7.

For each state, i.e. for each step in the iteration, there are 2 possibilities to maintain that the resulting number is not greater than M:

q > p. Here, Q must be strictly less than P because otherwise the resulting number would be greater than M.

q ≤ p. Here, Q must be less than or equal to P.

From the above explanation, let’s maintain two values while iterating the digits:

• prefix_less = the maximum prefix of S* that is strictly less than the corresponding prefix of M, or -1 if there is no such prefix
• prefix_equal = the corresponding prefix of M, or -1 if we cannot have equal prefix for M and S*

We update the two values after each step in the iteration. In the end, we choose the iteration that yields the maximum value. Please consult the following pseudocode for more details. The time complexity of this solution is O(|M|^2).

```// returns the maximum possible resulting number
// if we align the first digit of S with the k-th digit of M
// or -1 if it is impossible.
function solve(M, S, k):
prefix_less = -1
prefix_equal = 0
for i = 1; i ≤ |M|; i++:
// if the resulting number must be greater than M, then impossible
if prefix_less == -1 and prefix_equal = -1
return -1
// update prefix_less
for d = 9; d ≥ 0; d--:
// if we cannot transform the digit, continue.
if i ≥ k and i ≤ k + |S| - 1 and not valid[S[i - k + 1]][d]:
continue
// found the largest valid digit.
// if the new digit is greater than the current digit, use prefix_less
if d > M[i]:
if prefix_less != -1:
prefix_less = prefix_less * 10 + d
// if the new digit is not greater than the current digit, use either prefix_less or prefix_equal
else if prefix_less != -1:
if max(prefix_less, prefix_equal) != -1:
prefix_less = max(prefix_less, prefix_equal) * 10 + d
break
// update prefix_equal
if i ≥ k and i ≤ k + |S| - 1 and not valid[S*[i - k + 1]][M[i]]:
prefix_equal = -1
else
prefix_equal = prefix_equal * 10 + M[i]
return max(prefix_less, prefix_equal)

// the main code

best = 0
for k = 1; k ≤ |M| - |S| + 1; k++:
best = max(best, solve(M, S, k))
print(best)```

## SETTER’S SOLUTION

Can be found here.

## TESTER’S SOLUTION

Can be found here.

3 Likes

Correction:

``````for d = 9; d ≤ 0; d--:
``````

should be

``for d = 9; d >= 0; d--:``
3 Likes

Correction#2:

``````else if max_less != -1:
``````

should be

``else if prefix_less != -1:``

"Since we want to maximize the number, it is always optimal to make S contain the same number of digits as M. "

This is very, very wrong and a really bad starting point.

What if, for example, M=100 and S=25, like in the example used in the task itself? There is no way to make S have as many digits as M, without it being larger than M.

can you please tell me where my code is failing, i used the same approach as described in the editorial

http://www.codechef.com/viewsolution/1741764

It would be nice if you could post the input set used to validate the solutions…

Some of the tricky test cases:

```9
274 4883530
5 268343
2 558870
10381 16146
0 6
0 9
2 200543
4987565 14398964
1042216 1815366
```

```4883499
268343
558870
10989
0
8
200543
9989989
1098898
```

Will be updated regularly Let’s add another constraint to make it harder: the chef can perform the defacing operation at most K times.

They say this is an easy problem. It took me 2 days to understand Anton’s code snippet

``````// some kind of dp
max_smaller_digit[d1] = -1;
for (int d2 = 1; d2 < 10; ++d2) {
if (mark_digits[d1][d2-1]) {
max_smaller_digit[d1][d2] = d2-1;
} else {
max_smaller_digit[d1][d2] = max_smaller_digit[d1][d2-1];
}
}
``````

WTH! From where do you come up with these tricks, Anton? I finish the 2.5 hours time only thinking how to solve a problem…God knows when the hell am I gonna actually write more than one solutions in a Cook-Off or a Long Contest as well. God bless me!

Can anybody tell me why my

``````
 is resulting in Wrong Answer ? I have used the same approach as described in the editorial.

: http://www.codechef.com/viewsolution/1745718``````

My code is resulting in wrong answer …

http://www.codechef.com/viewsolution/1775055

I have tried every test case given in posts and problem…
working correctly…

what is that I am missing ?

what is with initial values of prefix_equal and prefix_less??

http://ideone.com/VRvkWc
this is my code for the problem.It is returning correct solution for every test case(mentioned).
Someone please tell me what is wrong with this because every time I submit it shows wrong answer

The only correct solution posted by me is copied.

I have written my code for this problem. Can you please tell me whats the format of the program. I mean it is not specified how the input will be taken and how the output should.
My code read the file which is in the folder of compiler and gives output in a file. But tester going to know this. He have to change the name of the file according to his testing computer.

@syker, you have to perform all the IO from/to stdin/stdout. Just like the normal way you code.
You can check few samples by going on anyone’s profile page and then hitting any problem and then the corresponding solution.

1 Like

@anton_lunyov
Excellent problem…my first attempt at codechef and I am enjoying every bit of it. http://www.codechef.com/viewsolution/1864293

It gives a correct output for all the 12 test cases given in problem, 9 tricky cases added by you plus two more given in other comments.

I know, it doesnt mean it is a correct solution but I am unable to understand why I am still getting wrong answer.

I have used my own logic, havent looked at your solution yet, I want to figure out the entire solution on my own. Can you can provide some more test cases, which will help me in figuring out the bug on my own. I tried to create test cases on my own, by using random number generator and all, but got correct answers for all of them. So, if you have some more test cases, it will be great

P.S. : I dont expect you to go through my code as it can be tricky to understand the logic @syker >> Try these tests:

8 13

8 24

Correct Answer: 8 and 18 respectively

UPD.
Your code gives correct answer for the above tests when they are performed initially, but gives 89 when they are written after some tests like 10 100. That might be because of faulty/misplaced initialization of some check arrays.

@anton_lunyov
I am now struggling with TLE issue for this problem.

What I find odd is: time limit is the same irrespective of platform. Isn’t some languages faster as compared to others? And I haven’t seen a single successful submission for JAVA for this problem. I know, it could be a mere coincidence but it still makes me wonder.

I have tried many optimization techniques but none of them are giving any improvements. On my system I see lot of improvements with every major optimization(I have simulated huge inputs, as per given constraints, using random number generators)but when I submit here, I don’t see any improvement at all in time, every time i just get 4.04 seconds, not even slightest improvement.

Moreover, some optimization techniques which definitely should have worked have actually deteriorated the performance, e.g. if you compare the submissions:
http://www.codechef.com/viewsolution/1877893 and
http://www.codechef.com/viewsolution/1883458

the only change is in the function convertToNumber(), where instead of using String object, to append digits one by one, I used StringBuilder and append() function, which according to me should give better performance, but instead the time deteriorated to 10 seconds from 4 seconds.

Any pointers in this regard?

Why is the answer for 25, 100 is 89 but not 98 ?

025 —> 089 —> 89

250 —> 098 —> 98