CF883J 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest - J. Renovation 贪心+树状数组

首先对于一个月的预算,如果非常小的话,我们可以留到后面的 \(a_i\) 最大的月来用,因为 \(a_i\) 越大能够拆建筑的越多。

于是我们把 \(a_i\) 合并给 \(i\) 后面的 \(a\) 值最大的位置,就可以把 \(a\) 合并成了一个下降序列。

然后我们根据经典的贪心的策略,将所有建筑按照 \(p_i\) 排序,从小到大来考虑拆不拆。

\(a\) 序列中找到最小的大于等于 \(b_i\) 的位置,计算其前缀的预算和。如果这个预算和大于等于 \(p_i\) 就意味着 \(i\) 是可以拆的。使用预算的时候,优先使用当前位置的预算,不够则从后向前考虑之前的位置的预算。

但是如果直接枚举每一个位置的预算的话,可以被卡成 \(O(nm)\)。容易发现,如果一个位置的预算被用完了,我们就再也不会使用这个位置了。所以向前枚举的时候,可以使用并查集将预算被用完的位置跳过去。

时间复杂度 \(O(m\log n)\)

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I>
inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

const int N = 1e5 + 7;

int n, m;
int a[N], q[N], fa[N];
ll c[N], s[N];

struct Bld {
    int b, p;
    inline bool operator < (const Bld &a) { return p < a.p; }
} d[N];

namespace BIT {
    #define lowbit(x) ((x) & -(x))
    ll s[N];
    inline void qadd(int x, ll k) {
        for (; x <= n; x += lowbit(x))
            s[x] += k;
    }
    inline ll qsum(int x) {
        ll ans = 0;
        for (; x; x -= lowbit(x)) ans += s[x];
        return ans;
    }
}
using BIT::qadd;
using BIT::qsum;

inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

inline void work() {
    int tp = 0, ans = 0;
    for (int i = 1; i <= n; ++i) {
        while (tp && a[i] >= a[q[tp]]) --tp;
        q[++tp] = i;
    }
    for (int i = 1; i <= tp; ++i) s[i] = s[q[i]], a[i] = a[q[i]], c[i] = s[i] - s[i - 1], fa[i] = i;
    n = tp;
    for (int i = 1; i <= n; ++i) qadd(i, c[i]);//, dbg("i = %d, a[i] = %d, c[i] = %I64d\n", i, a[i], c[i]);
    
    std::sort(d + 1, d + m + 1);
    for (int i = 1; i <= m; ++i) {
        int pos = std::upper_bound(a + 1, a + n + 1, d[i].b, std::greater<int>()) - a - 1;
//      dbg("i = %d, p = %d, b = %d, pos = %d, qsum(pos) = %I64d\n", i, d[i].p, d[i].b, pos, qsum(pos));
        if (qsum(pos) < d[i].p) continue;
        int val = d[i].p;
        ++ans;
        for (; pos && val; pos = find(pos)) {
            if (c[pos] > val) qadd(pos, -val), c[pos] -= val, val = 0;
            else qadd(pos, -c[pos]), val -= c[pos], c[pos] = 0, fa[pos] = find(pos - 1);
        }
    }
    printf("%d\n", ans);
}

inline void init() {
    read(n), read(m);
    for (int i = 1; i <= n; ++i) read(a[i]), s[i] = s[i - 1] + a[i];
    for (int i = 1; i <= m; ++i) read(d[i].b);
    for (int i = 1; i <= m; ++i) read(d[i].p);
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    work();
    fclose(stdin), fclose(stdout);
    return 0;
}

转载于:https://www.cnblogs.com/hankeke/p/CF883J.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值