Question

[for Python 3 class; must use stdin and stdout for IO; for libraries, must import by...

[for Python 3 class; must use stdin and stdout for IO; for libraries, must import by "from x import y"]

The Task

Feeling sorry for all the mischief he has caused recently, Hugh Manatee has agreed to help Professor Smith stock pile bundles of seagrass at one of several docks along the Indian River Lagoon. There are N (1 <= N <= 1,000,000, N) docks conveniently numbered 1..N, initially all of them have no bundles. Professor Smith then gives Hugh a sequence of K instructions (1 <= K <= 25,000), each of the form "A B", meaning that Hugh should add one new seagrass bundle to each dock in the range A to B (inclusive). For example, if Hugh is told "10 13", then he should add a bundle of seagrass to each of the docks 10, 11, 12, and 13. After Hugh finishes, Professor Smith would like to how evenly distributed the bundles are. Please help Hugh determine the answer to Professor Smith's question.

Sample Input

On the first line are two, space-separated integers, N and K. Each of the next K lines contain one of Professor Smith's instructions in the form of two, space-separated integers A B where (1<= A <= B <= N )

7 4
5 5
2 4
4 6
3 5

In this sample input there are N=7 docks, and Professor Smith issues K=4 instructions. The first instruction is to add a bundle of seagrass to dock 5, The second is to add bundles of seagrass to docks 2 through 4, and so on.

Sample Output

The output is one number, the difference between the largest number of bundles and the smallest number of bundles.

3

After Hugh is finished, the docks have 0,1,2,3,3,1,0 bundles. In the sorted order 0,0,1,1,2,3,3 we see the difference betweeen the most and least is three bundles.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If the input is given by the user

n,k = input().split(' ')
n = int(n)
k = int(k)
l = [0]*n
for i in range(0,k):
r1,r2 = input().split(' ')
r1 = int(r1)
r2 = int(r2)
for j in range(r1,r2+1):
l[j]+=1

l = sorted(l)
diff = l[-1] - l[0]
print(diff)

If the input is given via a file, and replace temp.txt with the filename

with open('temp.txt','r') as f:
s = f.read().split('\n')
n,k = s[0].split(' ')
n = int(n)
k = int(k)
l = [0]*n
for i in range(1,k+1):
r1,r2 = s[i].split(' ')
r1 = int(r1)
r2 = int(r2)
print(r1,r2)
for j in range(r1,r2+1):
l[j]+=1

l = sorted(l)
print(l)
diff = l[-1] - l[0]
print(diff)

Add a comment
Know the answer?
Add Answer to:
[for Python 3 class; must use stdin and stdout for IO; for libraries, must import by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT