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 個(gè)數(shù)
之后有 q 個(gè)詢問(wèn),每個(gè)詢問(wèn)輸入一個(gè) x ,問(wèn)對(duì)每個(gè)詢問(wèn)給出 n 只毛毛蟲在 x 時(shí)刻最高的那只毛毛蟲的高度。
此題可進(jìn)行暴力的預(yù)處理。
由于詢問(wèn)的 x 最大為
首先可以確定對(duì)于毛毛蟲 i ,
對(duì)于其余時(shí)刻,可視作最高點(diǎn)向兩邊的擴(kuò)展,作二維坐標(biāo)即斜率為 1 或 -1 。正反向分別掃描,ans[ x ] = max(ans[x], ans[x-1] -1) , ans[ x ] = max(ans[x], ans[x+1] - 1)。
需要注意的是,對(duì)于
此處不得不提及神奇的 ios::sync_with_stdio(0); 否則會(huì)被卡時(shí)間。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注