您的位置:首页 > 其它

ural 2064. Caterpillars

2016-03-04 10:23 513 查看

2064. Caterpillars

Time limit: 3.0 second
Memory limit: 64 MB

Young gardener didn’t visit his garden for a long time, and now it’s not very pleasant there: ncaterpillars 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.

Sample

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

Problem Author: Nikita Sivukhin (prepared by Alexey Danilyuk, Nikita Sivukhin)
Problem Source: Ural Regional School Programming Contest 2015

Tags: none (hide tags for unsolved problems)
Difficulty: 559

题意:有n只蜗牛,对于第i只蜗牛,有性质ti,这只蜗牛往上爬ti秒,有下降ti秒,上升下降速度都是1cm/s,问第x秒爬得最高的蜗牛多高。
分析:首先画出蜗牛们的高度的函数,就可以看到它们是周期的类似三角形的函数。
如果我们只关注最高点,那么第x秒的答案是
max{a[i] - abs(x - i)}
其中a[i]表示在第i秒这个点的最高点最大是多少。
把那个abs拆开,分成x<i,x>i讨论
发现当x>i时
a[i]+i - x
当x<i时
a[i]-i + x
所以用是不是有点像单调队列?
其实我们只用记录最大值即可。

/**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mk make_pair

inline int getInt()
{
int ret = 0;
char ch = ' ';
bool flag = 0;
while(!(ch >= '0' && ch <= '9'))
{
if(ch == '-') flag ^= 1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
ret = ret * 10 + ch - '0';
ch = getchar();
}
return flag ? -ret : ret;
}

const int N = 1000010;
int n, m, cnt
;
int ans
;

inline void input()
{
for(cin >> n; n--; )
{
int x, t;
cin >> x;
for(t = x; t < N; t += 2 * x)
cnt[t] = max(cnt[t], x);
cnt[N - 1] = max(cnt[N - 1], x - (t - (N - 1)));
}
cin >> m;
}

inline void solve()
{
for(int i = 1, now = -INF; i < N; i++)
{
now = max(now, cnt[i] + i);
ans[i] = max(ans[i], now - i);
}
for(int i = N - 1, now = -INF; i > 0; i--)
{
now = max(now, cnt[i] - i);
ans[i] = max(ans[i], now + i);
}

while(m--)
{
int x;
cin >> x;
cout << ans[x] << "\n";
}
}

int main()
{
ios::sync_with_stdio(0);
input();
solve();
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: