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 to the problem. Make sure both test cases return correct answers. 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. 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. Sample Input 1 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__":

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
80%

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 to the problem. Make sure both test cases return correct answers.

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.

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.

Sample Input 1
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()

Exams can be fairly challenging, especially exams with lots of problems! Coupled with a strict time limit, it can
be fairly intimidating and stressful to even think about solving said problems. A lot of time and effort is spent
deciding which problem to tackle first. Easy problems take a lot less time to solve than average problems, but
are often worth less points. Difficult problems take the most time, but are most of the time worth the most
points. It's almost like you are playing a game when you strategize for an exam; would you go for the easier
challenges but are worth less or go for the mother lodes you aren't even sure you can solve.
Ah, quite the dilemma indeed.
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 pi, 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 t₁ = −1
Transcribed Image Text:Exams can be fairly challenging, especially exams with lots of problems! Coupled with a strict time limit, it can be fairly intimidating and stressful to even think about solving said problems. A lot of time and effort is spent deciding which problem to tackle first. Easy problems take a lot less time to solve than average problems, but are often worth less points. Difficult problems take the most time, but are most of the time worth the most points. It's almost like you are playing a game when you strategize for an exam; would you go for the easier challenges but are worth less or go for the mother lodes you aren't even sure you can solve. Ah, quite the dilemma indeed. 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 pi, 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 t₁ = −1
Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY