Young gardener didn’t visit his garden for a long time, and now it’s not very pleasant there: n caterpillars have appeared on the ground.
Kirill decided to use this opportunity to have some fun and organized a competition — “caterpillar crawl-race.”
At Kirill’s command all caterpillars start crawling from the ground to the top of a tree. But they get tired PRetty fast. After crawling
Kirill is very much interested to find out how high on the tree is the leading caterpillar at different moments in time.
First line contains one integer n — the number of caterpillars (1 ≤ n ≤ 106).
Second line contains n integers
In the third line there is a number q — number of moments in time, which Kirill finds interesting (
Remaining q lines contain one query from Kirill each. A query is described by
For every query print in a separate line one integer, that describes how high is the highest caterpillar at the given moment of time.
给定 n 只毛毛虫,及 n 个数
之后有 q 个询问,每个询问输入一个 x ,问对每个询问给出 n 只毛毛虫在 x 时刻最高的那只毛毛虫的高度。
此题可进行暴力的预处理。
由于询问的 x 最大为
首先可以确定对于毛毛虫 i ,
对于其余时刻,可视作最高点向两边的扩展,作二维坐标即斜率为 1
或 -1
。正反向分别扫描,ans[ x ] = max(ans[x], ans[x-1] -1) , ans[ x ] = max(ans[x], ans[x+1] - 1)。
需要注意的是,对于
此处不得不提及神奇的 ios::sync_with_stdio(0);
否则会被卡时间。
新闻热点
疑难解答