问题描述
SDUQD 旁边的滨海公园有 x 条长凳。第 i 个长凳上坐着 a_i 个人。这时候又有 y 个人将来到公园,他们将选择坐在某些公园中的长凳上,那么当这 y 个人坐下后,记k = 所有椅子上的人数的最大值,那么k可能的最大值mx和最小值mn分别是多少。
Input
第一行包含一个整数 x (1 <= x <= 100) 表示公园中长椅的数目
第二行包含一个整数 y (1 <= y <= 1000) 表示有 y 个人来到公园
接下来 x 个整数 a_i (1<=a_i<=100),表示初始时公园长椅上坐着的人数
Output
输出 mn 和 mx
Sample input
3
7
1
6
1
Sample output
6 13
样例解释
最初三张椅子的人数分别为 1 6 1
接下来来了7个人。
可能出现的情况为{1 6 8},{1,7,7},…,{8,6,1}
相对应的k分别为8,7,…,8
其中,状态{1,13,1}的k = 13,为mx
状态{4,6,5}和状态{5,6,4}的k = 6,为mn
解题思路
思路题,首先我们可以思考,什么时候出现mx,那肯定是后来的人都坐在了本来人最多的地方。那什么时候出现mn,就是在当前情况下,下一个来的人坐到人最少的凳子上。也就是直到坐的x个凳子人都相等,就开始从第一个开始往后坐。
完整代码
//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int maxn=100+10;
int a[maxn],x,y,mx,mn,sum;
int getint()
{
int x=0,s=1;
char ch=' ';
while(ch<'0' || ch>'9')
{
ch=getchar();
if(ch=='-') s=-1;
}
while(ch>='0' && ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*s;
}
int main()
{
x=getint(); y=getint();
for (int i=1; i<=x; i++){
a[i]=getint();
if(a[i]>mx) mx=a[i];
}
for (int i=1; i<=x; i++) sum+=mx-a[i];
if(sum>=y) mn=mx;
else mn=ceil(((double)(y-sum))/((double)x))+mx;
mx+=y;
printf("%d %d",mn,mx);
return 0;
}