Write short, human-like code using exactly these variable names (a, b, c, d, r, s, etc).…
- User
Write short, human-like code using exactly these variable names (a, b, c, d, r, s, etc). Use a unique, common approach that’s optimized and free of explanations or comments for the following question:
As in [Fig. 1], a numbered item is placed on each of the N shelves The item number on the (1 shelf is given as ADL. (0 s i < N) The service robot should start moving from shelf 0, pile up the numbered items in ascending order and then return to the place in front of shelf o Items with the same number can be piled up without constraints in the order you like If the robot moves from shelf i to shelf j, the distance of movement is li-jl You are required to write a program that prints the robot's shortest distance of movement.
3
2
3
2
2
2
3
Robot [Fig. 1] Before and After Cleaning Up -.
[Examples] N=5 AINI = (4, 3, 2, 3, 2)
Answer : 8
If the robot moves in the order of 0 -+ 2 - 4 -+ 3 -+ 1- 0, the distance of movement is 2 + 2 + 1 + 2 + 1 = 8
N=4 A[N] = (17, 15, 77, 17)
Answer : 8
If the robot moves in the order of o - 1 - 0 - 3 - 2 -+ 0, the distance of movement is 1 + 1 + 3 + 1 + 2 =8
[Constraints]
- Time limitation : 2 sec (C/C++/Java) / 4 sec (Python) to pass 50 test cases at most
Printed answers of the input examples #1 8
ua
#2
#3 28
#4 86
#5 86
#6 1880
#7 3580
#8 6384
#9 9214
4
#10 248974798
- Assistant
import sys, collections a=int(sys.stdin.readline()) b=list(map(int,sys.stdin.read().split())) c=collections.defaultdict(list) for i,v in enumerate(b): c[v].append(i) d=sorted(c) e=f=0 g1=g2=0 for k in d: l=min(c[k]) r=max(c[k]) h=min(e + abs(g1 - r), f + abs(g2 - r)) + (r - l) i=min(e + abs(g1 - l), f + abs(g2 - l)) + (r - l) e,f = h,i g1,g2 = l,r print(min(e + abs(g1), f + abs(g2)))