Last Edited: Feb 21, 2019 5:45 PM
Tags: binary search,codeforces,greedy
二分枚举天数,从大到小模拟即可
#include<bits/stdc++.h>
using namespace std;
#define fst first
#define sec second
#define sci(num) scanf("%d",&num)
#define scl(num) scanf("%lld",&num)
#define mem(a,b) memset(a,b,sizeof a)
#define cpy(a,b) memcopy(a,b,sizeof b)
typedef long long LL;
typedef pair<int,int> P;
const int MAX_N = 2e5 + 100;
LL nums[MAX_N];
LL N,M;
//LL sum[MAX_N];
LL sub = 0;
bool check(LL days) {
LL t = 0;
LL ans = 0;
bool flag =true;
for (int i = 1,k;i <= N && flag ;i += days) {
for (k = 0;k < days && i + k <= N;k++) {
if (nums[i + k] > t) {
ans += nums[i + k] - t;
} else {
flag = false;
break;
}
}
t++;
}
if (ans >= M) return true;
else return false;
}
bool cmp(LL& a,LL& b) {
return a > b;
}
int main() {
//ios::sync_with_stdio(false); cin.tie(0);
scl(N); scl(M);
for (int i =1;i <= N;i++)
scl(nums[i]);
sort(nums+1,nums+N+1,cmp);
// for (int i = 1;i <= N;i++) {
// sum[i] += sum[i - 1] + nums[i];
// }
LL l = 1,r = 1e9 + 100;
LL ans = -1;
while (l <= r) {
LL mid =(l + r) >>1;
if (check(mid)) {
if (ans == -1) ans = mid;
else ans = min(ans,mid) ;
r = mid - 1;
} else {
l = mid + 1;
}
}
printf("%lld\n",ans);
return 0;
}