Information is present in the screenshot and below. Based on that need help in solving the code for this problem in python. The time complexity has to be as less as possible (nlogn or n at best, no n^2). Apply greedy algorithm in the problem. Make sure both test cases return correct answers. Output Format The output consists of one line, which is the maximal number of citizens hiding in one hiding place given the optimal division of the citizens as described in the problem statement. The optimal division is the division where the maximal citizens hiding in one hiding spot are minimized. Sample Input 0 3 1 10 Sample Output 0 4 Explanation 0 They can split as follows 4 citizens from town 1 3 citizens from town 1 3 citizens from town 1 The maximum number of citizens in one hiding place is 4. Sample Input 1 6 2 12 5 Sample Output 1 3 Explanation 1 They can split as follows 3 citizens from town 1 3 citizens from town 1 3 citizens from town 1 3 citizens from town 1 3 citizens from town 2 2 citizens from town 2 The maximum number of citizens in one hiding place is 3. Sample Input 2 10 5 2 3 1 1 4 Sample Output 2 2 Explanation 2 They can split as follows: 2 citizens from town 1 1 citizen from town 2 1 citizen from town 2 1 citizen from town 2 1 citizen from town 3 1 citizen from town 4 1 citizen from town 5 1 citizen from town 5 1 citizen from town 5 1 citizen from town 5 The maximum number of citizens in one hiding place is 2. The actual code p,n = list(map(int,input().rstrip().split(" "))) towns = [input().rstrip() for i in range(n)] # compute for answer

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Information is present in the screenshot and below. Based on that need help in solving the code for this problem in python. The time complexity has to be as less as possible (nlogn or n at best, no n^2). Apply greedy algorithm in the problem. Make sure both test cases return correct answers.

Output Format
The output consists of one line, which is the maximal number of citizens hiding in one hiding place given the optimal division of the citizens as described in the problem statement. The optimal division is the division where the maximal citizens hiding in one hiding spot are minimized.

Sample Input 0
3 1
10

Sample Output 0
4

Explanation 0
They can split as follows

  • 4 citizens from town 1
  • 3 citizens from town 1
  • 3 citizens from town 1

The maximum number of citizens in one hiding place is 4.

Sample Input 1
6 2
12
5

Sample Output 1
3

Explanation 1

They can split as follows

  • 3 citizens from town 1
  • 3 citizens from town 1
  • 3 citizens from town 1
  • 3 citizens from town 1
  • 3 citizens from town 2
  • 2 citizens from town 2

The maximum number of citizens in one hiding place is 3.

Sample Input 2
10 5
2
3
1
1
4

Sample Output 2
2

Explanation 2

They can split as follows:

  • 2 citizens from town 1
  • 1 citizen from town 2
  • 1 citizen from town 2
  • 1 citizen from town 2
  • 1 citizen from town 3
  • 1 citizen from town 4
  • 1 citizen from town 5
  • 1 citizen from town 5
  • 1 citizen from town 5
  • 1 citizen from town 5

The maximum number of citizens in one hiding place is 2.

The actual code

p,n = list(map(int,input().rstrip().split(" ")))
towns = [input().rstrip() for i in range(n)]
# compute for answer
Today is the 1055th birthday of Thor, God of Thunder (also, for posterity, today is Thorsday in Asgard). All the
citizens of Asgard have been invited to assist in surprising Thor when he enters the Halls of Valhalla.
Unfortunately, there are a limited number of hiding places in the throne room of Valhalla: N to be exact. Also,
the citizens of Asgard are quite picky with regard to the people they hide with.
Initially, Asgard is divided into M towns and each town has claimed exactly one hiding place in the throne
room. This means that, initially, M distinct hiding places in the throne room are occupied, one for each town.
To maximize the space, people from one hiding spot can divide into two groups and one of the groups can
choose another vacant hiding spot if available, occupying one additional hiding place. This means the citizens
from the M towns will always be hiding with people from the same town, because the towns don't necessarily
get along and don't like to mix with each other.
Since it will be easier for Thor to see the Asgardians if there are more people in one hiding spot, they must
divide themselves such that the maximum number of people in one hiding spot is minimized.
Input Format
Input consists of a single test case. The first line contains two space-separated integers N and M denoting
the number of hiding places in the throne room of Valhalla and the number of towns respectively.
M lines follow, the ith of which contains a single integer P, denoting the number of people in the ith town.
(The tranquiline halls of Valhalla have very large hiding places, so P₁ can easily reach to high values and the
hiding places can still accommodate the citizens.)
Constraints
1≤M≤N≤ 105
1 ≤ P₁ ≤ 10⁹
Transcribed Image Text:Today is the 1055th birthday of Thor, God of Thunder (also, for posterity, today is Thorsday in Asgard). All the citizens of Asgard have been invited to assist in surprising Thor when he enters the Halls of Valhalla. Unfortunately, there are a limited number of hiding places in the throne room of Valhalla: N to be exact. Also, the citizens of Asgard are quite picky with regard to the people they hide with. Initially, Asgard is divided into M towns and each town has claimed exactly one hiding place in the throne room. This means that, initially, M distinct hiding places in the throne room are occupied, one for each town. To maximize the space, people from one hiding spot can divide into two groups and one of the groups can choose another vacant hiding spot if available, occupying one additional hiding place. This means the citizens from the M towns will always be hiding with people from the same town, because the towns don't necessarily get along and don't like to mix with each other. Since it will be easier for Thor to see the Asgardians if there are more people in one hiding spot, they must divide themselves such that the maximum number of people in one hiding spot is minimized. Input Format Input consists of a single test case. The first line contains two space-separated integers N and M denoting the number of hiding places in the throne room of Valhalla and the number of towns respectively. M lines follow, the ith of which contains a single integer P, denoting the number of people in the ith town. (The tranquiline halls of Valhalla have very large hiding places, so P₁ can easily reach to high values and the hiding places can still accommodate the citizens.) Constraints 1≤M≤N≤ 105 1 ≤ P₁ ≤ 10⁹
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Eight puzzle problem
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education