PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
You have N movies to choose from to rent. The i-th movie has a rating of A_i and a cost of B_i.
You will only pick a movie with a rating of at least 7. What’s the minimum cost of renting a movie?
EXPLANATION:
It suffices to directly implement the condition outlined in the statement: only consider movies with a rating (A_i) that’s \geq 7, and take the minimum cost (B_i) among them.
That gives the following algorithm:
- Initialize \text{ans} = \infty
In practice, you can just set it to some number that’s larger than any B_i, like 1001. - Next, loop over i from 1 to N.
- If A_i \lt 7, ignore this movie.
- If A_i \geq 7, set \text{ans} = \min(\text{ans}, B_i)
In the end, print \text{ans}.
If there’s no movie with rating \geq 7, the value of \text{ans} will be what you initialized it to, i.e. a value greater than 1000. In this case, output -1 instead.
TIME COMPLEXITY:
\mathcal{O}(N) per testcase.
CODE:
Editorialist's code (PyPy3)
for _ in range(int(input())):
n = int(input())
ans = 2000
for i in range(n):
a, b = map(int, input().split())
if a >= 7: ans = min(ans, b)
if ans == 2000: ans = -1
print(ans)