5
\$\begingroup\$

This is a URI online judge problem (problem no: 1973).
Link : https://www.urionlinejudge.com.br/judge/en/problems/view/1973

After buying many adjacent farms at the west region of Santa Catarina, the Star family built a single road which passes by all farms in sequence. The first farm of the sequence was named Star 1, the second Star 2, and so on. However, the brother who lives in Star 1 has got mad and decided to make a Star Trek in order to steal sheep from the proprieties of his siblings. But he is definitely crazy. When passes by the farm Star i, he steals only one sheep (if there is any) from that farm and moves on either to Star i + 1 or Star i - 1, depending on whether the number of sheep in Star i was, respectively, odd or even. If there is not the Star to which he wants to go, he halts his trek. The mad brother starts his Star Trek in Star 1, stealing a sheep from his own farm.

Input

The first input line consists of a single integer N (1 ≤ N ≤ 10⁶), which represents the number of Stars. The second input line consists of N integers, such that the ith integer, Xi (1 ≤ Xi ≤ 10⁶), represents the initial number of sheep in Star i.

Output

Output a line containing two integers, so that the first represents the number of Stars attacked by the mad brother and the second represents the total number of non-stolen sheep.

Sample Input          Sample Output
8                     8 68
1 3 5 7 11 13 17 19   

8                     7 63
1 3 5 7 11 13 16 19   

I have solved the problem with two ways and they also give the desired output, but every time I submitted it it says time limit exceeded.

#1st solution:
num_star = int(input())
sheep = list(map(int, input().split()))
star = set()
index = 0

while index != num_star:
    if sheep[index] == 0:
        break
    elif sheep[index] % 2 == 1:
        star.add(index)
        sheep[index] -= 1
        index += 1
    else:
        star.add(index)
        sheep[index] -= 1
        index -= 1
        if index == -1:
            break

print(len(star), sum(sheep))

#2nd solution
n = int(input())
x = list(map(int, input().split()))
i = 0
farm_visited = 0
while i in range(n):
    if x[i] == 0:
        if i >= farm_visited: farm_visited = i+1
        break
    elif (x[i]) % 2 == 1:
        if i >= farm_visited: farm_visited = i + 1
        x[i] -= 1
        i += 1
    else:
        if i >= farm_visited: farm_visited = i + 1
        x[i] -= 1
        i -= 1
print(farm_visited, sum(x))
\$\endgroup\$
  • 1
    \$\begingroup\$ Generally Code Review's interpretation of "working code" encompasses "code that is too slow", so I do not think that this is off-topic for that reason. It could use some more general description of the performance analysis you've already done, and your specific concerns. \$\endgroup\$ – Reinderien Apr 15 at 16:24
  • 1
    \$\begingroup\$ Please change your title to summarize what the code is doing. \$\endgroup\$ – Peilonrayz Apr 16 at 13:00
6
\$\begingroup\$

set in the first solution, and while i in range(n) in the second one are very expensive.

That said, thou shalt not bruteforce.

The crazy guy moves right as long as he visits the odd-populated farms, leaving a trail of even-populated farms behind. As soon as he hits the even-populated farm, he switches direction, and since now on he faces only the even-populated farms, he goes all the way to the Star 1, and the track stops there.

So, find the leftmost even-populated farm. If there is none, the guy would visit all farms, stealing one ship per farm. If there is, its index is the number of farms visited; on the way there, one ship per farm will be stolen, and on the way back another ship per farm will be stolen (except if initially there was just one ship).

This should be enough to keep you going.

As a side note, break in

    if x[i] == 0:
        if i >= farm_visited: farm_visited = i+1
        break

is a bug. An empty farm should not stop him:

he steals only one sheep (if there is any) from that farm and moves on

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ (The theme has been Star Trek West of Santa Catarina, not Pirate in the Family.) \$\endgroup\$ – greybeard May 10 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.