For each query, you are given a non-empty range of indices, Richie budget A and the price per ticket K. Richie keeps watching the longest film he hasn't seen yet, spending K pesos per ticket, until his A pesos are insufficient to buy another ticket. For each query, please print the runtime of the last film Richie is able to see. You can assume that the Monkey's Paw made it so that all the movies showing have not been watched by Richie before. Input Format

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.

 

Output Format

For each query, output one line containing the length of the last movie Richie watches, without the credits, given the strategy described in the problem statement. If Richie can't watch any movie, output -1.

Sample Input 0

8
148 116
157 100
169 15
188 98
91 68
165 70
145 2
11 6
3
2 6 52 12
2 6 13 7
0 4 2 3

Sample Output 0

90
154
-1
 
The actual code

n = int(input())
movies = []
for i in range(n):
    r,c = list(map(int,input().rstrip().split(" ")))
    movies.append([r,c])

q = int(input())
for cc in range(q):
    s,e,a,k = list(map(int,input().rstrip().split(" ")))
    # solve for answer here

Richie likes to watch movies at the local cineplex. Pretty much all his savings go to movies. His only complaint
with the universe right now seems to be that there simply aren't enough hours in a day watch, like, 107
movies in one day for example. That all changed the day Richie found the Monkey's Paw.
Richie was sitting in the park when the Monkey's Paw spoke in a low voice.
"You have three wishes."
He looked around and saw it on the ground beside him. Perplexed he picked it up.
"What did you say?" Richie asked.
"Three wishes," The Paw replied.
"I wish for more wishes," Richie said, obviously.
"I see you are not pure of heart," The Paw replied angrily. "Any mortal who is greedy enough to wish for more
wishes wastes one wish. You now have two..."
"Okay, okay... ummm... I wish the day was long enough to show a lot of movies..."
"Hmmm... define 'a lot'."
"Like N movies maybe?"
"Done!" The Paw said, and Richie felt tomorrow was going to be a long day. "Tomorrow will last as long as N
movies. You have one wish left."
"I want to be able to stay awake to watch all of them!"
"Done. Our contract is sealed. Enjoy tomorrow."
And at that, the Monkey's Paw crumbled to dust. It was getting late so Richie went home to get rested for
tomorrow's Ultimate Movie Marathon.
The next day, Richie went to the ticket counter at the cineplex. There were a lot of movies showing that day.
Richie went to the counter and said "I'd like a ticket for all the movies please."
"That will be K pesos per ticket." "Crap," Richie thought. He didn't wish for enough money. In fact, he only had
A pesos. "Okay... okay... let's improvise."
"All the movies have the same ticket price?" Richie asked. "Yes, sir!" "I'll buy the tickets for the longest movie
then!" "I can't find that for you sir. You have to tell me the title." "You don't have a database?" "Nope. Here's a
list."
She hands him a list. To save time, Richie rips out a range of movies then picks the longest movie in that
range. Richie proceeds to do this until he's out of money, only picking movies from the range he ripped out to
save time.
Basically, given N films each with their runtimes, you must answer queries.
Transcribed Image Text:Richie likes to watch movies at the local cineplex. Pretty much all his savings go to movies. His only complaint with the universe right now seems to be that there simply aren't enough hours in a day watch, like, 107 movies in one day for example. That all changed the day Richie found the Monkey's Paw. Richie was sitting in the park when the Monkey's Paw spoke in a low voice. "You have three wishes." He looked around and saw it on the ground beside him. Perplexed he picked it up. "What did you say?" Richie asked. "Three wishes," The Paw replied. "I wish for more wishes," Richie said, obviously. "I see you are not pure of heart," The Paw replied angrily. "Any mortal who is greedy enough to wish for more wishes wastes one wish. You now have two..." "Okay, okay... ummm... I wish the day was long enough to show a lot of movies..." "Hmmm... define 'a lot'." "Like N movies maybe?" "Done!" The Paw said, and Richie felt tomorrow was going to be a long day. "Tomorrow will last as long as N movies. You have one wish left." "I want to be able to stay awake to watch all of them!" "Done. Our contract is sealed. Enjoy tomorrow." And at that, the Monkey's Paw crumbled to dust. It was getting late so Richie went home to get rested for tomorrow's Ultimate Movie Marathon. The next day, Richie went to the ticket counter at the cineplex. There were a lot of movies showing that day. Richie went to the counter and said "I'd like a ticket for all the movies please." "That will be K pesos per ticket." "Crap," Richie thought. He didn't wish for enough money. In fact, he only had A pesos. "Okay... okay... let's improvise." "All the movies have the same ticket price?" Richie asked. "Yes, sir!" "I'll buy the tickets for the longest movie then!" "I can't find that for you sir. You have to tell me the title." "You don't have a database?" "Nope. Here's a list." She hands him a list. To save time, Richie rips out a range of movies then picks the longest movie in that range. Richie proceeds to do this until he's out of money, only picking movies from the range he ripped out to save time. Basically, given N films each with their runtimes, you must answer queries.
For each query, you are given a non-empty range of indices, Richie budget A and the price per ticket K. Richie
keeps watching the longest film he hasn't seen yet, spending K pesos per ticket, until his A pesos are
insufficient to buy another ticket. For each query, please print the runtime of the last film Richie is able to see.
You can assume that the Monkey's Paw made it so that all the movies showing have not been watched by
Richie before.
Input Format
Input starts with a line containing a single integer N denoting the number of movies showing in the theater.
N lines follow, the ith of which contains two space-separated integers R and C. R is the runtime of the
film. C'; is the length of the credits. Richie always leaves before the credits, so you can subtract the credits
from the runtime when computing for the answer.
This is followed by a line containing a single integer Q denoting the number of queries.
Qlines follow, each containing 4 space-separated integers S. E. A. and K denoting the start index, end
index, Richie's budget, and the cost of a single ticket respectively. Indices are 0-based.
Constraints
1≤N≤ 106
1 ≤ C; <R ≤ 10⁹
1≤Q ≤ 4000
0≤S≤E<N
0≤E-S<104
1 ≤A, K≤ 10⁹
Output Format
For each query, output one line containing the length of the last movie Richie watches, without the credits,
given the strategy described in the problem statement. If Richie can't watch any movie, output -1.
Transcribed Image Text:For each query, you are given a non-empty range of indices, Richie budget A and the price per ticket K. Richie keeps watching the longest film he hasn't seen yet, spending K pesos per ticket, until his A pesos are insufficient to buy another ticket. For each query, please print the runtime of the last film Richie is able to see. You can assume that the Monkey's Paw made it so that all the movies showing have not been watched by Richie before. Input Format Input starts with a line containing a single integer N denoting the number of movies showing in the theater. N lines follow, the ith of which contains two space-separated integers R and C. R is the runtime of the film. C'; is the length of the credits. Richie always leaves before the credits, so you can subtract the credits from the runtime when computing for the answer. This is followed by a line containing a single integer Q denoting the number of queries. Qlines follow, each containing 4 space-separated integers S. E. A. and K denoting the start index, end index, Richie's budget, and the cost of a single ticket respectively. Indices are 0-based. Constraints 1≤N≤ 106 1 ≤ C; <R ≤ 10⁹ 1≤Q ≤ 4000 0≤S≤E<N 0≤E-S<104 1 ≤A, K≤ 10⁹ Output Format For each query, output one line containing the length of the last movie Richie watches, without the credits, given the strategy described in the problem statement. If Richie can't watch any movie, output -1.
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Intermediate SQL concepts
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
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