【题目链接】: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的更多相关文章

  1. 【codeforces 796C】Bank Hacking(用一些技巧来代替multiset)

    [题目链接]:http://codeforces.com/contest/796/problem/C [题意] 给你n个节点,你一开始选择一个节点,然后打掉它,然后与被打掉过的节点相连的节点才能被 打 ...

  2. 【codeforces 794A】Bank Robbery

    [题目链接]:http://codeforces.com/contest/794/problem/A [题意] 每个位置上可能有物品(>=1)或是没物品 你一开始在某一个位置b; 然后你最左可以 ...

  3. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  4. 【codeforces 755C】PolandBall and Forest

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【codeforces 750F】New Year and Finding Roots

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  7. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  8. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  9. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

随机推荐

  1. cf 749D Leaving Auction

    Leaving Auction time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  2. 用JavaScript实现歌词滚动播放

    各种音乐播放器上都有一个自动滚动播放歌词的功能,那么这个功能用JavaScript怎么实现呢?请看下文. 一般音乐播放器使用的歌词格式都是lrc,为了方便处理,我们这里使用XML格式的歌词.介绍一个网 ...

  3. Spinner的样式大致简介

    Spinner Spinner 是一个列表选择框,会在用户选择后,展示一个列表供用户进行选择.Spinner是ViewGroup的间接子类,它和其他的Android控件一样,数据需要使用Adapter ...

  4. Unicode ,UTF-8,assic, gbk, latin1编码 的区别

    1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte). ...

  5. BOM 标记

    BOM 是 Byte Order Mark 的简称,即字节序标记.用于标记文本流: 表示文本流的字节顺序,是小端序(little-endian)还是大端序(big-endian); 表示文本流是 Un ...

  6. PHP流程控制语句(if,foreach,break......)

    背景:PHP程序中,必不可少的要用到流程控制语句.这次对于流程控制语句进行一些总结. 条件控制语句和循环控制语句是两种基本的语法结构,它们都是用来控制程序执行流程.也是构成程序的主要语法基础. 一.程 ...

  7. nodejs全局安装路径的位置

    一般nodejs安装在默认的C盘,如果不知道安装在哪里,可以打开控制面板-系统和安全-系统-高级配置中找到 所谓全局安装: 是指安装在node中node_module的根目录里,可以在电脑的任何位置调 ...

  8. javascript DOM编程艺术 第10章问题记录

    为什么moveElement函数调用时,必须加字符串的拼接符 var repeat = "moveElement('"+elementID+"',"+final ...

  9. HTML地理位置定位

    最近公司项目需要做一个类似微信朋友圈的互动交友功能,需要显示用户位置信息,因此在网上查了部分资料,记下demo供以后查看学习:(用到了百度api来实现定位功能) <!DOCTYPE html&g ...

  10. 在python中调用js或者nodejs

    在python中调用js或者nodejs要使用PyExecJs第三方包. pip install pyexecjs 示例代码 >>> import execjs >>&g ...