线段树维护贪心
/* */
#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. java 连接 hiveserver2 例子

    启动了  hiveserver2 以后才能使用 程序连接 .目前的 使用的 是  server2 版本.和以前的版本驱动包名不同: package hadoop; import java.sql.Co ...

  2. py-day1-1 python的基本运算符和语句

    整体注释:  选中目标  ctrl + ?    基础: 运算符: **  表示幂函数 in  和 not in  : 比较运算符: 基本语法: pass 代表空代码,无意义,仅仅用于表示代码块: 引 ...

  3. jmeter分布式测试的坑

    转 : jmeter分布式测试的坑 有关jmeter分布式测试的环境配置,大概就是那样,但是每次想要进行jmeter分布式测试的时候,总是会有各种奇怪的问题,下面整理了一些可能遇到的坑. 只要错误中出 ...

  4. 服务器病了吗? Linux 服务器的那些性能参数指标

    一个基于 Linux 操作系统的服务器运行的同时,也会表征出各种各样参数信息.通常来说运维人员.系统管理员会对这些数据会极为敏感,但是这些参数对于开发者来说也十分重要,尤其当你的程序非正常工作的时候, ...

  5. JAVA的debug入门和多断电调试

    调试的一般都是逻辑 第一步的错误双击数字旁边的蓝色地方,或者点击右键如图 断点的意思就是程序执行在哪里就停止 当找不到DEBUG中的Variables是在位置输入Variables就可以了: 再按下F ...

  6. WPF实现打印用户界面功能

    方式一:public bool Print(string pathStr) { try { if (File.Exists(pathStr) == false) return false; var p ...

  7. sqlserver统计日志数目

    SELECT COUNT(1) FROM [sys].[fn_dblog](NULL,NULL)

  8. 使用socket.io client 开发时兼容IE低版本的办法

    使用socket.io client 开发时兼容IE低版本的办法 socket.io提供了针对各个版本浏览器的‘socket’功能的封转:websocket,长连接,流,flash什么的.给你格式化下 ...

  9. tomcat源码 Connector

    Connector容器主要负责解析socket请求,在tomcat中的源码位于org.apache.catalina.connector和org.apache.coyote包路径下:通过上两节的分析, ...

  10. Javascript中变量提升的问题(五)

    一.函数声明变量提升   函数声明具有变量提升的问题,所以在函数被声明之前就可以访问. console.log(getValue()); function getValue() { return 'a ...