Codeforces Round #408 (Div. 2) C.Bank Hacking(二分)
传送门
题意
给出n个银行,银行之间总共有n-1条边,定义i与j有边相连为neighboring,i到j,j到k有边,则定义i到k的关系为semi- neighboring,
每家银行hack的难度为a[i],
如果hack了一家银行,会使与它关系为neighboring、semi- 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(二分)的更多相关文章
- 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)C. Bank Hacking(STL)
题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...
- 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)
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) C
Description Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. ...
- 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) 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 ...
随机推荐
- chrome插件网站
chrome插件网站 http://chromecj.com/
- CentOS安装Openfire服务
原文::http://xiao987334176.blog.51cto.com/2202382/979677 系统是全新新安装的系统.版本号是Centos 5.6 x86 同步北京时间 # ntpda ...
- C++简单实现对象引用计数示例(转)
C++简单实现对象引用计数示例 #include <iostream> #include <stdio.h> using namespace std; class String ...
- ios系统铃声调用方法
首先,这里我要说明这里并非真正调用系统内部自带的铃声,由于苹果是不同意开发人员调用的,没有给开发人员接口.假设调用了就无法上线的! 那为什么AppStore里面还有那么多app显示的效果是调用系统的铃 ...
- jquery 获取下拉框 某个text='xxx'的option的属性 非选中 如何获得select被选中option的value和text和......
jquery 获取下拉框 某个text='xxx'的option的属性 非选中 5 jquery 获取下拉框 text='1'的 option 的value 属性值 我写的var t= $(" ...
- mysql关闭skip-grant-tables快速重置mysql密码
如果你忘记了mysql密码几乎是没有什么好办法可以直接修改密码了,但我们可以在my.ini把加上skip-grant-tables,然后重启mysql就不需要密码了,这时我们再修改root密码,最后再 ...
- excel 创建数据有效性及背景颜色
需求:用excel做数据或者表格时经常需要在一列中给出固定的几个进行悬着,这是如果每次键盘输入降低工作效率.如果做成鼠标双击进行选择,则提高很多效率,比如需要给一列填写Pass或Failure时,具体 ...
- Android 通过Application 传递数据
</pre><pre> package com.example.ApplicationTest; import android.app.Application; /** * C ...
- Spring中的AOP(学习笔记)
是什么AOP及实现方式 AOP的基本概念 Schema-base AOP Spring AOP API AspectJ
- Qt & opencv 学习(二)
例子:打开图片并显示.打开图片利用Qt的标准文件对话框,第一步是利用OpenCV创建窗口并显示. 添加一个Button,转到信号槽添加代码: using namespace cv; Mat image ...