首先对于一个月的预算,如果非常小的话,我们可以留到后面的 \(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. Python3解leetcode Reach a Number

    问题描述: You are standing at position 0 on an infinite number line. There is a goal at position target. ...

  2. php strripos()函数 语法

    php strripos()函数 语法 作用:寻找某字符串中某字符最后出现的位置,不区分大小写.大理石平台 语法:strripos(string,find,start) 参数: 参数 描述 strin ...

  3. [CSP-S模拟测试]:降雷皇(DP+树状数组)

    题目描述 降雷皇哈蒙很喜欢雷电,他想找到神奇的电光.哈蒙有$n$条导线排成一排,每条导线有一个电阻值,神奇的电光只能从一根导线传到电阻比它大的上面,而且必须从左边向右传导,当然导线不必是连续的.哈蒙想 ...

  4. Sultana后记:纯css也能写col,select,datepicker,carousel...

    未完待续 背景 如今css3越来越发达,focus-within等属性也已经开始在Chrome得到支持.如果有出色的css功底,一点点ps技能,你也能用css3配合原生html标签写出优秀的框架.通过 ...

  5. composer proc_open(): fork failed – Cannot allocate memory

    一般小的VPS 才1G内存,如果使用composer会提示内存不足的现象 解决办法,可以使用交换内存 直接命令 /bin/dd if=/dev/zero of=/var/swap.1 bs=1M co ...

  6. leetcode 78. 子集(c++)

    给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3]输出:[ [3],  [1],  [2],  ...

  7. PTA 1121 Damn Single

    题目链接:1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who i ...

  8. springboot异步任务、定时任务

    打开浏览器 http://localhost:8080/hello ,连续刷新可以看到不会 等待 3秒时间了,pom.xml controller service 代码如下. -----------S ...

  9. Scala操作外部数据

    Scala操作外部数据: 1.操作文件 2.操作XML 3.操作MySQL 读取文件: object FileApp { def main(args: Array[String]): Unit = { ...

  10. Struts2之动态方法调用

    1.感叹号 前台页面 <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...