# Codeforces 672D Robin Hood

128人阅读 评论(0)

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod %100000007
const int maxn = 500009;
LL n,k;
LL a[maxn];

void init()
{
cin>>n>>k;
rep(i,1,n) scanf("%I64d",&a[i]);
}

int check( LL x , int type )
{
LL t1 = 0;
LL t2 = 0;
rep(i,1,n)
if ( type == 0 )
{
if ( a[i] <= x )
{
t1 += x - a[i];
t2++;
}
}
else
{
if ( a[i] >= x )
{
t1 += a[i] - x;
t2++;
}
}
t2+=t1;
if ( t1 > k ) return -1;
if ( k >= t2 ) return 1;
return 0;
}

void solve()
{
LL S = 0;
LL m1,m2;
LL m,l,r;
LL ml,mr;
LL temp;
ml = mr = a[1];

rep(i,1,n)
{
S += a[i];
if ( a[i] < ml ) ml = a[i];
if ( a[i] > mr ) mr = a[i];
}
m1 = m2 = -1;
l = ml;
r = S/n;

while( l < r )
{
m = ( l + r )>>1;
temp = check(m , 0);
if (temp == -1) r = m - 1;
else if ( temp == 1 ) l = m + 1;
else
{
m1 = m;
break;
}
}
if (m1==-1) m1 = l;

l = (S+n-1)/n;
r = mr;
while( l < r )
{
m = ( l + r ) >> 1;
temp = check(m , 1);
if (temp == -1) l = m + 1;
else if ( temp == 1 ) r = m - 1;
else
{
m2 = m;
break;
}
}
if (m2==-1) m2 = l;
printf("%I64d\n",m2-m1);
}
int main()
{
init();
solve();
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：30651次
• 积分：2740
• 等级：
• 排名：第13136名
• 原创：254篇
• 转载：1篇
• 译文：0篇
• 评论：36条
评论排行
最新评论