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

    0.打开Hyper-V. 安装成功后,查看“任务管理器”: 1.下载安装程序. 2.一路"next"安装.安装成功后,桌面会出现图标: 3.双击该图标,成功运行后,右小角出现: 右 ...

  2. Centos7防火墙和SELinux的开启和关闭

    在虚拟机里面开启多个服务,对应多个端口,在防火墙开启的情况下,就要对外开放端口,这样客户端才能正常访问,但比较繁琐,关闭更直接点. 防火墙 临时关闭防火墙 systemctl stop firewal ...

  3. PerPoly Collision(用物体本身模型做碰撞)

    打开static mesh editor,在static mesh settings里找到collision complexity选项,选择最后一项: Use Complex collision as ...

  4. BaseActivity 基类

    public abstract class BaseActivity extends AppCompatActivity implements IBaseView { private ProxyAct ...

  5. HTML中<input>和<textarea>的区别

    在HTML中有两种方式表达文本框 一个是<input>元素的单行文本框 一种是<textarea>的多行文本框. <input>元素: 1.一定要指定type的值为 ...

  6. MySQL innodb的组合索引各个列中的长度不能超过767,

    MySQL索引的索引长度问题   MySQL的每个单表中所创建的索引长度是有限制的,且对不同存储引擎下的表有不同的限制. 在MyISAM表中,创建组合索引时,创建的索引长度不能超过1000,注意这里索 ...

  7. Dpr ppi 适配 等概念 弹性属性的讲解

    Dpr: Dpr的全称(Device pixel ratio)像素设备比例:就是说每个设备像素上占有的css位像素的个数 苹果手机常见的设备像素比:1.0安卓 iPhone2.0  3.0 如果是1. ...

  8. 134、TensorFlow检查点checkpoint文件中的信息

    # 1.你想创建多少Saver对象就可以创建多少,如果你需要去保存和恢复不同的子图模型 # 同样的变量可以在不同的saver对象中被加载 # 只有在Saver.restore()方法被调用的时候才会对 ...

  9. mysql.connector.errors.ProgrammingError: 1698 (28000): Access denied for user 'root'@'localhost'

    排错,首先在sql编辑工具 通过测试连接,查看一下自己的密码是否正确 : 由此可见,是自己的密码错误,输入正确的密码后 在这里把密码修改为正确之后程序运行正常 #初始化数据库连接 engine = c ...

  10. Drone 持续集成实践 - 基于 Gogs,以 Golang 为例

    Drone 官方示例 - Example Go project 用 Docker 部署 Go 服务器 Golang 官方示例 - outyet 一个生产环境的例子 用 rsync 复制文件的方式进行部 ...