【贪心 二分图 线段树】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$个数字填满了目标序列的 ...
随机推荐
- 73th LeetCode Weekly Contest Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- PHP根据经纬度计算距离
思路: 公式: W为纬度对应的弧度,J为经度对应的弧度,如上图所示 下面代码 lat是纬度 lng是经度 /** * 根据经纬度算距离,返回结果单位是公里,先纬度,后经度 * @param $la ...
- linux下WPS的使用
WPS退出了wps for linux ,高版本的一直安装不上,低版本的原来在桌面都有图标,重装后安装位置不是很明显打开关闭不是很方便.并且也不利于在终端模式下使用.现简单总结一下wps的表格 文字 ...
- hdu3949(线性基,求第k小的异或和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 XOR Time Limit: 2000/1000 MS (Java/Others) Me ...
- TortoiseSVN 控制图标未显示或显示异常解决方法
- strstr strcpy 函数的实现
一. strcpy 代码实现 #include <iostream> #include <assert.h> #include <iostream> //#incl ...
- notepad++上配置ruby执行环境
1.安装NppExec 插件 2.按快捷键F6,在弹出框中输入如下命令: npp_save cd "$(CURRENT_DIRECTORY)" jruby "$(FI ...
- Swift基础学习笔记
1.在学基本语法之前,简单看一下与OC的不同 注释:OC #pragma marks 视图加载完成 Swift //MARK: 视图加载完成 //TOOO:设置背景颜色(Xco ...
- 一、 Spring Cloud Eureka ,咱们先跑起来
一.Spring Cloud 简介 Spring Cloud 是一个基于Spring Boot 实现的微服务架构开发工具.是一个涉及到服务治理.分布式配置管理.负载均衡.服务容错.API网关.消息总线 ...
- Teradata 认证系列 - 1. TCPP这是个啥
一看历史,好几年没发帖...正好最近在自学teradata认证(学也不一定学的完,最后也不一定去考,仅仅安慰一下不想碌碌无为的内心) 网上一搜,百度上的中文相关资料简直为0.这个不奇怪,毕竟都没什么人 ...