URAL 2064|Caterpillars|堆

9 篇文章 0 订阅

N 只毛毛虫,开始高度为0,第i只每爬 ti cm,休息 ti min,即下滑 ti cm,速度1cm/min。每次询问时刻 t 最高的毛毛虫的高度。

首先很显然ti106时,就不用算了。
然后 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值