Codeforces Round #408 (Div. 2) C
Description
Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks.
There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength ai.
Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.
When a bank is hacked, it becomes offline (and no longer online), and other banks that areneighboring or semi-neighboring to it have their strengths increased by 1.
To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met:
- Bank x is online. That is, bank x is not hacked yet.
- Bank x is neighboring to some offline bank.
- The strength of bank x is less than or equal to the strength of Inzane's computer.
Determine the minimum strength of the computer Inzane needs to hack all the banks.
The first line contains one integer n (1 ≤ n ≤ 3·105) — the total number of banks.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the strengths of the banks.
Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — meaning that there is a wire directly connecting banks ui and vi.
It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
5
1 2 3 4 5
1 2
2 3
3 4
4 5
5
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
93
5
1 2 7 6 7
1 5
5 3
3 4
2 4
8
In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how:
- Initially, strengths of the banks are [1, 2, 3, 4, 5].
- He hacks bank 5, then strengths of the banks become [1, 2, 4, 5, - ].
- He hacks bank 4, then strengths of the banks become [1, 3, 5, - , - ].
- He hacks bank 3, then strengths of the banks become [2, 4, - , - , - ].
- He hacks bank 2, then strengths of the banks become [3, - , - , - , - ].
- He completes his goal by hacking bank 1.
In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93.
题意:我们可以攻击比自己能力小的,攻击成功后,与它相邻的或者间接相邻防御的会+1,问全部攻击成功最小要多少攻击力
题意:因为是求最小值,想到二分,我们把最小防御和最大防御求出来。接下来就是判断能不能符合条件了,我们根据模拟发现,除了相邻的或者间接相邻的,其他都是+2,我们就先将所有防御与中间值比较,大的就return 0,否则就+2
对于每一个+2的点,如果大于中间值,记录个数,再次遍历,到了大于中间值的点,那么说明可以作为开始点,与它相邻的点-1等于中间值,减去个数,如果最后等于0,说明可行
#include<bits/stdc++.h>
using namespace std;
const int len=;
long long maxn=-;
long long minn=(<<)-;
long long x[len];
long long y[len];
long long n,m;
vector<int>q[len];
int solve(long long w)
{
int cnt=;
for(int i=;i<=n;i++)
{
y[i]=x[i];
//if(x[i]>w) return 0;
}
for(int i=;i<=n;i++)
{
if(x[i]>w) return ;
y[i]+=;
if(y[i]>w)
{
cnt++;
}
}
if(cnt==) return ;
if(n==) return ;
for(int i=;i<=n;i++)
{
int ans=cnt;
if(y[i]>w)
{
ans--;//大于x的点作为起点,相邻的点为+1
}
for(int j=;j<q[i].size();j++)//遍历相邻的点
{
int pos=q[i][j];
if(pos==i) continue;
if(y[pos]-==w)
{
ans--;
}
}
if(ans==) return ;
}
return ;
} long long answer;
int main()
{
scanf("%lld",&n);
for(int i=;i<=n;i++)
{
scanf("%lld",&x[i]);
maxn=max(x[i],maxn);
minn=min(x[i],minn);
}
for(int i=;i<=n-;i++)
{
long long u,v;
scanf("%lld%lld",&u,&v);
q[u].push_back(v);
q[v].push_back(u);
}
if(n==)
{
printf("%lld\n",x[]);
return ;
}
long long ans;
maxn=maxn+;
while(minn<=maxn)
{
long long mid=(maxn+minn)/;
if(solve(mid))
{
ans=mid;
maxn=mid-;
}
else
{
minn=mid+;
}
}
printf("%lld\n", ans);
return ;
}
Codeforces Round #408 (Div. 2) C的更多相关文章
- Codeforces Round #408 (Div. 2)(A.水,B,模拟)
A. Buying A House time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)
题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...
- Codeforces Round #408 (Div. 2) C. Bank Hacking
http://codeforces.com/contest/796/problem/C Although Inzane successfully found his beloved bone, Zan ...
- Codeforces Round #408 (Div. 2) A B C 模拟 模拟 set
A. Buying A House time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #408 (Div. 2) D - Police Stations
地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...
- Codeforces Round #408 (Div. 2) B
Description Zane the wizard is going to perform a magic show shuffling the cups. There are n cups, n ...
- Codeforces Round #408 (Div. 2)
C. Bank Hacking 题目大意:给出一棵n个节点的树,每个节点有一个权值,删掉一个点的代价为当前这个点的权值,并且会使其相邻点和距离为2且中间隔着未被删除的点的点权值加1,现在选一个点开始删 ...
- Codeforces Round #408 (Div. 2) 题解【ABCDE】
A - Buying A House 题意:给你n个房间,妹子住在第m个房间,你有k块钱,你想买一个离妹子最近的房间.其中相邻的房间之间距离为10,a[i]=0表示已经被别人买了. 题解:扫一遍更新答 ...
- Codeforces Round #408 (Div. 2) D. Police Stations(最小生成树+构造)
传送门 题意 n个点有n-1条边相连,其中有k个特殊点,要求: 删去尽可能多的边使得剩余的点距特殊点的距离不超过d 输出删去的边数和index 分析 比赛的时候想不清楚,看了别人的题解 一道将1个联通 ...
- Codeforces Round #408 (Div. 2) C.Bank Hacking(二分)
传送门 题意 给出n个银行,银行之间总共有n-1条边,定义i与j有边相连为neighboring,i到j,j到k有边,则定义i到k的关系为semi- neighboring, 每家银行hack的难度为 ...
随机推荐
- 信雅达面试题atoi函数实现
atoi函数: 功 能: 把字符串转换成整型数. 名字来源:ASCII to integer 的缩写. 原型: int atoi(const char *nptr); 函数说明 参数nptr字符串,如 ...
- Apache Qpid 高可用集群
一.RHCS RHCS是Red Hat Cluster Suite(红帽子集群套件)的缩写.RHCS是一个功能完备的集群应用解决方案,它从应用的前端访问到后端的数据存储都提供了一个行之有效的集群架构实 ...
- 【网站支付PHP篇】thinkPHP集成汇潮支付(ecpss)
系列目录 支付宝集成:http://www.cnblogs.com/nerve/p/3437879.html 系列说明 最近在帮朋友的系统安装支付模块(兑换网站积分),现在总结一些开发心得,希望对大家 ...
- 三分钟教你学Git(十四) 之 线下传输仓库
有时候还有一个人不能从远程直接clone仓库或者说由于非常大,clone非常慢或其他原因.我们能够使用bundle命令将Git仓库打包,然后通过U盘或者是其他介质拷贝给他,这样他拿到打包好的仓库后能够 ...
- IDHTTP用法详解 good
一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入ID ...
- AndroidStudio——Android SDK
前言 安卓的SDK包,跨过长城下载好的,分享出来一下~ Android Studio版本 | 3.4.1 下载地址 微云下载地址 | 链接:https://share.weiyun.com/5rm6l ...
- codeforces 399B. Red and Blue Balls 解题报告
题目链接:http://codeforces.com/problemset/problem/399/B 题目意思:给出 n 个只由 R 和 B 组成的字符串(由上到下排列,相当于栈),问最多可以操作多 ...
- hdu-5675 ztr loves math(数学)
题目链接: ztr loves math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 【AHOI2009】中国象棋
[题目链接] 点击打开链接 [算法] 动态规划 f[i][j][k]表示前i行,有j列放了1个,有k列放了两个 分六种情况讨论即可 [代码] #include<bits/stdc++.h> ...
- bzoj 3073 Journeys —— 线段树优化连边
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3073 建两棵线段树,一棵从下往上连边,一棵从上往下连边,叶子节点之间也有连边: 区间向区间连 ...