Sunset Street is a very shiny street in a tiny town called Sunset Town. Everyday when the sun
sets, the whole street is soaked in the glow and houses form a beautiful skyline. These days there
will be a music festival held in Sunset Town. In order to decorate the main street, Sunset Street,
the organizer decides to put some posters up the wall on the houses.
But the things have been more complicated now. Due to the shortage of funds, the organizer
can only afford to produce one size for the posters. So the organizer’s first problem is to estimate
the best poster size. Consider the street’s skyline, as shown in the figure below.
Note that all the posters are rectangle-shaped, and they cannot be overlapped. Every part of
a poster should be stick up on the wall tightly.
There will be one or more test cases in the input file.
For each test case, the first line contains two numbers n (1 ≤ n ≤ 100, 000) and q (0 ≤ q ≤
10, 000), indicating the length of the street and the number of questions respectively. The next
n non-negative integers, separated by one or more space or newline characters, are the heights of
the wall, from left to right. Then the next q lines are questions. Each line contains two positive
integers W,H (1 ≤ W ≤ n, 1 ≤ H < 231
) indicating the width and the height of the poster size.
All the numbers will not exceed 231
− 1.
n = q = 0 indicates the end of the input, as shown in the sample input. Don’t process it.
For each question output a line contains a number k, the maximum number of the posters can be
put up onto the wall.
Print a blank line between consecutive test cases.
15 3 3 3 5 5 2 2 6 6 4 4 5 5 5 1 1 3 4 1 1 5 5 2 1 5 7 2 1 0 0
2 15 0 1
Migrated from old NTUJ.
PTC
No. | Testdata Range | Score |
---|