题目链接:http://codeforces.com/problemset/problem/796/C

题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻(距离<=2)的银行能量+1,除了第一次外,攻击一家银行需要满足以下条件:

      ①:跟被攻击过后的银行相邻;

      ②:能量低于攻击者

      ③:银行没有被攻击过

题解:可以从题意得知,比如攻击银行i,如果说银行i能量为t,跟银行距离>=2的银行中能量最大的为mx,自身至少所需能量=max(t+1,mx+2),因为其他银行能量最多也只能+2;

   这样,我们只需要遍历1~n家银行找到最少需要的能量就可以了;

   这里我们用了两个c++数据结构,vector和multiset。vector属于<vector>是动态数组,multiset属于<set>会自动将里面的数字按从小到大排好;

   

 #include<iostream>
#include<cstdio>
#include<set>
#include<vector>
using namespace std;
const int N=3e5+;
vector<int>v[N];
multiset<int>ms; int main(){
int n,a,b;
int arr[N];
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",arr+i);
ms.insert(arr[i]);
}
for(int i=;i<=n-;i++){
scanf("%d %d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
int ans=<<;
multiset<int>::iterator it;
for(int i=;i<=n;i++){//遍历n家银行,找到需要最少能量的银行
//存下银行i的值后,在ms中删除
int temp=arr[i];
ms.erase(ms.find(arr[i]));
//找到跟i相邻的银行,比较后,在ms中删除
for(int j=;j<v[i].size();j++){
int k=v[i][j];
temp=max(temp,arr[k]+);
ms.erase(ms.find(arr[k]));
}
//比较距离大于2的银行能量最大值+2和temp的大小,保证每个银行都可以攻击
if(!ms.empty()){
it=ms.end();
temp=max(temp,*(--it)+);//由于ms.end()指向最后一位,而不是最后一个元素所以--it;
}
//还原ms中删除的元素
ms.insert(arr[i]);
for(int j=;j<v[i].size();j++){
int k=v[i][j];
ms.insert(arr[k]);
}
ans=min(temp,ans);//找到所有情况中最少的能量
}
printf("%d\n",ans);
}

另外附上一种贪心写法:

C1为mx个数  C2为mx-1个数 
ans=mx 只有当C1=1&&正好有C2个mx-1与mx相连  
ans=mx+1 只有当存在一个点满足 其距离<=1内 mx的个数为C1 
其余情况ans=mx+2

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
const ll inf=1e10;
const int N=2e6+;
ll n,a[N],vis[N],can[N];
vector<int> e[N];
void solve()
{
ll C1=,C2=,mx=-inf,u;
for(int i=;i<=n;i++)
mx=max(a[i],mx);
for(int i=;i<=n;i++)
{
if(a[i]==mx)
C1++,u=i;
else if(a[i]==mx-)
C2++;
}
ll ans=inf;
if(C1==)//
{
int cnt=;
for(int i=;i<e[u].size();i++)
{
int v=e[u][i];
if(a[v]==mx-)
cnt++;
}
if(cnt==C2)
ans=mx;
}
if(ans==inf)
{
for(int i=;i<=n&&ans==inf;i++)//遍历边O(n),找到相邻为1内,有C1个mx
{
ll res=;
if(a[i]==mx)
res++;
for(int j=;j<e[i].size();j++)
{
int v=e[i][j];
if(a[v]==mx)
res++;
}
if(res==C1)
ans=mx+;
}
}
if(ans==inf)
ans=mx+;
cout<<ans<<endl;
}
int main()
{
while(cin>>n)
{
for(int i=;i<=n;i++)
scanf("%I64d",&a[i]),e[i].clear();
int u,v;
for(int i=;i<=n-;i++)
{
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
solve();
}
return ;
}

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

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

    传送门 题意 给出n个银行,银行之间总共有n-1条边,定义i与j有边相连为neighboring,i到j,j到k有边,则定义i到k的关系为semi- neighboring, 每家银行hack的难度为 ...

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

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

  3. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  4. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  5. Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)

    Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...

  6. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  7. Codeforces Round #556 (Div. 2) - D. Three Religions(动态规划)

    Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 3000 mSec Problem Descripti ...

  8. Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)

    Codeforces Round #521 (Div. 3)  E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...

  9. Codeforces Round #622 (Div. 2) B. Different Rules(数学)

    Codeforces Round #622 (Div. 2) B. Different Rules 题意: 你在参加一个比赛,最终按两场分赛的排名之和排名,每场分赛中不存在名次并列,给出参赛人数 n ...

随机推荐

  1. @Html.DropDownListFor默认选中项

    http://q.cnblogs.com/q/73902/ 项目使用mvc4,给dropDownList指定默认值未选中 页面代码是: 1.未有默认选中值 Html.DropDownListFor(m ...

  2. supervisor 通过图形界面管理

    编辑 supervisor 配置文件, [inet_http_server] ; inet (TCP) server disabled by default port=*:9001 ; ip_addr ...

  3. web项目中classPath指的是哪里?

    classpath可以是SRC下面的路径 但是项目最终编译会到WEB-INF下面,所以有时候WEB-INF下面的classes也可以放配置文件,也可以读取到. 因为最终src都会放到WEB-INF下面 ...

  4. etcd3集群管理

    目录 在什么情况下需要集群的运行时更改 集群运行时更改的操作 更新一个节点 删除一个节点 增加一个节点 节点迁移和灾难恢复 迁移节点 灾难恢复 在什么情况下需要集群的运行时更改 维护和升级多个机器 如 ...

  5. Java泛型底层源码解析-ArrayList,LinkedList,HashSet和HashMap

    声明:以下源代码使用的都是基于JDK1.8_112版本 1. ArrayList源码解析 <1. 集合中存放的依然是对象的引用而不是对象本身,且无法放置原生数据类型,我们需要使用原生数据类型的包 ...

  6. P2306 被yyh虐的mzc

    P2306 被yyh虐的mzc 容量为 \(V\), 有 \(n\) 件物品, 反正直接背包绝对超时 , 每个有重量和价值 \(a_{i}, b_{i}(a_{i}, b_{i} <= 10)\ ...

  7. redhat6下安装centos的yum源

    因为redhat中的yum是收费的,未注册时不允许使用的,下面是挂载光盘后的情况,未挂载是没有yum命令.但是下面即便挂载了也是需要验证的 [root@localhost /]# yum instal ...

  8. 【操作记录】Asp.Net Core 的一些基本操作或属性

    用于记录在项目中使用到的方法.属性.操作,持续更新中 .net core 开源地址 图片上传: public async Task<IActionResult> Upload([FromS ...

  9. codevs 1080 线段树练习 CDQ分治

    codevs 1080 线段树练习 http://codevs.cn/problem/1080/  时间限制: 1 s  空间限制: 128000 KB   题目描述 Description 一行N个 ...

  10. Android 利用广播接收器启动服务

    public class MainActivity extends Activity { private Button bt ; protected void onCreate(Bundle save ...