Given a problem:
You have a certain amount of soldiers.
Each soldier has a given rank (some are officers, sergeants, etc). Now they are to go and kill some guys.
You have a limited amount of ammunition. Depending on the rank, each person is given a box of ammunition. The soldiers are standing in a straight line.
The person with a higher rank has to be given more ammunition if a person of lower rank is next to him.
Each person has to be given at least one box.
Example: using numbers from 1 upwards to represent rank: 4 2 3 2 2 1 3 6
. the equivalent boxes of ammunition are: 2 1 2 1 2 1 2 3
.
The fastest way for me to come up with the list of ammunition is to take the first three ranks and compare them to each other (i.e. from the example, I pick 4 2 3
).Next I increment by one (i.e. 2 3 2
) and make a comparison again. Obviously, this takes a lot of time.Is there a faster way?
NOTE: Soldiers with same rank standing next to each other don't care how much ammunition each has.
soldier_num = int(input())
i = 0
rating_array = []
ammo_array = []
can_can = soldier_num
while(i < soldier_num):
rating_array.append(int(input()))
ammo_array.append(1)
i += 1
i = 0
while(i < soldier_num):
if(i == 0):
if((rating_array[i] > rating_array[i+1]) and (ammo_array[i] <= ammo_array[i+1])):
ammo_array[i] += 1
i = i-1
can_can += 1
if(0<i<(soldier_num-1)):
if((rating_array[i] > rating_array[i+1]) and (ammo_array[i] <= ammo_array[i+1])):
ammo_array[i] += 1
i = i-1
can_can += 1
elif((rating_array[i] > rating_array[i-1]) and (ammo_array[i] <= ammo_array[i-1])):
ammo_array[i] += 1
i = i-1
can_can += 1
elif((rating_array[i] < rating_array[i-1]) and (ammo_array[i] >= ammo_array[i-1])):
ammo_array[i-1] += 1
i = i-1
can_can += 1
elif((rating_array[i] < rating_array[i+1]) and (ammo_array[i] >= ammo_array[i-1])):
ammo_array[i+1] += 1
i = i-1
can_can += 1
i += 1
if(i == (soldier_num-1)):
if((rating_array[i] > rating_array[i-1]) and (ammo_array[i] <= ammo_array[i-1])):
ammo_array[i] += 1
can_can += 1
print(can_can)