国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Ural 2064 Caterpillars

2019-11-08 03:17:31
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Description

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 ti cm i-th caterpillar needs to rest for ti minutes. During that time it slides down a bit. Crawling speed of a caterpillar is 1 cm/minute, sliding speed — also 1 cm/minute.

Kirill is very much interested to find out how high on the tree is the leading caterpillar at different moments in time.

Input

First line contains one integer n — the number of caterpillars (1 ≤ n ≤ 106).

Second line contains n integers ti — characteristics of caterpillars (1≤ti≤109).

In the third line there is a number q — number of moments in time, which Kirill finds interesting (1≤q≤106).

Remaining q lines contain one query from Kirill each. A query is described by xi — number of minutes since the start of the competition (1≤xi≤106).

Output

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ù) ti 。對(duì)于每只毛毛蟲,它們均從 0 時(shí)刻以高度 0 向上攀爬,以 1 cm/min 的速度上爬 ti 分鐘,再以 1cm/min 的速度下滑 ti 分鐘,再以 1 cm/min 的速度上爬 ti 分鐘,如此往復(fù)。

之后有 q 個(gè)詢問(wèn),每個(gè)詢問(wèn)輸入一個(gè) x ,問(wèn)對(duì)每個(gè)詢問(wèn)給出 n 只毛毛蟲在 x 時(shí)刻最高的那只毛毛蟲的高度。

分析

此題可進(jìn)行暴力的預(yù)處理。

由于詢問(wèn)的 x 最大為 106 ,因此只需預(yù)處理出 [1,106] 區(qū)間每個(gè)時(shí)刻的最高高度。

首先可以確定對(duì)于毛毛蟲 iti,3ti,5ti,... 時(shí)刻它必定處在最高點(diǎn),因此暴力處理每只毛毛蟲在 ti,3ti,5ti,... 時(shí)刻的高度計(jì)為 ti ,始終求最大值保存在 ans[ x ] 。

對(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ì)于106 時(shí)刻這個(gè)截止點(diǎn),必須額外處理,否則可能在邊界出錯(cuò)。

此處不得不提及神奇的 ios::sync_with_stdio(0); 否則會(huì)被卡時(shí)間。

代碼

#include<bits/stdc++.h>using namespace std;const int N = 1e6;int n, q, t[N+10], ans[N+10];void deal(){ for(int i=1, j;i<=n;++i) { if(t[i] == t[i-1]) continue; for(j=t[i];j<=N;j+=2*t[i]) if(t[i] > ans[j]) ans[j] = t[i]; ans[N] = max(ans[N], t[i] - (j-N)); } for(int i=1;i<=N;++i) ans[i] = max(ans[i],ans[i-1]-1); for(int i=N;i;--i) ans[i] = max(ans[i],ans[i+1]-1);}int main(){ ios::sync_with_stdio(0); cin>>n; for(int i=1;i<=n;i++) cin>>t[i]; sort(t+1, t+n+1); deal(); cin>>q; for(int i=1,x;i<=q;i++) { cin>>x; cout<<ans[x]<<endl; }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沭阳县| 泗洪县| 科技| 原阳县| 会泽县| 新绛县| 武乡县| 太湖县| 平昌县| 榕江县| 泸州市| 荆州市| 富锦市| 邯郸市| 万宁市| 外汇| 永济市| 来安县| 梓潼县| 梁河县| 麟游县| 正宁县| 巴彦淖尔市| 鄂托克前旗| 通城县| 雷山县| 高要市| 象州县| 社旗县| 博爱县| 马边| 原阳县| 亚东县| 公安县| 美姑县| 右玉县| 靖边县| 永兴县| 错那县| 江西省| 陕西省|