Given an exam of N problems with a time limit of T, your task is to figure out the maximum number of exam items you can complete within the time limit, given the amount of time t; each problem would take for you to solve them and the amount of points p; you can get from solving each respective problem. Some really difficult problems may as well be unsolvable; these problems will have a t; value of —1. If you cannot solve a single problem within the time limit, then you are to say "This exam is impossible!" Input Format The first line of the input contains two space-separated integers N and T, indicating the number of problems in the exam and the exam's time limit (in minutes, rounded to the nearest minutes). N lines follow, each containing a pair of space-separated integers t; and p₁, where t; is the number of minutes, rounded to the nearest minute, needed to complete the ith problem and p; is the number of points the ith problem is worth. Note that if a problem cannot be solved, it's t; value would be -1. Constraints 1 ≤ N ≤ 105 0≤T≤ 10⁹ 0 ≤ ti, Pi ≤ 104 V ti = Output Format = -1 Output consists of either a single integer n, where n is the maximum number of problems that can be solved within the given time limit T or "This exam is impossible!" (without quotes) if no problem can be solved within the given time limit T.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 18SA
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.

Sample Input 0
5 29
3 8
7 9
7 2
6 6
7 4

Sample Output 0
4

Explanation 0
You can finish the first 4 problems, which will take 3+7+7+6=23 seconds. Attempting the fifth problem will result in 23+7=30 seconds, which is greater than T=29.

6 5
6 6
7 10
9 4
9 11
10 10
10 0

Sample Output 1
This exam is impossible!

Explanation 1
None of the problems have t_i <= T = 5, so it is impossible to finish even a single problem.

The actual code

def solve(n,t,problems):
    # compute and return answer here

def main():
    n,t = list(map(int,input().strip().split(' ')))
    problems = [tuple(map(int,input().strip().split(" "))) for i in range(n)]
    print(solve(n,t,problems))

if __name__ == "__main__":
    main()

Given an exam of N problems with a time limit of T, your task is to figure out the maximum number of exam
items you can complete within the time limit, given the amount of time t; each problem would take for you to
solve them and the amount of points p; you can get from solving each respective problem. Some really
difficult problems may as well be unsolvable; these problems will have a t; value of -1.
If you cannot solve a single problem within the time limit, then you are to say "This exam is impossible!"
Input Format
The first line of the input contains two space-separated integers N and T, indicating the number of problems
in the exam and the exam's time limit (in minutes, rounded to the nearest minutes).
N lines follow, each containing a pair of space-separated integers t; and p¿, where t; is the number of
minutes, rounded to the nearest minute, needed to complete the ith problem and p; is the number of points
the ith problem is worth.
Note that if a problem cannot be solved, it's t; value would be −1.
Constraints
1 ≤ N ≤ 105
0≤ T ≤ 10⁹
0 ≤ ti, Pi ≤ 10¹ V t; = −1
Output Format
Output consists of either a single integer n, where ʼn is the maximum number of problems that can be solved
within the given time limit T or "This exam is impossible!" (without quotes) if no problem can be solved within
the given time limit T.
Transcribed Image Text:Given an exam of N problems with a time limit of T, your task is to figure out the maximum number of exam items you can complete within the time limit, given the amount of time t; each problem would take for you to solve them and the amount of points p; you can get from solving each respective problem. Some really difficult problems may as well be unsolvable; these problems will have a t; value of -1. If you cannot solve a single problem within the time limit, then you are to say "This exam is impossible!" Input Format The first line of the input contains two space-separated integers N and T, indicating the number of problems in the exam and the exam's time limit (in minutes, rounded to the nearest minutes). N lines follow, each containing a pair of space-separated integers t; and p¿, where t; is the number of minutes, rounded to the nearest minute, needed to complete the ith problem and p; is the number of points the ith problem is worth. Note that if a problem cannot be solved, it's t; value would be −1. Constraints 1 ≤ N ≤ 105 0≤ T ≤ 10⁹ 0 ≤ ti, Pi ≤ 10¹ V t; = −1 Output Format Output consists of either a single integer n, where ʼn is the maximum number of problems that can be solved within the given time limit T or "This exam is impossible!" (without quotes) if no problem can be solved within the given time limit T.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Computing Algorithms
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
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning