• CF883J 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest


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

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

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

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

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

    时间复杂度 (O(mlog 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
    ", 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
    ", 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
    ", 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;
    }
    
  • 相关阅读:
    Vsphere日记01.ESXi5.5.install
    Linux快速计算MD5和Sha1命令
    在linux虚机中装vmtools
    BT下载原理分析
    虚拟防火墙实验
    简单介绍一下vue2.0
    小白编程系列(一):编程语言选择
    html几种美丽的分割线
    利用:header匹配所有标题做目录
    markdown编辑器的小建议
  • 原文地址:https://www.cnblogs.com/hankeke/p/CF883J.html
Copyright © 2020-2023  润新知