【贪心 二分图 线段树】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$个数字填满了目标序列的 ...
随机推荐
- webpack配置Jquery全局包及全局包插件
一:在配置文件配置: plugins: [ //将来以template为模版,生成一个index.html并且发布到webpack-dev-server开启的node服务器上面去 new HtmlWe ...
- html5 替换 历史 记录
history.replaceState({url:"/admin/index"},null,"/admin/index"); url 是 需要替换的路径
- jquery click嵌套 事件重复注册 多次执行的问题
jquery click嵌套 事件重复注册 多次执行的问题 上面只是参考,我自己的解决方法是先使用unbind("click")解除事件然后再绑定新事件: $("#tes ...
- Hadoop实战:用Hadoop处理Excel通话记录
项目需求 有博主与家庭成员之间的通话记录一份,存储在Excel文件中,如下面的数据集所示.我们需要基于这份数据,统计每个月每个家庭成员给自己打电话的次数,并按月份输出到不同文件夹. 数据集 下面是部分 ...
- nodejs加密解密
nodejs是通集成在内核中的crypto模块来完成加密解密. 常用加密解密模块化代码: /** * Created by linli on 2015/8/25. */ var crypto = re ...
- Linux安装配置相关
1.常用汇总 useradd usertemp //添加用户 passwd usertemp //修改/设置密码 userdel usertemp //删除用户 2. profile/baserc等配 ...
- Maven的学习资料收集--(八) 构建MyBatis项目
在这里,写一下,怎么使用Maven构建MyBatis项目. 1. 新建一个Web项目 可以参考前面的博客 2. 修改pom.xml,添加MyBatis依赖 <project xmlns=&quo ...
- collectd 与 logstash配置
节点 node1: 配置logstash node2: 配置collectd, collectd收集本地的信息, 通过配置将信息发送到node1节点 node1安装配置logstash rpm -iv ...
- android 开发-ListView列表显示控件的实现
列表的显示需要三个元素: 1.ListVeiw 用来展示列表的View. 2.适配器 用来把数据映射到ListView上的中介. 3.数据 具体的将被映射的字符串,图片,或者基本组件. 根据列表 ...
- ASP.NET MVC缓存
根据缓存的位置不同,可以区分为: ①客户端缓存(缓存在用户的客户端,例如浏览器中) ②服务器缓存(缓存在服务器中,可以缓存在内存中,也可以缓存在文件里,并且还可以进一步地区分为本地缓存和分布式缓存两种 ...