This is a URI online judge problem (problem no: 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 ≤ 106), which represents the number of Stars. The second input line consists of N integers, such that the ith integer, Xi (1 ≤ Xi ≤ 106), 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.
I have solved the problem and 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))
Don't read below if you want to solve it on your own.
def madstar(s): # s is the list
if all(e % 2 for e in s): # all Stars with odd numbers
return (len(s), sum(s)-len(s)) # just one sheep stolen from each Star
for i,e in enumerate(s):
if e % 2 == 0: # even number found
return (i+1, # Stars are numbered from 0, so i==0 -> 1 Star visited etc.
sum(s) - ( # stolen sheep
2*(i+1) # two for every visited Star
- s[:i].count(1) # except visited Stars with initially only 1 sheep
- (1 if e>0 else 2) # and the final one, where it is either 0 or 1, but never 2
))
for test_list in [[1,3,5,7,11,13,17,19],
[1,3,5,7,11,13,16,19],
[1],
[3,0,2],
[0],
[2]]:
print(test_list, '->', madstar(test_list))
Find here another (maybe easier?) implementation. Don't read below if you want to solve it on your own!
num_star = int(input())
def print_solution(stars_list):
sheep_taken_when_go = 0
sheep_taken_when_return = 0
visited_stars_count = 0
total_sheep = sum(stars_list)
for sheep_in_current_star in stars_list:
visited_stars_count+=1
sheep_taken_when_go += 1 if sheep_in_current_star>0 else 0 #take one sheep if available
if sheep_in_current_star%2==0:
print(visited_stars_count,total_sheep-sheep_taken_when_go-sheep_taken_when_return)
return
sheep_taken_when_return += 1 if sheep_in_current_star>1 else 0 #if we ever return, take another sheep if available
print(visited_stars_count,total_sheep-sheep_taken_when_go)
print_solution(list(map(int, input().split())))
solved thanks @Błotosmętek and thanks everyone.
def thief(stars, sheep):
pos_even = 0
for i in range(stars):
if sheep[i] % 2 == 0:
pos_even = i + 1
last_even_value = sheep[i]
break
if pos_even:
print(pos_even, (sum(sheep) - (pos_even * 2 - sheep[:i].count(1) - (1 if last_even_value > 0 else 2))))
else:
print(stars, (sum(sheep) - stars))
num_star = int(input())
s = list(map(int, input().split()[:num_star]))
thief(num_star, s)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.