【贪心 二分图 线段树】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$个数字填满了目标序列的 ...
随机推荐
- TFS 签入时,提示“变更集注释策略 中的内部错误……”
提示: 变更集注释策略 中的内部错误.加载 变更集注释策略 策略时出错(策略程序集“Microsoft.TeamFoundation.PowerTools.CheckinPolicies.Change ...
- js校验金额输入
//验证非负实数,保留2位小数,不采取四舍五入,不能去掉无效零,obj是this //示例:<input type="text" onkeyup="IWS_Chec ...
- 从HTML form submit 到 django response是怎么完成的
HTML form 里的数据是怎么被包成http request 的?如何在浏览器里查看到这些数据? 浏览器做的html数据解析 form里的数据变成name=value对在POST Body中 re ...
- map系统学习
映射map又称字典,表,或者查找表,其元素是由key和value两个分量组成的对偶(key,value). key是键,value是与键key相关联的映射值,这样的元素又称“关联”.key和value ...
- centos服务器nginx相关命令
1.找到nginx路径: ps aux | grep nginx -> /usr/local/nginx/sbin/nginx 2.nginx配置检查: /usr/local/nginx/sbi ...
- pycharm 更改字体and背景颜色
File-settings-Appearance&Behavior-Appearance-Theme File-settings-Editor-font
- CentOS 6.2安装nagios
nagios分为监控机和被监控机两类机器 监控主机:gserver150(192.168.111.150) 被监控主机:gserver151(192.168.111.151) 一. 监 ...
- 记录下laravel 5.2的auth/logout路由工作不正常的问题
- Arduino连接SHT10温湿度传感器--返回值不正常解决办法
如题目,arduino中连接温湿度传感器,用的是一个github开源项目,地址:点击打开,其实这个就是一个封装好的库,下载后把解压的文件夹复制到Arduino目录下的librarys文件夹内,重启Ar ...
- es6声明对象以及作用域与es5对比
es6声明变量: let x=1;//声明一个变量 const y=2;//声明一个只读常量,声明时必须赋值,之后值不可修改 es5声明变量: var z=3;//声明一个变量 区别: let不存在变 ...