I can’t solve the issue of distance between friends

Asked

Viewed 107 times

-1

i made a simple program to solve an Obi exercise every time Obi fix the Error progamma please someone can fix.

exercise link: https://olimpiada.ic.unicamp.br/pratique/p1/2019/f1/amigos/

 N = int(input())
lista = []
maxi = []
for r in range(0,N):
    i = int(input())
    if i == 0:
        lista.append(i+1)
    else:
        lista.append(i)
contl = -1
contg = 0
while contg < N+1:
    for r in range(0,N-1):
        S = lista[0+contg]+lista[contl] + N+contl
        maxi.append(S)
        contl -= 1   
    contl = -1  
    contg += 1  
    N = N - 1
resul = max(maxi)
print(resul)

error:

inserir a descrição da imagem aqui

  • 1

    Please click on [Edit] and put the code and other messages as text. Putting them as image is not ideal, understand the reasons reading the FAQ. It is also interesting to bring the statement here, because the questions can not depend on external links (they can serve as a complement, but the question must have all the necessary information)

1 answer

1

This question refers to the problem Distance between friends made available by OBI 2019 and. subsequently, made available by the website URI Online Judge with the same title and numbering 3050 of the category AD-HOC.

Behold Here the integrity of the statement made available by the OBI and, if you prefer, see Here the integrity of the statement made available by the website URI - which by the way is the same.

To resolve this issue we must take into consideration a few things.

First of all we must capture the values correctly. The correct capture of the values is:

n = int(input())
p = [int(i) for i in input().split()]

Note that n is the amount of values that will be entered in the next input. In the next input we should insert exactly the quantity of values specified above.

After that we must calculate the correct distance between the building and the top floor of building 0. For this we must implement the following loop:

dist = 0
k = -1
for i in range(n):
    d = p[0] + i + p[i]
    if d > dist:
        dist = d
        k = i

And finally, we must calculate the position of the friend farthest from the top floor of building K. For this we must implement another loop of repetition:

max_dist = 0
for j in range(n):
    if j != k:
        max_dist = max(max_dist, p[k] + abs(k - j) + p[j])

print(max_dist)

Note that in this last stage we are calculating the maximum distance between friends.

The complete code would be:

n = int(input())
p = [int(i) for i in input().split()]

dist = 0
k = -1
for i in range(n):
    d = p[0] + i + p[i]
    if d > dist:
        dist = d
        k = i

max_dist = 0
for j in range(n):
    if j != k:
        max_dist = max(max_dist, p[k] + abs(k - j) + p[j])

print(max_dist)

For the record, this issue has already been tested, submitted and properly approved.

  • Awesome guy vlw a doubt I tried to add value manually in the algorithm is gave error can explain me pq ?

  • This is the default entry for all Obi’s problems ? sorry I’m a beginner in the programming.

  • could comment on the errors of my code would help me a lot if it is not too uncomfortable.

  • Do not understand this command p = [int(i) for i in input(). split()]

  • Friend this enters is just one of dozens of possible entries of the OBI. This entry is the one that works in this issue. In the first input you must enter a value - example 5 - and press ENTER. In the second input, you must enter all 5 values, on the same line and separated by a single space and press ENTER.

  • The command line - p = [int(i) for i in input(). split()] - is nothing more than a List Comprehension. This line of code serves to capture more than one value per line.

  • Because your code presents some errors, I found by well reassemble it, instead of explaining all the errors.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.