Codeforces 671B/Round #352(div.2) D.Robin Hood 二分

D. Robin Hood

We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.

There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest‘s 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.

After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.

Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn‘t affect the answer.

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) — the number of citizens in Kekoland and the number of days left till Robin Hood‘s retirement.

The second line contains n integers, the i-th of them is ci (1 ≤ ci ≤ 109) — initial wealth of the i-th person.

Output

Print a single line containing the difference between richest and poorest peoples wealth.

Examples

input

4 11 1 4 2

output

2

input

3 12 2 2

output

0

Note

Lets look at how wealth changes through day in the first sample.

  1. [1, 1, 4, 2]
  2. [2, 1, 3, 2] or [1, 2, 3, 2]

So the answer is 3 - 1 = 2

In second sample wealth will remain the same for each person.

题意:给你n个数,每次能从最大的数一个移给最小的数,问k次操作后,最大和最小的数的差。

思路 : 题目可以简化成找最终状态下的最大值和最小值,那么分别进行二分最大值和最小值,此外注意二分最大、最小时的不同之处。

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <string.h>
 5 using namespace std;
 6
 7 const int MAX = 500010;
 8 const int INF = 0x3f3f3f3f;
 9 int a[MAX], n;
10
11 int findmax(int x, int k)
12 {
13     for(int i = 0; i < n; i++)
14     {
15         if(a[i] > x)
16             k-=a[i] - x;
17         if(k < 0)
18             return  0;
19     }
20     return 1;
21 }
22 int findmin(int x, int k)
23 {
24     for(int i = 0; i < n; i++)
25     {
26         if(a[i] < x)
27             k-=x - a[i];
28         if(k < 0)
29             return  0;
30     }
31     return 1;
32 }
33
34 int main()
35 {
36     int k;
37     __int64 sum = 0;
38     int t1, t2;
39     cin >> n >> k;
40     t1 = 0;
41     t2 = INF;
42     for(int i = 0; i < n; i++)
43     {
44         scanf("%d", a + i);
45         sum += a[i];
46         t1 = max(t1, a[i]);
47         t2 = min(t2, a[i]);
48     }
49     int mx = sum / n + (sum%n>0 ?1 : 0);
50     int mi = sum / n;
51
52     int l , r, maxx , minn;
53     /////mi
54     l = t2, r = mi;
55     while(l <= r) //注意等于
56     {
57         int mid = (l + r) >> 1;
58         if(findmin(mid, k))
59         {
60             l = mid + 1;
61             minn =  mid;
62         }
63         else r = mid - 1;
64     }
65     ////ma
66     l = mx, r = t1;
67     while(l < r)
68     {
69         int mid = (l + r) >> 1;
70         if(findmax(mid, k))
71         {
72             r = mid;
73             maxx = max(mid, maxx);
74         }
75         else l = mid + 1;
76     }
77     maxx = (l +  r) >>1;   //结束状态时取
78     printf("%d\n", maxx - minn);
79 }

时间: 2024-11-10 08:20:48

Codeforces 671B/Round #352(div.2) D.Robin Hood 二分的相关文章

Codeforces Round #352 (Div. 2) D. Robin Hood

题目连接请戳此处 题意:n个人每人有一定的财富值ci,每天Robin Hood从最富的人手中取财富1分给最穷的人(取后最穷, 即可以退回),若有多个最穷的人,任选一个给出财富值.问k天后最富的人和最穷的人财富差值为多少. 1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109 1 ≤ ci ≤ 109 分析: 每一天随着财富值的取和给,最穷的人和最富的人都在动态更新. 最后要求的是  (richest-poorest)min,那么  要使这个等式最小,只需求出k天后richest的最小值和po

Codeforces Round #352 (Div. 2) D. Robin Hood (二分法+判断平衡态)

解题思路: 由求最小值和求最大值各自二分. 由l*(a[l+1]-a[l]):求取填 1~l 到a[l+1]的高度需要多少的钱,如果大于剩余的k 则可执行 若否 判断剩余的k是否为l,若否最小值为a[l],否则 为a[l]+k/l; 由(n-r+1)*(a[r]-a[r-1]):求去掉n~n-r+1的这部分需要多少钱,如果大于剩余的k 则执行 若否 判断剩余的k是否为n-r+1,若是最大值为a[n-r+1]-k/(n-r+1); 如果最大值小于最大值,直接printf 如果最小值大于等于最大值,

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform it

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

Codeforces Beta Round #6 (Div. 2 Only) B. President&#39;s Office

题目大意 给出一个n*m的矩阵 ,描述桌子的布局.总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色.问总统有多少个副手. 解题思路 搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量. 题目代码 #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3

Codeforces Round #262 (Div. 2) 460C. Present(二分)

题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little beaver is a beginner programmer, so informatics is his favorite subjec