首先对于一个月的预算,如果非常小的话,我们可以留到后面的 \(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;
}

CF883J 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest - J. Renovation 贪心+树状数组的更多相关文章

  1. 2018-2019 ICPC, NEERC, Southern Subregional Contest

    目录 2018-2019 ICPC, NEERC, Southern Subregional Contest (Codeforces 1070) A.Find a Number(BFS) C.Clou ...

  2. Codeforces 2018-2019 ICPC, NEERC, Southern Subregional Contest

    2018-2019 ICPC, NEERC, Southern Subregional Contest 闲谈: 被操哥和男神带飞的一场ACM,第一把做了这么多题,荣幸成为7题队,虽然比赛的时候频频出锅 ...

  3. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror) Solution

    从这里开始 题目列表 瞎扯 Problem A Find a Number Problem B Berkomnadzor Problem C Cloud Computing Problem D Gar ...

  4. Codeforces1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)总结

    第一次打ACM比赛,和yyf两个人一起搞事情 感觉被两个学长队暴打的好惨啊 然后我一直做傻子题,yyf一直在切神仙题 然后放一波题解(部分) A. Find a Number LINK 题目大意 给你 ...

  5. 2018.10.20 2018-2019 ICPC,NEERC,Southern Subregional Contest(Online Mirror, ACM-ICPC Rules)

    i207M的“怕不是一个小时就要弃疗的flag”并没有生效,这次居然写到了最后,好评=.= 然而可能是退役前和i207M的最后一场比赛了TAT 不过打得真的好爽啊QAQ 最终结果: 看见那几个罚时没, ...

  6. codeforce1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) 题解

    秉承ACM团队合作的思想懒,这篇blog只有部分题解,剩余的请前往星感大神Star_Feel的blog食用(表示男神汉克斯更懒不屑于写我们分别代写了下...) C. Cloud Computing 扫 ...

  7. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)

    A. Find a Number 找到一个树,可以被d整除,且数字和为s 记忆化搜索 static class S{ int mod,s; String str; public S(int mod, ...

  8. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) Solution

    A. Find a Number Solved By 2017212212083 题意:$找一个最小的n使得n % d == 0 并且 n 的每一位数字加起来之和为s$ 思路: 定义一个二元组$< ...

  9. 【*2000】【2018-2019 ICPC, NEERC, Southern Subregional Contest C 】Cloud Computing

    [链接] 我是链接,点我呀:) [题意] [题解] 我们可以很容易知道区间的每个位置有哪些安排可以用. 显然 我们优先用那些花费的钱比较少的租用cpu方案. 但一个方案可供租用的cpu有限. 我们可以 ...

随机推荐

  1. [USACO2011 Feb]Best Parenthesis

    Time Limit: 10 Sec Memory Limit: 128 MB Description Recently, the cows have been competing with stri ...

  2. 有关于log4j详解

    Log4j记录日志使用方法 一.什么是log4j Log4J是Apache的一个开放源代码的项目.通过使用Log4J,程序员可以控制日志信息输送的目的地,包括控制台,文件,GUI组件和NT事件记录器, ...

  3. 攻防世界 | string

    #encoding=utf-8 #!usr/bin/python from pwn import * io = remote('111.198.29.45',42643) io.recvuntil(& ...

  4. xiugai-去除js注释

    <div class="myLoading"> <div class="svg-wrap"> <svg width="8 ...

  5. Linux 初始化系统(init)- systemd

    wikipedia 浅析 Linux 初始化 init 系统 systemd 中文手册 fedoraproject - systemd 1. Systemd 简介 Systemd 是 Linux 系统 ...

  6. 【FICO系列】SAP FICO模块-固定资产月结的注意点

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP FICO模块-固定资产月 ...

  7. 20190825 On Java8 第十二章 集合

    第十二章 集合 java.util 库提供了一套相当完整的集合类(collection classes)来解决这个问题,其中基本的类型有 List . Set . Queue 和 Map. 不要在新代 ...

  8. oracle函数与存储方法

    oracle中的函数, 可以理解为java中的方法 有参数, 或者没有参数 通过return返回一个值 oracle存储过程跟函数唯一的区别, 存储过程不能通过return返回一个值 参数的类型, i ...

  9. spring容器的启动过程

    spring的启动过程: 首先,对于一个web应用,其部署在web容器中,web容器提供其一个全局的上下文环境,这个上下文就是ServletContext,其为后面的spring IoC容器提供宿主环 ...

  10. CompletionService的好处与使用场景

    转自:https://blog.csdn.net/jdsjlzx/article/details/52912701 FutureTask既是Future.Runnable,又是包装了Callable( ...