Can anyone help me with Pro Chef And Katana from LoC June 2018 ?

help
codechef
loc
short-contest

#1

Hello friends can someone tell me how to approach this problem ? I am getting WA.

@vijju123 @taran_1407 @vivek_1998299 @meooow @john_smith_3 @aryanc403 @vbt_95

Here is my latest submission. If it’s too messy, here is the approach I used :

n =readInt()
arr = readInts()
count = 0
for i in range(len(arr)):
    count+=1
    f =0 
    minus = arr*
    for j in range(len(arr)):
        arr[j]-=minus
    for j in range(len(arr)):
        if arr[j]>0:
            f =1
    if f==0:
        break
print(count)

#2

You’ll need to subtract the MINIMUM element of the segment from each element of the segment. By segment I mean, a contiguous range, surrounded by a 0 at both sides.

Eg. 1, 4, 7, 0, 4, 0, 7, 8 has three segments; 1 is minimum of the first segment, 4 of the second and 7 of the third segment.

Try the simple test:

2
2 1

Your code gives 1 as answer, but you need atleast 2 cuts.


#3

At least make sure arr* > 0 before subtracting it from the whole array.


#4

n =readInt()

arr = readInts()

count = 0

for i in range(len(arr)):

count+=1

f =0 

minus = arr*

if minus <0:

    continue

for j in range(i,len(arr)):

    arr[j]-=minus

for j in range(i,len(arr)):

    if arr[j]>0:

        f =1

if f==0:


    break

print(count)

@sarthakmanna here is the approach where am I missing. Also for 100 points I feel it’s too slow.


#5

I was trying to explain this approach: https://www.codechef.com/viewsolution/19150087

For 100pts, you can use a stack or priority queue like this: https://www.codechef.com/viewsolution/19011529

The 100pt solution is written in Java. I hope you’ll have no problem in understanding it. However, if you have any, feel free to ask.


#6

thanks man I got it now. Let me code it out using stack :slight_smile: