线段树维护贪心
/* */
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
#include<set>
#define ll long long
#define M 500010
#define mmp make_pair
#define INF 1000000000
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
vector<int> v[M] ;
int h[M], hm[M], st_tmp[M], scnt ;
struct P {
int id, hm ;
bool operator < (const P &rhs) const {
return hm<rhs.hm ;
}
} cave[M]; void dfs0(int x, int f, int nowh) {
hm[x]=nowh ;
for(auto i : v[x]) if(i != f)
dfs0(i, x, min(nowh, h[i])) ;
}
int tmp[M], tcnt ;
void dfs2(int x, int f, int nowhm) {
tmp[tcnt++]=nowhm ;
for(auto i : v[x]) if(i != f) {
int newhm=min(nowhm, h[i]) ;
if(newhm > hm[i]) dfs2(i, x, newhm) ;
}
} multiset<int> st ;
bool check() {
if(tcnt < st.size()) return 0 ;
scnt=0 ;
for(int i=0; !st.empty() && i<tcnt; i++) {
auto it=st.upper_bound(tmp[i]) ;
if(it != st.begin())
it-- , st_tmp[scnt++]=*it , st.erase(it) ;
}
int sz=st.size() ;
for(int i=0; i<scnt; i++) st.insert(st_tmp[i]) ;
return sz==0 ;
} int LIM ;
bool dfs(int x, int f, int add) {
if(h[x]<=LIM) {
int newhm= (f==-1 ? h[x]+add : min(hm[f], h[x]+add)) ;
if(newhm>hm[x]) {
tcnt=0 ;
dfs2(x, f, newhm) ;
if(check()) return 1 ;
}
}
for(auto i : v[x]) if(i != f && dfs(i, x, add))
return 1 ;
return 0 ;
} main() {
int n = read();
for(int i=1; i<=n; i++) h[i] = read();
for(int i=1; i<n; i++) {
int x = read(), y = read();
v[x].push_back(y) ;
v[y].push_back(x) ;
}
dfs0(1, -1, h[1]) ; int k = read();
for(int i=1, x; i<=k; i++) x = read(), st.insert(x) ;
for(int i=k+1; i<=n; i++) st.insert(0) ;
for(int i=1; i<=n; i++) cave[i]=(P) {
i, hm[i]
} ;
sort(cave+1, cave+n+1) ; LIM=-1 ;
for(int i=1; i<=n; i++) {
auto it=st.upper_bound(cave[i].hm) ;
if(it != st.begin()) st.erase(--it) ;
else if(LIM==-1) LIM=cave[i].hm ;
}
if(st.empty()) {
printf("0\n") ;
return 0;
}
if(!dfs(1, -1, INF)) {
printf("-1\n") ;
return 0 ;
} int l=0 , r=INF ;
while(r-l>1) {
int mid=(r+l)/2 ;
if(dfs(1, -1, mid)) r=mid ;
else l=mid ;
}
printf("%d\n", r) ;
}

CF533A Berland Miners的更多相关文章

  1. 【贪心 二分图 线段树】cf533A. Berland Miners

    通过霍尔定理转化判定方式的一步还是很妙的 The biggest gold mine in Berland consists of n caves, connected by n - 1 transi ...

  2. ATC/TC/CF

    10.25 去打 CF,然后被 CF 打了. CF EDU 75 A. Broken Keyboard 精神恍惚,WA 了一发. B. Binary Palindromes 比赛中的憨憨做法,考虑一个 ...

  3. cf723d Lakes in Berland

    The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cel ...

  4. CF723D. Lakes in Berland[DFS floodfill]

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. codeforces 723D: Lakes in Berland

    Description The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × ...

  6. CF 370B Berland Bingo

    题目链接: 传送门 Berland Bingo time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  7. Codeforces Round #Pi (Div. 2) B. Berland National Library set

    B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  8. Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. react-redux 知识点

    React-Redux 使用 如果只使用redux,那么流程是这样的: component --> dispatch(action) --> reducer --> subscrib ...

  2. sql update操作结果

    Mysql 在MySQL中只有真正对记录进行修改了的情况下,row_count才会去记录影响的行数,否则如果记录存在但是没有实际修改则不会将该次更新记录到row_count中. update操作执行结 ...

  3. Hadoop HDFS DataNode 目录结构

    DataNode 目录结构 和namenode不同的是,datanode的存储目录是初始阶段自动创建的,不需要额外格式化. 1.    在/opt/module/hadoop-2.7.2/data/t ...

  4. Asp.Net Grieview Eval 绑定数据 调用JS事件

    <asp:TemplateField ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp: ...

  5. java远程调用linux的命令或者脚本

    转载自:http://eksliang.iteye.com/blog/2105862 Java通过SSH2协议执行远程Shell脚本(ganymed-ssh2-build210.jar) 使用步骤如下 ...

  6. Linux命令之shutdown

    shutdown命令安全地将系统关机. 有些用户会使用直接断掉电源的方式来关闭linux,这是十分危险的.因为linux与windows不同,其后台运行着许多进程,所以强制关机可能会导致进程的数据丢失 ...

  7. 魔豆应用开发傻瓜书——helloworld

    一.准备 对于使用Windows的朋友,请注意,你们的编译器⼀定要将Dos换⾏符设置变更为Unix换行符,否则在路由器里就会看到每行的最后有一个^M,对于部分命令的正确执⾏是有问题的. 二.建立项目 ...

  8. 【转】jumpserver 堡垒机环境搭建(图文详解)

    jumpserver 堡垒机环境搭建(图文详解)   摘要: Jumpserver 是一款由python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装ag ...

  9. Dubbo的原理以及详细原理、配置

    Dubbo的背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. Dubbo的应用 用于大规模 ...

  10. 【redis】之centos6.x安装redis3.0.x

    centos6.9_x86_64 1.下载redis安装包 http://download.redis.io/releases/redis-3.2.9.tar.gz 2.解压 编译到指定得目录 mak ...