The only difference between easy and hard versions is the constraints.
Polycarp has to write a coursework. The coursework consists of mm pages.
Polycarp also has nn cups of coffee. The coffee in the ii-th cup Polycarp has aiaicaffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely(i.e. he cannot split any cup into several days).
Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least).
Let's consider some day of Polycarp's work. Consider Polycarp drinks kk cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are ai1,ai2,…,aikai1,ai2,…,aik. Then the first cup he drinks gives him energy to write ai1ai1 pages of coursework, the second cup gives him energy to write max(0,ai2−1)max(0,ai2−1) pages, the third cup gives him energy to write max(0,ai3−2)max(0,ai3−2) pages, ..., the kk-th cup gives him energy to write max(0,aik−k+1)max(0,aik−k+1) pages.
If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day.
Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible.
Input
The first line of the input contains two integers nn and mm (1≤n≤2⋅1051≤n≤2⋅105, 1≤m≤1091≤m≤109) — the number of cups of coffee and the number of pages in the coursework.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiaiis the caffeine dosage of coffee in the ii-th cup.
Output
If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it.
Examples
Input
5 8 2 3 1 1 2
Output
4
Input
7 10 1 3 4 2 1 4 2
Output
2
Input
5 15 5 5 5 5 5
Output
1
Input
5 16 5 5 5 5 5
Output
2
Input
5 26 5 5 5 5 5
Output
-1
题意:n杯咖啡,m页论文,每杯咖啡有自己的咖啡因含量,对应着喝完这杯咖啡能写页多少论文。在一天内喝第一杯咖啡,效果等于原值(ai的咖啡因量能肝ai页论文),每多喝一杯,这杯咖啡能产生的效果就会减一。问最少多少天完成论文?
题解:二分枚举天数,含量大的在每一天的先喝即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
int a[200010];
int judge(int mid)
{
ll ans=0;
int mm=mid,cnt=0;
for(int i=n;i>=1;i--)
{
if(mm==0)
{
mm=mid;
cnt++;
}
ans+=max(0,a[i]-cnt);
mm--;
// cout<<ans<<" ";
}
// cout<<endl;
// cout<<mid<<" "<<ans<<endl;
return ans>=m;
}
int main()
{
ll sum=0;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i],sum+=a[i];
sort(a+1,a+1+n);
if(sum<m)
{
cout<<"-1\n";
return 0;
}
int l=1,r=n;
int ans;
while(l<=r)
{
int mid=(r+l)>>1;
if(judge(mid))
{
r=mid-1;
ans=mid;
}
else l=mid+1;
}
cout<<ans<<endl;
return 0;
}