码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #408 (Div. 2) C

时间:2017-04-12 23:25:34      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:main   最小值   ace   logs   integer   比较   equal   eal   res   

Description

Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks.

技术分享

There are n banks, numbered from 1 to n. There are also n?-?1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength ai.

Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.

When a bank is hacked, it becomes offline (and no longer online), and other banks that areneighboring or semi-neighboring to it have their strengths increased by 1.

To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met:

  1. Bank x is online. That is, bank x is not hacked yet.
  2. Bank x is neighboring to some offline bank.
  3. The strength of bank x is less than or equal to the strength of Inzane‘s computer.

Determine the minimum strength of the computer Inzane needs to hack all the banks.

Input

The first line contains one integer n (1?≤?n?≤?3·105) — the total number of banks.

The second line contains n integers a1,?a2,?...,?an (?-?109?≤?ai?≤?109) — the strengths of the banks.

Each of the next n?-?1 lines contains two integers ui and vi (1?≤?ui,?vi?≤?nui?≠?vi) — meaning that there is a wire directly connecting banks ui and vi.

It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.

Output

Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.

Examples
input
5
1 2 3 4 5
1 2
2 3
3 4
4 5
output
5
input
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
output
93
input
5
1 2 7 6 7
1 5
5 3
3 4
2 4
output
8
Note

In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how:

  • Initially, strengths of the banks are [1,?2,?3,?4,?5].
  • He hacks bank 5, then strengths of the banks become [1,?2,?4,?5,??-?].
  • He hacks bank 4, then strengths of the banks become [1,?3,?5,??-?,??-?].
  • He hacks bank 3, then strengths of the banks become [2,?4,??-?,??-?,??-?].
  • He hacks bank 2, then strengths of the banks become [3,??-?,??-?,??-?,??-?].
  • He completes his goal by hacking bank 1.

In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93.

题意:我们可以攻击比自己能力小的,攻击成功后,与它相邻的或者间接相邻防御的会+1,问全部攻击成功最小要多少攻击力

题意:因为是求最小值,想到二分,我们把最小防御和最大防御求出来。接下来就是判断能不能符合条件了,我们根据模拟发现,除了相邻的或者间接相邻的,其他都是+2,我们就先将所有防御与中间值比较,大的就return 0,否则就+2

对于每一个+2的点,如果大于中间值,记录个数,再次遍历,到了大于中间值的点,那么说明可以作为开始点,与它相邻的点-1等于中间值,减去个数,如果最后等于0,说明可行

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int len=400000;
 4 long long maxn=-1;
 5 long long minn=(1<<31)-1;
 6 long long x[len];
 7 long long y[len];
 8 long long n,m;
 9 vector<int>q[len];
10 int solve(long long w)
11 {
12     int cnt=0;
13     for(int i=1;i<=n;i++)
14     {
15         y[i]=x[i];
16         //if(x[i]>w) return 0;
17     }
18     for(int i=1;i<=n;i++)
19     {
20         if(x[i]>w) return 0;
21         y[i]+=2;
22         if(y[i]>w)
23         {
24             cnt++;
25         }
26     }
27     if(cnt==0) return 1;
28     if(n==1)   return 1;
29     for(int i=1;i<=n;i++)
30     {
31         int ans=cnt;
32         if(y[i]>w)
33         {
34             ans--;//大于x的点作为起点,相邻的点为+1
35         }
36         for(int j=0;j<q[i].size();j++)//遍历相邻的点
37         {
38             int pos=q[i][j];
39             if(pos==i) continue;
40             if(y[pos]-1==w)
41             {
42                 ans--;
43             }
44         }
45         if(ans==0) return 1;
46     }
47     return 0;
48 }
49 
50 long long answer;
51 int main()
52 {
53     scanf("%lld",&n);
54     for(int i=1;i<=n;i++)
55     {
56         scanf("%lld",&x[i]);
57         maxn=max(x[i],maxn);
58         minn=min(x[i],minn);
59     }
60     for(int i=1;i<=n-1;i++)
61     {
62         long long u,v;
63         scanf("%lld%lld",&u,&v);
64         q[u].push_back(v);
65         q[v].push_back(u);
66     }
67     if(n==1)
68     {
69         printf("%lld\n",x[1]);
70         return 0;
71     }
72     long long ans;
73     maxn=maxn+2;
74     while(minn<=maxn)
75     {
76         long long mid=(maxn+minn)/2;
77         if(solve(mid))
78         {
79             ans=mid;
80             maxn=mid-1;
81         }
82         else
83         {
84             minn=mid+1;
85         }
86     }
87     printf("%lld\n", ans);
88     return 0;
89 }

 

Codeforces Round #408 (Div. 2) C

标签:main   最小值   ace   logs   integer   比较   equal   eal   res   

原文地址:http://www.cnblogs.com/yinghualuowu/p/6701631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!