【贪心 二分图 线段树】cf533A. Berland Miners
通过霍尔定理转化判定方式的一步还是很妙的
The biggest gold mine in Berland consists of n caves, connected by n - 1 transitions. The entrance to the mine leads to the cave number 1, it is possible to go from it to any remaining cave of the mine by moving along the transitions.
The mine is being developed by the InMine Inc., k miners work for it. Each day the corporation sorts miners into caves so that each cave has at most one miner working there.
For each cave we know the height of its ceiling hi in meters, and for each miner we know his height sj, also in meters. If a miner's height doesn't exceed the height of the cave ceiling where he is, then he can stand there comfortably, otherwise, he has to stoop and that makes him unhappy.
Unfortunately, miners typically go on strike in Berland, so InMine makes all the possible effort to make miners happy about their work conditions. To ensure that no miner goes on strike, you need make sure that no miner has to stoop at any moment on his way from the entrance to the mine to his cave (in particular, he must be able to stand comfortably in the cave where he works).
To reach this goal, you can choose exactly one cave and increase the height of its ceiling by several meters. However enlarging a cave is an expensive and complex procedure. That's why InMine Inc. asks you either to determine the minimum number of meters you should raise the ceiling of some cave so that it is be possible to sort the miners into the caves and keep all miners happy with their working conditions or to determine that it is impossible to achieve by raising ceiling in exactly one cave.
Input
The first line contains integer n (1 ≤ n ≤ 5·105) — the number of caves in the mine.
Then follows a line consisting of n positive integers h1, h2, ..., hn (1 ≤ hi ≤ 109), where hi is the height of the ceiling in the i-th cave.
Next n - 1 lines contain the descriptions of transitions between the caves. Each line has the form ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi), where aiand bi are the numbers of the caves connected by a path.
The next line contains integer k (1 ≤ k ≤ n).
The last line contains k integers s1, s2, ..., sk (1 ≤ sj ≤ 109), where sj is the j-th miner's height.
Output
In the single line print the minimum number of meters that you need to raise the ceiling by in some cave so that all miners could be sorted into caves and be happy about the work conditions. If it is impossible to do, print - 1. If it is initially possible and there's no need to raise any ceiling, print 0.
题目大意
有一个以 1 号点为根的 N 个节点的树形洞穴结构,每个节点有高度 $H[i]$,还有 $M$ 根长棒,每根的长度为 $B[i]$。要求把这些长棒放置到树上的一些节点上,每个节点最多只能放一根棒子,并且如果要把一根棒子放到一个节点上,必须满足这个点到根路径上所有点的高度都不低于棒子的长度。 可是,这样的方案似乎太难实施了。因此,允许对树上的一个节点开凿,使那个点的高度变大,要求开凿后方案能够顺利实施。如果无解则输出$-1$,如果不需要进行开凿输出 $0$,否则输出开凿点高度增加的最小值。
题目分析
首先考虑如何判定无修改时是否合法。
先对于每一个树上节点,预处理出$mn_i$表示从根到$i$路径上最小点权;那么就可以连边向$b_j(b_j \le mn_i)$,这就是一个网络流的模型,复杂度大致是$O(n\times 网络流)$级别的,如果想要进一步优化可以采用线段树优化建边的方式。但是很明显,这样处理没有用好这个图的性质,实际的表现远远不够通过此题。
由于只有节点向木棒的连边,这张图变成了判定二分图是否具有完全匹配的问题。因此可以用霍尔定理得到方案合法的充要条件:将$b_i$从小到大排序,$\forall deg_{b_i}\ge m-i+1$.
有了这个充要条件,就可以在均摊$\log n$复杂度内判断修改一个节点是否能够使方案合法。所以总的复杂度是$O(n\log n)$的。
似乎挺多人的做法是$O(n\log^2 n)$的,即枚举每个节点时二分修改的值。实际上只需要预处理出一个最大的非法位置$lst$,修改时考虑把$lst$这个位置安排进去。因为每修改一个节点,它所影响的其他所有节点增量必定是小于等于它自身的,那么当然是只要判定是否能够加入最大非法位置即可。
#include<bits/stdc++.h>
typedef std::set<std::pair<int, int> > spar;
const int maxn = ;
const int maxm = ;
const int INF = 2e9; struct node
{
int mn,tag;
}f[maxn<<];
int n,ans,lst;
int h[maxn],c[maxn],m,b[maxn];
int edgeTot,head[maxn],nxt[maxm],edges[maxm];
int mn[maxn],sn[maxn];
std::vector<int> vec[maxn];
spar::iterator it;
spar s; int read()
{
char ch = getchar();
int num = , fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = -;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
return num*fl;
}
void addedge(int u, int v)
{
edges[++edgeTot] = v, nxt[edgeTot] = head[u], head[u] = edgeTot;
edges[++edgeTot] = u, nxt[edgeTot] = head[v], head[v] = edgeTot;
}
void pushup(int rt)
{
f[rt].mn = std::min(f[rt<<].mn, f[rt<<|].mn);
}
void pushdown(int rt)
{
if (f[rt].tag){
int tag = f[rt].tag;
f[rt<<].mn += tag, f[rt<<|].mn += tag;
f[rt<<].tag += tag, f[rt<<|].tag += tag;
f[rt].tag = ;
}
}
void build(int rt, int l, int r)
{
if (l==r) f[rt].mn = -(m-l+);
else{
int mid = (l+r)>>;
build(rt<<, l, mid);
build(rt<<|, mid+, r);
pushup(rt);
}
}
void modify(int rt, int l, int r, int L, int R, int c)
{
if (L <= l&&r <= R){
f[rt].mn += c, f[rt].tag += c;
return;
}
int mid = (l+r)>>;
pushdown(rt);
if (L <= mid) modify(rt<<, l, mid, L, R, c);
if (R > mid) modify(rt<<|, mid+, r, L, R, c);
pushup(rt);
}
int query(int rt, int l, int r, int c)
{
if (l==r) return f[rt].mn;
int mid = (l+r)>>;
pushdown(rt);
if (c <= mid) return query(rt<<, l, mid, c);
else return query(rt<<|, mid+, r, c);
}
void dfs(int x, int fa)
{
s.insert(std::make_pair(h[x], x));
it = s.begin();
mn[x] = (*it).first, vec[(*it).second].push_back(x);
if ((++it)!=s.end()) sn[x] = (*it).first;
else sn[x] = INF;
for (int i=head[x]; i!=-; i=nxt[i])
if (edges[i]!=fa) dfs(edges[i], x);
s.erase(s.find(std::make_pair(h[x], x)));
}
void End(int x){printf("%d\n",x), exit();}
int main()
{
memset(head, -, sizeof head);
n = read();
for (int i=; i<=n; i++) h[i] = read();
for (int i=; i<n; i++) addedge(read(), read());
m = read();
for (int i=; i<=m; i++) b[i] = read();
std::sort(b+, b+m+);
dfs(, ), build(, , m);
for (int i=; i<=n; i++)
{
c[i] = std::upper_bound(b+, b+m+, mn[i])-b-;
if (c[i]) modify(, , m, , c[i], );
}
if (f[].mn >= ) End();
ans = INF;
for (lst=m; lst>=; lst--) //lst=n
if (query(, , m, lst) < ) break;
for (int i=, mx; i<=n; i++)
if (h[i] <= b[lst]&&sn[i] >= b[lst]&&((int)vec[i].size() >= -f[].mn)){
mx = vec[i].size();
for (int j=; j<mx; j++)
{
int val = std::min(sn[vec[i][j]], b[lst]);
int id = std::upper_bound(b+, b+m+, val)-b-;
if (c[vec[i][j]]) modify(, , m, , c[vec[i][j]], -); //c[i]
if (id) modify(, , m, , id, );
}
if (f[].mn >= ) ans = std::min(ans, b[lst]-h[i]);
for (int j=; j<mx; j++)
{
int val = std::min(sn[vec[i][j]], b[lst]);
int id = std::upper_bound(b+, b+m+, val)-b-;
if (c[vec[i][j]]) modify(, , m, , c[vec[i][j]], );
if (id) modify(, , m, , id, -);
}
}
End(ans==INF?-:ans);
return ;
}
END
【贪心 二分图 线段树】cf533A. Berland Miners的更多相关文章
- [BZOJ 4025]二分图(线段树分治+带边权并查集)
[BZOJ 4025]二分图(线段树分治+带边权并查集) 题面 给出一个n个点m条边的图,每条边会在时间s到t出现,问每个时间的图是否为一个二分图 \(n,m,\max(t_i) \leq 10^5\ ...
- BZOJ 4025: 二分图 [线段树CDQ分治 并查集]
4025: 二分图 题意:加入边,删除边,查询当前图是否为二分图 本来想练lct,然后发现了线段树分治的做法,感觉好厉害. lct做法的核心就是维护删除时间的最大生成树 首先口胡一个分块做法,和hno ...
- 【Luogu1973】仓配置(贪心,线段树)
[Luogu1973]仓配置 题面 直接找洛谷把... 题解 很明显的贪心吧 按照线段的右端点为第一关键字,左端点第二关键字排序 然后线段树维护区间最小就可以啦 #include<iostrea ...
- bzoj4025二分图(线段树分治 并查集)
/* 思维难度几乎没有, 就是线段树分治check二分图 判断是否为二分图可以通过维护lct看看是否链接出奇环 然后发现不用lct, 并查集维护奇偶性即可 但是复杂度明明一样哈 */ #include ...
- 【Luogu1937】仓配置(贪心,线段树)
[Luogu1937]仓配置 题面 直接找洛谷把... 题解 很明显的贪心吧 按照线段的右端点为第一关键字,左端点第二关键字排序 然后线段树维护区间最小就可以啦 #include<iostrea ...
- CF533A Berland Miners
线段树维护贪心 /* */ #include<cstdio> #include<algorithm> #include<cstring> #include<i ...
- [BZOJ4025]二分图(线段树分治,并查集)
4025: 二分图 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2191 Solved: 800[Submit][Status][Discuss] ...
- [bzoj1577][Usaco2009 Feb]庙会捷运Fair Shuttle_贪心_线段树
庙会捷运 Fair Shuttle bzoj-1577 Usaco-2009 Feb 题目大意:有一辆公交车从1走到n.有m群奶牛从$S_i$到$E_i$,第i群奶牛有$W_i$只.车有一个容量c.问 ...
- [BJOI2019] 删数 [dp转贪心结论+线段树]
题面 传送门 思路 dp部分 以下称合法序列为原题面中可以删空的序列 这个是我在模拟考场上的思路 一开始我是觉得,这个首先可以写成一个dp的形式:$dp[i][j]$表示用$j$个数字填满了目标序列的 ...
随机推荐
- ubuntu16.0.4下修改MySQL的data目录之mysqld启动报错
由于需要更换MySQL的data目录,更改完成后启动报错如下: apparmor="DENIED" operation="mknod" profile=&quo ...
- 【ACM】最长公共子序列 - 动态规划
最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...
- Phpstorm Git 操作
一.前提: 1.下载并安装好 Phpstorm 2.下载并安装好 Git 3.熟悉 Git 相关命令行操作 二.Git pull & commit(add): 下面简单说一下相关操作: Php ...
- remote error: You can't push to git 解决办法
- malloc内存申请--释放-收缩
一.验证思路和代码 #include <stdio.h> #include <unistd.h> #include <malloc.h> #include < ...
- CentOS yum安装mcrypt
CentOS yum安装mcrypt 本篇排错的前提是只想用yum安装,不想使用源码包编译安装. php依赖一下包: #yum install libmcrypt libmcrypt-deve ...
- vue使用element-ui实现按需引入
基于Vue的Ui框架 饿了么公司基于vue开的的vue的Ui组件库 Element Ui 基于vue pc端的UI框架 MintUi 基于vue 移动端的ui框架 http://element.ele ...
- >>我要到处浪系列 之 JS随便投票小脚本
首先郑重声明:我不是对任何网站或者任何个人或组织有意见,仅仅是觉得 4点几 的评分对某些玩票的片段都太高了,为了落实想法,切实履行公民的投票权,并且 bibibabibobi biubiubiu..所 ...
- thinkphp搜索实现
视图: <html lang="zh-cn"><head> <meta charset="UTF-8"><title& ...
- Apache Spark 2.2.0 中文文档 - GraphX Programming Guide | ApacheCN
GraphX Programming Guide 概述 入门 属性 Graph 示例属性 Graph Graph 运算符 运算符的汇总表 Property 运算符 Structural 运算符 Joi ...