N
只毛毛虫,开始高度为0,第
首先很显然
然后
ti<106
对于每个毛毛虫,在
(2n+1)ti
时到达顶点。
然后答案肯定比这个顶点小。
所以时间的变动都可以视为毛毛虫是下滑的。
没了。。
灵异事件?
GCC/Clang:引用cstdio->TLE; stdio.h->AC
VC++:cstdio->AC
求老司机指点迷津?
GCC的cstdio有何神奇之处,没看出来?
VC的cstdio倒是和stdio.h没差别。。
这个故事告诉我们C大法好?
#include <stdio.h>
#include <queue>
const int M = 1e6, N = M + 5;
int ans[N]; bool vis[N];
struct Data {
int t, h;
Data(int a, int b): t(a), h(b) { }
bool operator< (const Data &b) const { return h < b.h; }
};
int main() {
std::priority_queue<Data> pq;
int i, j, n, m, x, t, flag = 0;
scanf("%d", &n);
for (i = 0; i < n; ++i) {
scanf("%d", &t);
if (t >= M) flag = 1;
else vis[t] = 1;
}
if (!flag) {
for (i = M; i; --i) if (vis[i]) for (j = i; ; j += i << 1) {
if (j >= M) {
int tmp = i - (j - M);
if (ans[M] < tmp) pq.push(Data(M, ans[M] = tmp));
break;
}
if (!ans[j])
pq.push(Data(j, ans[j] = i));
}
while (!pq.empty()) {
Data p = pq.top(); pq.pop();
if (ans[p.t] < p.h) continue;
x = p.t - 1;
if (x && ans[x] < ans[p.t] - 1)
pq.push(Data(x, ans[x] = ans[p.t] - 1));
x = p.t + 1;
if (x <= M && ans[x] < ans[p.t] - 1)
pq.push(Data(x, ans[x] = ans[p.t] - 1));
}
}
scanf("%d", &m);
while (m--) {
scanf("%d", &x);
if (flag) printf("%d\n", x);
else printf("%d\n", ans[x]);
}
return 0;
}
Caterpillars
Time Limit: 3000MS
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 ≤ 10 6).
Second line contains n integers ti — characteristics of caterpillars (1 ≤ ti ≤ 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 ≤ xi ≤ 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.
Sample Input
4
1 3 2 1
12
1
2
3
4
5
6
7
8
9
10
11
12
Sample Output
1
2
3
2
1
2
1
2
3
2
1
0