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
  • Import java.util.*; public class PairFinder { public static void main(String[] args) { Scanner sc...

    import java.util.*; public class PairFinder { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in the value of k int k = Integer.parseInt(sc.nextLine());    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } }    System.out.println(findPairs(numbers, k)); }    //method that...

  • HHackerRank Spirinkle: Unfulfilled Orders Ms. Sugar has decided to use her spirinkle idea as a re...

    Please find an expert in algorithms to solve this problem HHackerRank Spirinkle: Unfulfilled Orders Ms. Sugar has decided to use her spirinkle idea as a regular option on the specials menu. To support this, she has purchased & spirinkle machines. There are n customers coming in to the store. The th customer (1く讠 n) places their order at time. requesting a spirinkle of size ky. These orders are to be processed on a 'first come first served basis, however not...

  • Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin...

    Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin will be processed as follows and passed to the function In the first line, there is a single integer n. In the second line, there is a single integer m. In the ph of the next n lines there are m space-separated integers denoting the throw of the initial grid. In the next line, there is a single integer k. In the next line,...

  • Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException;...

    Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class SongsReport {    public static void main(String[] args) {               //loading name of file        File file = new File("songs.csv"); //reading data from this file        //scanner to read java file        Scanner reader;        //line to get current line from the file        String line="";       ...

  • Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions...

    Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions you created. Only use one value of r and h for all the function. Ask the user for the r and h in the main() as an input, and h for all the functions. You should put the functions for areas in a separate file. ​ For example, firstDigtec7ze Write a function e digits n) (returning the number of digits in the argument returning...

  • package bigIntegerPackage; import java.util.Scanner; public class BigIntMathTesterOne {    private static Scanner keyboard = new Scanner(System.in);...

    package bigIntegerPackage; import java.util.Scanner; public class BigIntMathTesterOne {    private static Scanner keyboard = new Scanner(System.in);    /* *    * @param args    */    public static void main(String[] args)    { /** * Sample valid input and resulting output *    "44444444445555555555666666666677777777770000000000" * stores ["4","4","4","4","4","4","4","4","4","4","5","5","5","5","5","5","5","5","5","5","6","6","6","6","6","6","6'<'6","6","6","7","7","7","7","7","7","7","7","7","7","0","0","0","0","0","0","0","0","0","0"] in ArrayList and sets the value to positive *returns string "44444444445555555555666666666677777777770000000000" * "100000" stores ["1","0","0","0","0","0"] in ArrayList and sets the value to positive *returns string "100000" *"+0" stores ["0"] in ArrayList and sets...

  • PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything...

    PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything a course uses in a grading scheme, # like a test or an assignment. It has a score, which is assessed by # an instructor, and a maximum value, set by the instructor, and a weight, # which defines how much the item counts towards a final grade. def __init__(self, weight, scored=None, out_of=None): """ Purpose: Initialize the GradeItem object. Preconditions: :param weight: the weight...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deq...

    Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Recommended Steps testDeque.cpp : // C++ implementation of doubly linked list Deque doubly linked list #include <bits/stdc++.h> using namespace std; class Timer { // To replace with the full timer class definition // inside this folder: LearnCpp9_18_timeSortArray.cpp }; // Node of a doubly linked list template<class T> class...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

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
ADVERTISEMENT