【codeforces 796C】Bank Hacking
【题目链接】:http://codeforces.com/contest/796/problem/C
【题意】
给你n个节点,你一开始选择一个节点,然后打掉它,然后与被打掉过的节点相连的节点才能被
打掉,但打掉一个点之后,与它距离小于等于2的节点的权值会增加1(如果中间节点有被打掉的,则不增加,即被切断了);
重复上述步骤,直至所有节点被打掉,能打掉某个节点,你需要满足:你的权值大于等于这个节点的权值.求一开始你的
权值的最小值;
【题解】
枚举每一个点作为第一个打掉的点,除了和它距离为1的那些点权值只递增1之外,其他点都会递增2,用multiset维护除了这个点和距离为1的点之外的点的权值的最大值
用这个点权值a[i],距离为1的那些点的权值(a2[i]+1),以及multiset的(头结点的权值+2)这3个值中的最大值来获取你打掉所有节点且从第i个节点开始打需要的权值的最小值;
对于第一次选取的点只选最大值是有漏洞的如
3
3 2 3
1 2
2 3
选2的话答案更优为4
选3则为5了
【Number Of WA】
4
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 3e5+100;
int a[N],n,ans=21e8;
vector <int> G[N];
multiset<int,greater<int>> myset;
void dele(int x)
{
__typeof(myset.begin()) t;
t = myset.find(x);
myset.erase(t);
}
int main()
{
// freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n)
rei(a[i]),myset.insert(a[i]);
rep1(i,1,n-1)
{
int x,y;
rei(x),rei(y);
G[x].ps(y),G[y].ps(x);
}
rep1(i,1,n)
{
dele(a[i]);
int ma = a[i];
for (int y:G[i])
{
dele(a[y]);
ma = max(ma,a[y]+1);
}
if (!myset.empty())
ma = max(ma,*myset.begin()+2);
for (int y:G[i])
myset.insert(a[y]);
myset.insert(a[i]);
ans = min(ans,ma);
}
printf("%d\n",ans);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 796C】Bank Hacking的更多相关文章
- 【codeforces 796C】Bank Hacking(用一些技巧来代替multiset)
[题目链接]:http://codeforces.com/contest/796/problem/C [题意] 给你n个节点,你一开始选择一个节点,然后打掉它,然后与被打掉过的节点相连的节点才能被 打 ...
- 【codeforces 794A】Bank Robbery
[题目链接]:http://codeforces.com/contest/794/problem/A [题意] 每个位置上可能有物品(>=1)或是没物品 你一开始在某一个位置b; 然后你最左可以 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 755C】PolandBall and Forest
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 750F】New Year and Finding Roots
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
随机推荐
- java四舍五入保留几位小数
double d = 3.1415926; String result = String.format("%.2f", d); // %.2f %. 表示 小数点前任意位数 2 表 ...
- bzoj1690
二分+分数规划+dfs判环 跟1486很像,但是我忘记怎么判环了, 我们可以写一个dfs,如果当前节点的距离小于更新的距离,而且这个点已经在当前访问过了,那么就是有环了,如果没有访问过就继续dfs,每 ...
- bzoj4889
http://www.lydsy.com/JudgeOnline/problem.php?id=4889 人傻常数大 bzoj上跑不过 洛谷上能过两到三个点 我写的是树套树啊 怎么跑的比分块还慢 每次 ...
- C# 工厂单例
public class BusinessFactory { private static BusinessFactory instance = null; pri ...
- Sum It Up -- 深搜 ---较难
每一行都是一组测试案例 第一个数字 表示总和 第二个数字表示 一共有几个可用数据 现在 按照从小到大的顺序 输出 那些数字中若干数字之和为总和的 信息 /. 很好很明显的 遍历痕迹 , ...
- 卸载Mysql connect 6.9.9
我们在卸载MySQL的时候,会发现有一个名为“Connector Net X.X.X”(如:Connector Net 6.9.9)软件总是卸载不成功,下面我们来看看解决方法:1. 在C盘的目录下,有 ...
- easyui datagrid 高度布局自适应
最近在把以前写的一个项目改成用easyui做前端.过程中遇到了不少问题.其中一个就是datagrid不能很好的布局.想了好多办法都有局限.最后想到会不会是布局(easyui-layout)的问题,经过 ...
- Unity学习-地形的设置(五)
添加地形游戏对象 [Hierarchy-Create-Terrain] 为了看的看清楚,在添加一个平行光 [Hierarchy-Create-Direction light] 导入地形包 [Asset ...
- Caffe:导入caffePython-PyQt failed
在另一台电脑上使用caffe python版本,显示 Backend Qt5Agg is interactive backend. Turning interactive mode on. ...
- 文件下载之ServletOutputStream
使用response.getOutputStream可以获取ServletOutputStream,从而实现向页面发送流数据.但是需要注意的是,不能使用ajax进行请求,因为这样页面不会有任何反应,可 ...