Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)
题目链接: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)的更多相关文章
- Codeforces Round #408 (Div. 2) C.Bank Hacking(二分)
传送门 题意 给出n个银行,银行之间总共有n-1条边,定义i与j有边相连为neighboring,i到j,j到k有边,则定义i到k的关系为semi- neighboring, 每家银行hack的难度为 ...
- 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 #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- Codeforces Round #556 (Div. 2) - D. Three Religions(动态规划)
Problem Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 3000 mSec Problem Descripti ...
- Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)
Codeforces Round #521 (Div. 3) E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...
- Codeforces Round #622 (Div. 2) B. Different Rules(数学)
Codeforces Round #622 (Div. 2) B. Different Rules 题意: 你在参加一个比赛,最终按两场分赛的排名之和排名,每场分赛中不存在名次并列,给出参赛人数 n ...
随机推荐
- 解题:SCOI 2005 骑士精神
题面 我把这个当做IDA*的模板题的说,说说我个人对IDA*的理解 IDA*是一个DFS,和A*一样,它也有一个乐观的估价函数.这里这个估价函数是用来限制状态的扩展的,如果当前代价加上乐观的估计都无法 ...
- 【codeforces gym】Increasing Costs
Portal --> Increasing Costs Description 给你一个\(n\)个点无重边无自环的无向连通图,每条边有一个边权,对于每一条边,询问去掉这条边之后有多少个点到\( ...
- css选择器 nth-child
html代码: <div> <p>多云转晴</p> <p>多云转晴</p> <p>多云转晴</p> <p> ...
- 3.Filter和interceptor的区别
https://blog.csdn.net/qq_36411874/article/details/53996873
- Mockserver -MOCO的使用
转自: http://blog.csdn.net/shensky711/article/details/52770686
- JavaScript 时间与时间戳转换
一.获取yyyy-MM-dd hh:mm:ss 格式的时间 function getdate(timeStamp) { if (timeStamp) { var now = new Date(time ...
- ECMAScript6语法检查规范错误信息说明
项目中使用ECMAScript6的时候经查会使用语法检查,下面是常见错误信息的汇总: “Missing semicolon.” : “缺少分号.”, “Use the function form of ...
- 10个好用的JQuery代码片段收集
1.预加载图片 (function($) { var cache = []; // Arguments are image paths relative to the current page. $. ...
- java学习第01天(搭建环境配置)
搭建环境配置分为3个步骤 1.下载并安装 jdk使用1.8.0_151版本,下载地址(包含32位和64位):链接:https://pan.baidu.com/s/1qZau8oO 密码:0t5o 2. ...
- GDB基本用法
基本命令 进入GDB:#gdb test test是要调试的程序,由gcc test.c -g -o test生成.进入后提示符变为(gdb) . 查看源码:(gdb) l 源码会进行行号提示. 如果 ...