传送门

题意

给出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. ruby rails

    http://www.zhihu.com/question/19552402   作者:陈振宇链接:http://www.zhihu.com/question/19552402/answer/1236 ...

  2. 【转载】图说OOP基础(一)

    本文用图形化的形式描述OOP的相关知识.对OOP进行系统化的梳理,以便掌握. 涉及知识点: OOP的相关知识 OOP知识[Object-Orientation Programming 面向对象编程]总 ...

  3. 对JS闭包的理解

    闭包,是JS里很重要的一个概念,也是相对来讲不太容易理解的一个东西,不过即使难理解,我们也要迎难而上啊,嘿嘿,网上有很多文章都在讲闭包,我在看JS设计模式的时候,书里也着重讲了闭包,但是书里官方的的确 ...

  4. mysql使用“.frm”文件恢复表结构

    mysql创建每张表后都会在“mysql安装目录/data/数据库名/”目录下创建一个“表名.frm”文件. 该.frm文件并不能直接打开,但是它可以帮助你恢复你的表结构~~ 具体操作如下: 我现在准 ...

  5. iptables apache2

    Apache2 iptables 安装指令:sudo apt-get install apache2 2.产生的启动和停止文件是:/etc/init.d/apache2 3.启动:sudo apach ...

  6. 设计模式学习笔记——Mediator中介者模式

    将众多对象之间的网状关系转为全部通过一个中间对象间接发生关系,此中间对象为中介者. 看图最直观: 作用不言而喻,就是降低对象之间的耦合度,乃至降低了整个系统的复杂度. 有点象代理模式,更象外观模式:

  7. Xamarin.Android 记事本(二)自定义AlertDialog

    导读 1.自定义一个AlertDialog 2.添加一条数据 正文 记事本应当有一个添加功能,这里我打算在右上角放一个item,然后点击这个item弹出一个对话框,输入名称,点击确定跳转到另一个act ...

  8. webservice client setTimeOut

    一:eclipse生成的client,基于axis client_sub.getOptions().setTimeOutInMilliSeconds(1000*60); client_sub表示一个客 ...

  9. Android IntentService的使用和源代码分析

    引言 Service服务是Android四大组件之中的一个,在Android中有着举足重轻的作用.Service服务是工作的UI线程中,当你的应用须要下载一个文件或者播放音乐等长期处于后台工作而有没有 ...

  10. Deep Learning 31: 不同版本的keras,对同样的代码,得到不同结果的原因总结

    一.疑问 这几天一直纠结于一个问题: 同样的代码,为什么在keras的0.3.3版本中,拟合得比较好,也没有过拟合,验证集准确率一直高于训练准确率. 但是在换到keras的1.2.0版本中的时候,就过 ...