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 ...
随机推荐
- 不同eclipse版本的git库使用
在eclipse 4.4.2 -win32版本下编译好后上传版本: 1.在eclipse 4.4.0 -win64 版本下,编译报 缺少 sevlet.http...库,解决方法 project - ...
- java 加载数据库驱动
JDBC编程步骤见 JDBC编程步骤 JDBC编程的第一步是加载数据库驱动,使用Class类的forName()方法,Class.forName("com.mysql.jdbc.Driver ...
- Jmeter关于断言
本文摘自:一颗糖果 https://www.cnblogs.com/linglingyuese/p/linglingyuese-five.html 1.断言持续时间(Duration to Ass ...
- Spark应用HanLP对中文语料进行文本挖掘--聚类详解教程
软件:IDEA2014.Maven.HanLP.JDK: 用到的知识:HanLP.Spark TF-IDF.Spark kmeans.Spark mapPartition; 用到的数据集:http:/ ...
- MySQL触发器详解
MySQL触发器 触发器是特殊的存储过程.不同的是,触发器不需要手动调用.只要当预定义的事件发生时,会被MySQL自动调用.主要用于满足复杂业务的规则和需求. 一.创建触发器 1.创建只有一个执行语句 ...
- Centos6.7 运行 eclipse出错解决办法
今天在centos下运行eclipse c++出现来点问题.主要原因是jdk点安装以及环境变量始终不对. 尝试在/etc/profile中手动配置,也没有成功. 做了如下步骤成功解决. 1.查看jdk ...
- 手机浏览器User-Agent信息
ChormeMozilla/5.0 (Linux; Android 4.2.1; AMOI N828 Build/JOP40D) AppleWebKit/537.36 (KHTML, like Gec ...
- jersey2+freemarker+spring3的集成
由于即将开始的新项目,是一个对外网开放访问权限的web应用.所以,公司技术管理层不允许使用struts以及spring mvc这一套.所以,我们开始转战曾经用作REST API的框架jersey及其周 ...
- XE5开发Android程序调用电话相关功能(短信息和电话)
方法a.不使用Intent而是直接发短信. smsManager对应的Delphi代码应该是: uses Androidapi.JNI.JavaTypes,Androidapi.JNI.Telepho ...
- .net 4.0 程序遇到 停止工作 appcrash ,kernelbase.dll 等提示
经测试,删除*.exe.config 中 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v ...