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. Sample Input 3 4 4 12 10 5 1 Sample Output 3 12 Sample Input 4 10 5 2 3 1 100 4 Sample Output 4 17 The actual code p,n = list(map(int,input().rstrip().split(" "))) towns = [input().rstrip() for i in range(n)] # compute for answer
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
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.
Sample Input 3
4 4
12
10
5
1
Sample Output 3
12
Sample Input 4
10 5
2
3
1
100
4
Sample Output 4
17
The actual code
p,n = list(map(int,input().rstrip().split(" "))) towns = [input().rstrip() for i in range(n)] # compute for answer |
Step by step
Solved in 3 steps