CF533A Berland Miners
线段树维护贪心
/*
*/
#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的更多相关文章
- 【贪心 二分图 线段树】cf533A. Berland Miners
通过霍尔定理转化判定方式的一步还是很妙的 The biggest gold mine in Berland consists of n caves, connected by n - 1 transi ...
- ATC/TC/CF
10.25 去打 CF,然后被 CF 打了. CF EDU 75 A. Broken Keyboard 精神恍惚,WA 了一发. B. Binary Palindromes 比赛中的憨憨做法,考虑一个 ...
- 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 ...
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 × ...
- CF 370B Berland Bingo
题目链接: 传送门 Berland Bingo time limit per test:1 second memory limit per test:256 megabytes Descrip ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 基于MVC4+EasyUI的Web开发框架形成之旅(4)--附件上传组件uploadify的使用
大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随笔<Web开发中的文件上传组件uploadify的使用>中可以看到,Asp.NET中 ...
- xcrun: error: invalid active developer path
问题 mac升级到10.12(macOS Sierra),执行命令,出现如下错误: xcrun: error: invalid active developer path (/Library/Deve ...
- Hive 特殊分隔符处理
HIVE特殊分隔符处理 Hive对文件中的分隔符默认情况下只支持单字节分隔符,,默认单字符是\001.当然你也可以在创建表格时指定数据的分割符号.但是如果数据文件中的分隔符是多字符的,如下图: 01| ...
- Linux mktemp命令
mktemp命令 Linux mktemp命令用于建立暂存文件.mktemp建立的一个暂存文件,供shell script使用.主要特点就是可以做到每次执行mktemp时产生文件和目录都不重名:这个特 ...
- RedHat如何关闭防火墙 : http://blog.csdn.net/chongxin1/article/details/76072758
版本号:RedHat6.5 JDK1.8 Hadoop2.7.3 hadoop 说明:从版本2开始加入了Yarn这个资源管理器,Yarn并不需要单独安装.只要在机器上安装了JDK就可以直接安 ...
- Centos 7 systemctl和防火墙firewalld命令
今天自己在Hyper-v下搭建三台Linux服务器集群,用于学习ELKstack(即大数据日志解决技术栈Elasticsearch,Logstash,Kibana的简称),下载的Linux版本为cen ...
- 第一个NIOS II工程using Qsys-------Let Qsys Say Hello
1.新建工程 2.添加原理图文件 注:似乎Nios II工程都需要涉及到原理图编程. 3.进入Qsys进行内核设计 注:启动Qsys后,系统已经为内核默认添加了一个组件clk_0. 4.设置时钟名字和 ...
- FastReport 保存为文件
public void SaveToPDF<TModel>(List<TModel> model, string content, string saveFilePath) { ...
- NSIS 制作简单安装包
;replace NSIS/Contrib/zip2exe/Modern.nsh;NSI format: Asni;http://nsis.sourceforge.net/ ;UAC级别Request ...
- 持续集成(Continuous Integration)基本概念与实践
本文由Markdown语法编辑器编辑完成. From https://blog.csdn.net/inter_peng/article/details/53131831 1. 持续集成的概念 持续集成 ...