传送门

题意

给出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. 转: CentOS 6 使用 yum 安装MongoDB及服务器端配置

    转: http://www.cnblogs.com/shanyou/archive/2012/07/14/2591838.html CentOS 6 使用 yum 安装MongoDB及服务器端配置   ...

  2. Hash分析

    分析Hash 列表内容 Hash表中的一些原理/概念,及依据这些原理/概念,自己设计一个用来存放/查找数据的Hash表,而且与JDK中的HashMap类进行比較. 我们分一下七个步骤来进行. Hash ...

  3. js跳出循环的方法区别( break, continue, return ) 及 $.each 的(return true 和 return false)

    js编程语法之break语句: break语句会使运行的程序立刻退出包含在最内层的循环或者退出一个switch语句. 由于它是用来退出循环或者switch语句,所以只有当它出现在这些语句时,这种形式的 ...

  4. C#总结复习5(需要进一步复习)

    第十五章 接口 1.接口: C++中允许多继承没有接口的概念.而java与C#中有,因为C#中 是单继承多接口. 所谓的接口,其实和抽象类.方法相似.都只有一个空方法.其本身不可以为基类,但是允许被其 ...

  5. mysql 内置函数大全 mysql内置函数大全

    mysql 内置函数大全 2013年01月15日 19:02:03 阅读数:4698 对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符的ASCII代 ...

  6. 线程池ThreadPoolExecutor分析

    线程池.线程池是什么,说究竟,线程池是处理多线程的一种形式,管理线程的创建,任务的运行,避免了无限创建新的线程带来的资源消耗,可以提高应用的性能.非常多相关操作都是离不开的线程池的,比方android ...

  7. [LeedCode OJ]#28 Implement strStr()

    [ 声明:版权全部,转载请标明出处,请勿用于商业用途.  联系信箱:libin493073668@sina.com] 题目链接:https://leetcode.com/problems/implem ...

  8. 翻译:A Tutorial on the Device Tree (Zynq) -- Part IV

    获取资源信息 内核模块驱动加载之后,就开始把硬件资源管理起来,如读写寄存器.接收中断. 来看看设备树里的一条: xillybus_0: xillybus@50000000 { compatible = ...

  9. 细数AutoLayout以来UIView和UIViewController新增的相关API

    本文转载至 http://www.itjhwd.com/autolayout-uiview-uiviewcontroller-api/ 细数AutoLayout以来UIView和UIViewContr ...

  10. Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心

    C. Hacker, pack your bags!     It's well known that the best way to distract from something is to do ...