您的位置:首页 > 其它

G - Caterpillars URAL - 2064 ----思维题

2017-04-28 20:21 357 查看


G - Caterpillars

 URAL
- 2064 

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 t i cm i-th caterpillar needs to rest for t i 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 ≤ 10 6).

Second line contains n integers t i — characteristics of caterpillars (1 ≤ t i ≤ 10 9).

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

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 ≤ x i ≤ 10 6).

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.

Example
inputoutput
4
1 3 2 1
12
1
2
3
4
5
6
7
8
9
10
11
12

1
2
3
2
1
2
1
2
3
2
1
0

题目链接:https://cn.vjudge.net/contest/160782#problem/G

题目的意思是每个虫子向上爬ti厘米后休息ti秒,上升和下降的速度都是1厘米每分钟,问给你一个时刻,虫子最高爬到什么地方

直接看代码吧,我根据虫子的周期性来计算,如果一个虫子在i分钟爬到a[i],那么i+1和i-1分钟都是a[i]-1;

记得这个题用cin输入,关闭流输入,scanf会超时

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1000002;
int a[maxn];
int ans[maxn];
int main(){
int n,h,flag=0;
ios::sync_with_stdio(false);
cin>>n;
for(int i=0;i<n;i++){
cin>>h;
if(flag){
continue;
}
if(h>=1000000){
flag=1;
continue;
}
if(a[h]<h){
int j;
for(j=h;j<maxn;j+=2*h){
a[j]=a[j]>h?a[j]:h;
}
a[maxn-1]=a[maxn-1]>h-(j-(maxn-1))?a[maxn-1]:h-(j-(maxn-1));
}
}
if(flag==0){
int cnt=-inf;
for(int i=1;i<maxn;i++){
cnt=(a[i]+i)>cnt?(a[i]+i):cnt;
ans[i]=ans[i]>(cnt-i)?ans[i]:(cnt-i);
}
cnt=-inf;
for(int i=maxn-1;i>0;i--){
cnt=cnt>(a[i]-i)?cnt:(a[i]-i);
ans[i]=ans[i]>(cnt+i)?ans[i]:(cnt+i);
}
}
int q;
cin>>q;
while(q--){
int x;
cin>>x;
if(flag){
cout<<x<<endl;
}
else{
cout<<ans[x]<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: