Coffee and Coursework (Hard Version)
2.5 seconds
256 megabytes
standard input
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.
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.
If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it.
5 8
2 3 1 1 2
4
7 10
1 3 4 2 1 4 2
2
5 15
5 5 5 5 5
1
5 16
5 5 5 5 5
2
5 26
5 5 5 5 5
-1
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)的更多相关文章
- Coffee and Coursework (Easy version)
Coffee and Coursework (Easy version) time limit per test 1 second memory limit per test 256 megabyte ...
- 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 ...
- Codeforces Round #540 (Div. 3)--1118D2 - Coffee and Coursework (Hard Version)
https://codeforces.com/contest/1118/problem/D2 和easy version的主要区别是,数据增加了. easy version采用的是线性查找,效率低 在 ...
- Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)
https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...
- Codeforces - 1118D2 - Coffee and Coursework (Hard Version) - 二分
https://codeforces.com/problemset/problem/1118/D2 也是很好想的一个二分啦. 验证m的可行性的时候,肯定是把最多咖啡因的咖啡先尽可能平均分到每一天,因为 ...
- 【Codeforces 1118D1】Coffee and Coursework (Easy version)
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 从小到大枚举天数. 然后贪心地,从大到小分配a[i]到各个天当中. a[n]分配到第1天,a[n-1]分配到第2天,...然后a[n-x]又分 ...
- 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)\)的贡献,问最 ...
- 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> ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
随机推荐
- selenium之 文件上传所有方法整理总结【转】
本文转自:https://blog.csdn.net/huilan_same/article/details/52439546 文件上传是所有UI自动化测试都要面对的一个头疼问题,今天博主在这里给大家 ...
- Delphi及C++Builder经典图书一览表(持续更新中2018.01.02)
序号 书名 原版书名 作者 译者 出版社 页数 年代 定价 备注 1 C++Builder 5程序设计大全 C++Builder 5 Developer's Guide Jarrod Hollingw ...
- java 根据系统日期获取前一天、后一天时间(根据初始日期推算出期望(向前/向后)日期)
1.情景展示 java 根据系统当前日期获取前一天日期.后一天日期,或者根据初始日期推算出期望(向前/向后)日期. 2.解决方案 导包 import java.text.ParseExcepti ...
- C# 对轻量级(IoC Container)依赖注入Unity的使用
概述 Unity是一个轻量级的可扩展的依赖注入容器,支持构造函数,属性和方法调用注入.Unity可以处理那些从事基于组件的软件工程的开发人员所面对的问题.构建一个成功应用程序的关键是实现非常松散的耦合 ...
- Python 能做什么?
Python 能做什么? Python具有简单.易学.免费.开源.可移植.可扩展.可嵌入.面向对象等优点,我所关注的是网络爬虫方面,2018即将到来,我准备步入python的世界. Hello,wor ...
- Winscp使用sudo user登录
为了安全期间, 一般设置了禁止root用户ssh登录.使用普通用户登录后再sudo获取操作权限. 可为了文件传文件使用winscp,如何使用sudo登录以获取权限传文件呢 先在服务端设置sudo帐号权 ...
- 超简单的okhttp封装工具类(上)
版权声明:转载请注明出处:http://blog.csdn.net/piaomiao8179 https://blog.csdn.net/piaomiao8179/article/details/ ...
- centos6 利用外部的smpt服务器计划任务发送邮件
centos可通过修改配置文件以使用外部SMTP服务器,达到不使用sendmail而用外部的smtp服务器发送邮件的目的, 操作如下: 一.安装mailx与sendmail # yum -y inst ...
- 程序猿必备的10款web前端动画插件一
1.动画SVG框架幻灯片 在幻灯片之间切换时显示动画SVG帧的实验性幻灯片.不同的形状可以用来创建各种风格. 我们想和大家分享一个实验幻灯片.我们的想法是在从一个幻灯片转换到另一张幻灯片时,使SVG帧 ...
- 数据类型表(DELPHI、C++)
delphi整型数据表 Integer -2147483648..2147483647 signed 32-bit Cardinal 0..4294967295 unsigned 32-bit Sho ...