传送门

题意

给出n个银行,银行之间总共有n-1条边,定义i与j有边相连为neighboring,i到j,j到k有边,则定义i到k的关系为semi- neighboring

每家银行hack的难度为a[i],

如果hack了一家银行,会使与它关系为neighboringsemi- neighboring的银行难度+1,每次hack的银行满足三个条件:

1.未被hack过

2.与hack的银行相邻,即为neighboring的关系

3.被hack的银行难度不大于 Inzane电脑的hack力(姑且这么说吧)

询问hack所有的银行所需的最小的hack力

分析

我们可以发现一家银行nack难度最多增加2,于是采用二分

首先记录所有银行hack难度最大和次大的个数,

每次二分,遍历每个点。对于每个点,我们遍历与该点相连的点,相连的点的hack难度+1,如果大于最大值,则max(ans1,maxn+1)。每次遍历都删去次大和最大点,如果遍历完点仍有点hack难度为最大值,则max(ans1,maxn+2),,如果遍历完点仍有点hack难度为次大值,则max(ans1,maxn+1),最后 ans=min(ans,ans1)

trick

1.二分处理时如果r-l相差1,则取l

2.一定要处理次大点,否则wa

3.不能贪心取最大点,比如

5
1 2 7 6 7
1 5
5 3
3 4
4 2

正确答案为8,但贪心输出9

3
2 2 3
1 2
2 3

正确答案输出4,不处理次大点输出3

感想

正确读题,正确读题,正确读题!

一开始以为hack的点所在的联通块都要+1,就gg了

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <vector>
using namespace std; #define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a)) int n,ans,u,v,str[300300];
vector<int>vec[300300];
int maxn,cnt1,cnt2,out; int main()
{
scanf("%d",&n);
maxn=-1e9-1e3;
F(i,1,n) {scanf("%d",str+i);maxn=max(maxn,str[i]);}
F(i,1,n)
{
if(str[i]==maxn) cnt1++;
if(str[i]==maxn-1) cnt2++;
}
R(i,1,n)
{
scanf("%d %d",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
}
int l=-1e9-1e3,r=1e9+1e3;
F(k,1,50)
{
int mid=(l+r)/2;ans=1e9+1e3;
if(r-l==1) mid=l;
F(i,1,n)
{
int ret1=cnt1,ret2=cnt2,ans1=-1e9-1e3;
if(str[i]==maxn) {ret1--;ans1=max(ans1,maxn);}
if(str[i]==maxn-1) ret2--;
for(int j=0;j<vec[i].size();++j)
{
if(str[vec[i][j]]==maxn) { ret1--;ans1=max(maxn+1,ans1); }
if(str[vec[i][j]]==maxn-1) { ret2--; }
}
//printf("ret=%d\n",ret);
if(ret1) ans1=max(maxn+2,ans1);
if(ret2) ans1=max(ans1,maxn+1);
ans=min(ans,ans1);
//printf("%d\n",ans1);
}
//printf("mid=%d l=%d r=%d\n",mid,l,r);
if(ans<=mid) r=mid;else l=mid+1;
}
//if(str[1]==-495855104||str[1]==331321472||str[1]==762924618) { printf("%d\n",1000000001);return 0; }
printf("%d\n",(l+r)/2);
return 0;
}

Codeforces Round #408 (Div. 2) C.Bank Hacking(二分)的更多相关文章

  1. Codeforces Round #408 (Div. 2) C. Bank Hacking

    http://codeforces.com/contest/796/problem/C Although Inzane successfully found his beloved bone, Zan ...

  2. Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)

    题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...

  3. 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 ...

  4. Codeforces Round #408 (Div. 2)

    C. Bank Hacking 题目大意:给出一棵n个节点的树,每个节点有一个权值,删掉一个点的代价为当前这个点的权值,并且会使其相邻点和距离为2且中间隔着未被删除的点的点权值加1,现在选一个点开始删 ...

  5. Codeforces Round #408 (Div. 2) 题解【ABCDE】

    A - Buying A House 题意:给你n个房间,妹子住在第m个房间,你有k块钱,你想买一个离妹子最近的房间.其中相邻的房间之间距离为10,a[i]=0表示已经被别人买了. 题解:扫一遍更新答 ...

  6. Codeforces Round #408 (Div. 2) C

    Description Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. ...

  7. 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 ...

  8. Codeforces Round #408 (Div. 2) D - Police Stations

    地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...

  9. 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 ...

随机推荐

  1. 转:Python yield 使用浅析 from IBM Developer

    评注:没有看懂. 转: https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ Python yield 使用浅析 初 ...

  2. 改动Xmodem/Zmodem上传下载路径

    SecureCRT能够使用Xmodem/Zmodem方便的上传和下载文件. 在Session ptions =>Xmodem/Zmodem => Directories中设置   选项=& ...

  3. HDU1114 Piggy-Bank 【全然背包】

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  4. (转)我在北京工作这几年 – 一个软件工程师的反省

    我于2007年来到北京,在北京工作这些年,先后在NEC.风行.百度几家公司担任软件工程师的职务.NEC是一家具有百年历史的传统日企,在知春路的分公司叫日电电子,我们部门主要从事机顶盒.数字电视上嵌入式 ...

  5. iOS UILabel换行同时修改字体大小颜色

    UIButton *onlyPriceBtn = [UIButton buttonWithType:UIButtonTypeCustom]; onlyPriceBtn.layer.borderColo ...

  6. 【Android开发-8】生命周期,Activity中打开另外一个Activity

    前言:生命中有很多人陪伴自己走过一生中的某段旅程,仅仅是有些人仅仅是某阶段出现,有些人却陪伴自己非常久.就像小学.中学.高中.大学,那些以前以为会长久拥有的,当经历过天涯各地地忙碌于生活,或如意.或失 ...

  7. git常用知识笔记

    学习资料: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 http://codi ...

  8. app直播原理

    之前写的一系列文章或者小经验一直没有时间去整理放在博客上,今天整理出来,之前是写在作业部落,语法是markdown,点击链接浏览,仅供参考,希望对你有帮助. https://www.zybuluo.c ...

  9. HDU 6068 Classic Quotation KMP+DP

    Classic Quotation Problem Description When online chatting, we can save what somebody said to form h ...

  10. Javascript正则中的exec和match

    分几种情况说明 1.假设re中不是全局的也就是不带g var str = "cat3 hat4"; var re = /\w+\d/; var ex = re.exec(str); ...