Python Forum
Settle the tourists into their rooms
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Settle the tourists into their rooms
#6
There are several rooms in the same hall on the same side of the hall in the hotel. The distance between adjacent rooms equals 1 meter. You know which rooms are not vacant and which are. New group of tourist arrived and is waiting to check into.You want everyone to check into separate room so that the distance between the tourists' rooms would be as long as possible.
Input:First you enter the number of vacant rooms and the number of tourists
Then the vacant rooms' numbers
Output: One number- the possible maximum distance between tourists' rooms

Example:
Input:
7 4
3 6 8 12 16 21 28
Output:
7

Explanation: We have 7 vaccant rooms with room numbers 3,6,8,12,16,21,28. So the distances between those rooms are 3,2,4,4,5,7 accordingly. The number of tourist is 4 as it's given in the first row of input. For example,first two tourists we check into rooms with numbers 3 and 28. Then we have to check into rooms two last tourists so that the distance between them and first two tourists would be as long as possible. The best decision is to provide the third tourist a 12th room, the fourth tourist the 21st room. All in all, tourist №1 is checked into room number 3, №2 - 12,№3 - 21, №4- 28. The distance between those rooms is 9,9,7. The minimum is 7 and that's the answer

I wrote code only for 4 tourists now. And here it is:
vr, t =map(int,input().split())
l=list(map(int,input().split()))
d=[] #the list with distances
for i in range(len(l)-1):
    d.append(l[i+1]-l[i])
sum1 = d[0]
sum2 = d[1]
sum3 = sum(int(d[m]) for m in range(2, int(len(d))))
i=2
k=2
max = 0
for j in range(1,len(d)-1): #(1)we are increasing the distance between the second and the first room
    while sum3 != d[len(d)-1]:#(2)(we are increasing the distance between the second and the third room)
        sum2+= d[i]
        sum3-=d[i]
        i+=1
        a = min(sum1,sum2,sum3)
        if max < a:
            max=min(sum1,sum2,sum3)
    sum1+=d[j]
    i = k+1
    sum2=d[i-1]
    sum3 = sum(int(d[m]) for m in range(i, int(len(d))))
    k += 1
print(max)
Now it became obvious that as the number of tourists grows so the number of inner loops on the lines which i marked as 1 and 2. As for sums we can put them in the list,I assume. But I don't quite understand yet how to make a connection between the number of tourists and the number of loops(so that the number of loops also would grow)
Reply


Messages In This Thread
InterstingTask - by Katenn - Sep-30-2020, 10:01 PM
RE: InterstingTask - by Gribouillis - Oct-01-2020, 11:47 AM
RE: InterstingTask - by Katenn - Oct-01-2020, 12:03 PM
RE: InterstingTask - by perfringo - Oct-01-2020, 12:11 PM
RE: InterstingTask - by Gribouillis - Oct-01-2020, 02:04 PM
Settle the tourists into their rooms - by Katenn - Oct-04-2020, 07:55 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020