Predictable Queue
InterviewTime limit1sMemory limit512 MB
Given N task times and M time limits T, print how many tasks are processed first in order before their prefix sum exceeds T.
- Level
Easy3 of 10
- Topics
- Prefix sum, Binary search
- Solved
- No attempts yet
Problem
Hyeokjin likes to take on a lot of work. One day he took on a mountain of work without a plan, and now he has no idea how to get through it. While thinking about the most efficient order to do the work in, he came up with a good method based on what he learned in his data structures class. His method is very simple. He stores the time each task takes, and processes the tasks one at a time in the order they were given.
Smart Hyeokjin already knew that the data structure best suited to his method is the queue. A queue is one of the basic data structures in computing, and it stores data in FIFO (First In First Out) order, where data inserted first comes out first.
Hyeokjin was processing his work nicely with this method when he found a very important flaw in the data structure. If he processes tasks in the given order, then even when an urgent task appears in the middle, he has to finish every task ahead of it first. Since the time he can work is fixed, processing tasks in the given order can lead to situations where he can or cannot get to an urgent task in the middle. Hyeokjin decided that to find out whether he can process a task in the middle, he first needs to be able to predict how many tasks he can process within the time he can work.
Hyeokjin wants to build a new data structure with this feature, the Predictable Queue. Help Hyeokjin find out how many tasks he can process within the time he can work.
Input
The first line gives the integers N, M (1 ≤ N, M ≤ 200,000), where N is the number of tasks Hyeokjin has taken on and M is the number of queries asking how many tasks he can process within the time he can work.
The second line gives N integers t1, t2, ..., tN (1 ≤ ti ≤ 10,000) separated by spaces. ti is the time each task placed into the queue takes, and the leftmost task is the one given first.
Each of the next M lines gives an integer T (1 ≤ T ≤ 2×109), the time Hyeokjin can work.
Output
For each of the M lines, print the number of tasks Hyeokjin can process within the time he can work.