DICEGAME3 - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: yash_daga
Tester: jay_1048576
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

Chef is playing a game, and will roll a regular 6-sided die N times.
His scores is calculated using the following rule.
Suppose he rolls X on a certain turn. Then,

  • If this is the first turn, or the previous roll was not a 1, add X to the score.
  • Otherwise, add 2X to the score.

What’s the maximum possible score Chef can attain?

EXPLANATION:

Notice that rolling a 1 followed by a 6 gives a higher score than rolling two sixes, because:

  • Two sixes give a score of 6+6=12.
  • A 1 and a 6 give a score of 1 + 2\cdot 6 = 13.

So, the maximum possible score is attained by the sequence (1, 6, 1, 6, 1, 6, \ldots)
If N is odd, you’ll have one extra element at the end, this might as well be a 6 for maximum score.

This means the answer is:

  • 13\cdot \frac{N}{2}, if N is even.
  • 13\cdot \frac{N-1}{2} + 6, if N is odd.

TIME COMPLEXITY:

\mathcal{O}(1) per testcase.

CODE:

Editorialist's code (Python)
for _ in range(int(input())):
    n = int(input())
    ans = 13*(n//2)
    if n%2 == 1: ans += 6
    print(ans)

I spent 60 minutes on trying to figure out why my solution is incorrect. Can someone please help me by pointing it out?

for _ in range (int(input())):
    n=int(input())
    score=0
    if n==1:
        print(6)
    else:
        if n%2==0:
            score= (n/2) * 13
        else:
            f= ((n-1)/2)
            score= f*13 + 6
    print(score)
            

Use integer division in python, not normal division.
That means n//2 instead of n/2, and (n-1)//2 instead of (n-1)/2.

For example for N = 2 the expected output is 13, whereas your code prints 13.0 (they’re equal, yes, but when the expected output is an integer, you should print only an integer.
You can also print int(score) instead of just score.

I tried the int(score) method as shown below but it still wasn’t accepting my code. can you please help me by explaining why?

for _ in range (int(input())):
    n=int(input())
    score=0
    d=n/2
    if n==1:
        print(6)
    else:
        if n%2==0:
            
            score= (n/2) * 13
        
        else:
            f= (n-1)/2
            score= f*13 + 6
            
    print(int(score))

Look at what your code outputs for the input

1
1

The output is:

6
0

Yes, and it should be just

6

right? There’s only one test, after all.

Yes, correct. What am I missing here?

It’s a good idea to try and debug this yourself, that’s a skill you’ll use very often in contests (and other contexts too, most likely).
Try walking through your code with that input and see what it does, and why it’s doing that.

If you really want the answer:

Spoiler

Two numbers are being printed, which of course means two print statements are being executed.
Your code only has two at all; so they’re both being executed, which isn’t what you want.
Notice that the print(ans) statement is always executed, but you actually want it to happen only when N\neq 1.

tl;dr your indentation is wrong.

Oh man!
Thank you so much for the help.
Gonna go cry in the corner. :slightly_smiling_face: