Coffee and Coursework (Hard Version)
time limit per test

2.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 aiai caffeine 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 aiai is 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

Copy
5 8
2 3 1 1 2
Output

Copy
4
Input

Copy
7 10
1 3 4 2 1 4 2
Output

Copy
2
Input

Copy
5 15
5 5 5 5 5
Output

Copy
1
Input

Copy
5 16
5 5 5 5 5
Output

Copy
2
Input

Copy
5 26
5 5 5 5 5
Output

Copy
-1
Note

In the first example Polycarp can drink fourth cup during first day (and write 11 page), first and second cups during second day (and write 2+(3−1)=42+(3−1)=4 pages), fifth cup during the third day (and write 22 pages) and third cup during the fourth day (and write 11 page) so the answer is 44 . It is obvious that there is no way to write the coursework in three or less days.

In the second example Polycarp can drink third, fourth and second cups during first day (and write 4+(2−1)+(3−2)=64+(2−1)+(3−2)=6 pages) and sixth cup during second day (and write 44 pages) so the answer is 22 . It is obvious that Polycarp cannot write the whole coursework in one day in this test.

In the third example Polycarp can drink all cups of coffee during first day and write 5+(5−1)+(5−2)+(5−3)+(5−4)=155+(5−1)+(5−2)+(5−3)+(5−4)=15 pages of coursework.

In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5+(5−1)+(5−2)+(5−3)=145+(5−1)+(5−2)+(5−3)=14 pages of coursework and during second day he will write 55 pages of coursework. This is enough to complete it.

In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.

分析:大数据,既然已经使用sort排序了,直接二分就可以了鸭

 #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
int n,m;
vector<int> a; bool can(int i){
ll sum=;
for( int j=; j<n; j++ ){
sum+=max(a[j]-j/i,);
}
if(sum>=m)return true;
else return false;
} int main(int argc, char const *argv[])
{
cin>>n>>m;
a=vector<int>(n); for( int i=; i<n; i++ ){
cin>>a[i];
} sort(a.rbegin(),a.rend()); int l=,r=n;
while(l<=r){
ll mid=(l+r) >> ;
if(can(mid)) r=mid-;
else l=mid+;
} if(can(l)) cout<<l<<endl;
// else if(can(r)) cout<<r<<endl;
else{
cout<<-<<endl;
} return ;
}

Coffee and Coursework (Hard Version)的更多相关文章

  1. Coffee and Coursework (Easy version)

    Coffee and Coursework (Easy version) time limit per test 1 second memory limit per test 256 megabyte ...

  2. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】

    任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...

  3. Codeforces Round #540 (Div. 3)--1118D2 - Coffee and Coursework (Hard Version)

    https://codeforces.com/contest/1118/problem/D2 和easy version的主要区别是,数据增加了. easy version采用的是线性查找,效率低 在 ...

  4. Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)

    https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...

  5. Codeforces - 1118D2 - Coffee and Coursework (Hard Version) - 二分

    https://codeforces.com/problemset/problem/1118/D2 也是很好想的一个二分啦. 验证m的可行性的时候,肯定是把最多咖啡因的咖啡先尽可能平均分到每一天,因为 ...

  6. 【Codeforces 1118D1】Coffee and Coursework (Easy version)

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 从小到大枚举天数. 然后贪心地,从大到小分配a[i]到各个天当中. a[n]分配到第1天,a[n-1]分配到第2天,...然后a[n-x]又分 ...

  7. Codeforces Round #540 (Div. 3) D2. Coffee and Coursework (Hard Version) (二分,贪心)

    题意:有\(n\)个数,每次可以选\(k(1\le k\le n)\)个数,并且得到\(a_1+max(0,a_2-1)+max(0,a_3-2)+...+max(0,a_k-k+1)\)的贡献,问最 ...

  8. Codeforces Round #540 (Div. 3) A,B,C,D2,E,F1

    A. Water Buying 链接:http://codeforces.com/contest/1118/problem/A 实现代码: #include<bits/stdc++.h> ...

  9. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

随机推荐

  1. SpringBoot无废话入门03:SpringMVC支持

    1.默认配置 Springboot对于路径的默认位置为: spring.resources.static-locations=classpath:/META-INF/resources/,classp ...

  2. 13、spark-submit

               

  3. android:如何通过chrome远程调试APP中的webView的h5代码

    今天出现一个问题,在老板的Mate9 Pro上,我们APP的所有H5页面都是一片空白,但是在其他手机上都是好的,那么我们就怀疑是h5报错了,但是到底是什么错,无法得知,所以就想要可以像在pc的chro ...

  4. elementUI tree组件获取当前选择所有选中(check)和半选中(indeterminate)的节点

    网上查了半天,一大堆都说要改源码的,最后发现有方法不用改源码的 获取方法如下 this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHa ...

  5. 如何永久激活(破解) IntelliJ IDEA 2018.2

    1.去官网下载并安装 idea 地址:https://www.jetbrains.com/idea/download 文件有点大,耐心等待一会儿. 2.下载破解(crack) jar 包 地址 htt ...

  6. 信用评分及模型原理解析(以P2P网贷为例)

    本博文将针对消费贷款领域的信用评分及其模型进行相关研究探讨.虽然人人都可以通过对借款方在Lending Club(国外最大的P2P网站)和Prosper上的历史借贷数据进行分析,但我相信,了解消费信贷 ...

  7. 使用MiniProfiler调试ASP.NET web api项目性能

    本质上,集成Miniprofiler可以分解为三个问题: 怎样监测一个WebApi项目的性能. 将性能分析监测信息从后端发送到UI. 在UI显示分析监测结果. 首先安装Miniprofiler,Min ...

  8. 使用viewport中的vm来适配移动端页面

    前言 作为一个小前端,经常要和H5打交道,这就面临着不同终端的适配问题. Flexible方案通过Hack手段来根据设备的dpr值相应改变<meta>标签中viewport的值,给我更贴切 ...

  9. Protobuf3 序列化

    在message_lite.h中定义了SerializeToString ,SerializeToArray ,SerializeToCodedStream ,SerializeToZeroCopyS ...

  10. vue2.0 技巧汇总

    /** * Created by */ export default { trim: (str) => { //删除左右两端的空格 return str.replace(/(^\s*)|(\s* ...