Codeforce 1118 D2 Coffee and Coursework (Hard Version)

D2. 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.

 

题意:n杯咖啡,m页论文,每杯咖啡有自己的咖啡因含量~,对应着喝完这杯咖啡能写页多少论文。在一天内喝第一杯咖啡,效果等于原值(ai的咖啡因量能肝ai页论文),每多喝一杯,这杯咖啡能产生的效果就会减一。问最少多少天完成论文?

二分

贪心时优先天数,每天的第i杯咖啡

#include<bits/stdc++.h>
using namespace std;
int a[200005];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    long long p=0;
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]),p+=a[i];
    sort(a,a+n,greater<int>());
    if(p<m)
    {
        printf("-1");
        return 0;
    }

    int l=1,r=n;
    while(r-l>1)
    {
        int day=(l+r)/2;
        long long sum=0;
        int u=0;
        int cnt=0;
        for(int i=0; i<n; i++)
        {
            if(a[i]>u)
            {
                sum+=a[i]-u;
                cnt++;
            }
            else
                break;
            if(cnt==day)
                cnt=0,u++;
        }
        if(sum>=m)
        {
            r=day;
        }
        else
        {
            l=day;
        }
    }
    int day=l;
    long long sum=0;
    int u=0;
    int cnt=0;
    for(int i=0; i<n; i++)
    {
        if(a[i]>u)
        {
            sum+=a[i]-u;
            cnt++;
        }
        else
            break;
        if(cnt==day)
            cnt=0,u++;
    }
    if(sum>=m)
    {
        r=day;
    }
    printf("%d\n",r);
    return 0;
}

 

 

全部评论

相关推荐

其实本来打算等lastday的时候再写的,但是现在提笔写下这篇总结完全是出于自己的想法,今天上午自己被学校发的签到吵醒时才突然想明白了很多事情,遂决定写下本文进行总结,虽然现在顶多算2.5个月,但也大差不差喵。回看这段时间的日常实习,我的关键词是:遗憾,焦虑。当然也有快乐的时候,不过大部分时间都是前面这两种情绪主导。为了避免后人再次踩坑,我将在本文详细解释我遇到的困难&nbsp;+&nbsp;产生的原因&nbsp;+&nbsp;应对的措施。同时总结新人实习时除了业务本身,还有如何处理生活与工作上的平衡,调控自身的情绪,让自己恢复到最好的工作状态。本文不会教你实习怎么去做产出,因为有产出的前提是你的心态足够健康,且在工作之余还有时间去...
wuwuwuoow:你的经历跟挺像,但我实力远没你强,现在只能干外包。但解决焦虑这块我应该比你更有经验,因为我曾经也非常迷茫和焦虑: 1.规律作息。无论节假日,都必须在同一时间点睡觉,同一时间点起床。放假睡的多,工作睡的少,这就是典型的作息不规律。将直接干扰前额叶皮层功能,导致情绪波动(易怒、焦虑)。无论上班还是周末,我都是 11:30 睡,7 点起床。7.5h 睡眠,完全足够了。 2.运动。缓解压力,强身健体,提高免疫力。不要觉得每天没有时间锻炼,都是懒惰的借口。 3.冥想。长期练习会增厚前额叶皮层(理性决策区),缩小杏仁核体积(减少情绪过敏反应,核心),增强情绪调控能力。 方法很简单,任何时候都能做。就是闭上眼睛,只专注自己的呼吸,不去想其他任何事情。你可以尝试一下,你会发现非常难只专注呼吸,会有大量的想法涌现出来(什么走马灯),不要去压抑它们,而是放平心态,把注意力继续放在呼吸上面。 而且最重要的是,冥想让你学会“活在当下”。因为处于冥想的你,除了专注呼吸你还能做什么呢?你什么都做不了。生活也是这样,我们无法改变过去,无法预知未来会发生什么,我们能做的只有手头的事情,除此之外什么都别想,因为你无法去改变它们。 4.工作与生活分离。工作不是生活的全部,生活可不是只有工作。像我放假的时候,从不带电脑回去。放假该玩就玩吧。 上面要是都能做到,其实完全解决不了你工作上的问题,完不成的需求还是完不成,面试该挂还是得挂。不过呢,当你再次迷茫,再次焦虑的时候,你会发现,诶,还好,没这么难受。比如面试挂了,可能以前的你会感觉非常难受。但如果你做到以上 4 点,你还是会难受的,但其实又没这么难受,可能你会这样想:既然挂了我还能怎么样?这公司不要我,有的是公司要我!
投递腾讯等公司10个岗位 >
点赞 评论 收藏
分享
03-28 19:11
铜陵学院 C++
有礼貌的山羊追赶太阳:太典了,连笔试都没有开始就因为HC满了而结束了,而且还卡你不让你再投其他部门的。
点赞 评论 收藏
分享
咦哟,从去年八月份开始长跑,两处实习转正都失败了,风雨飘摇,终于拿到offer了更新一下面试记录:秋招:多部门反复面试然后挂掉然后复活,具体问了啥已经忘了,只是被反复煎炸,直至焦香😋春招:base北京抖音hr打来电话说再次复活,准备面试,gogogo北京抖音一面:六道笔试题:1.promise顺序2.定义域问题3.flat展开4.并发请求5.岛屿数量算法(力扣)深度,广度都写6.忘记了,好像也是算法,难度中等其他问题多是框架底层设计,实习项目重难点~~~秒过😇北京抖音二面:三道笔试题:(为什么只有三道是因为第三道没做出来,卡住了)1.中等难度算法(忘记啥题了,应该是个数组的)2.认识js的继承本质(手写继承模式,深入js的面相对象开发)3.手写vue的响应式(卡在了watch,导致挂掉)---后知后觉是我的注册副作用函数写得有问题,有点紧张了其他题目多是项目拷打,项目亮点,对实习项目的贡献~~~第二天,挂,but立马复活转战深圳客服当天约面深圳客服一面:六道笔试题,由于面过太多次字节,面试官叫我直接写,不用讲,快些写完😋,具体都是些继承,深拷贝(注意对数组对象分开处理,深层次对象,循环引用),加中等难度算法题~~~秒过深圳客服二面:口诉八股大战:大概囊括网络,浏览器渲染原理,动画优化,时间循环,任务队列等等(你能想到的简单八股通通拉出来鞭尸😋)算法题:笔试题6道:1:找出数组内重复的数,arr[0]-arr[n]内的数大小为[1-n],例如[1,2,2,3,3]返回[2,3],要求o(n),且不使用任何额外空间(做到了o(n),空间方面欠佳,给面试官说进入下一题,做不来了)2:原滋原味的继承(所以继承真滴很重要)3:力扣股票购买时机难度中等其他滴也忘记了,因为拿到offer后鼠鼠一下子就落地了,脑子自动过滤掉可能会攻击鼠鼠的记忆😷~~~秒过深圳客服三面:项目大战参与战斗的人员有:成员1:表单封装及其底层原理,使用成本的优化,声明式表单成员2:公司内部库生命周期管理成员3:第三方库和内部库冲突如何源码断点调试并打补丁解决成员4:埋点的艺术成员5:线上项目捷报频传如何查出内鬼成员6:大文件分片的风流趣事成员7:设计模式对对碰成员8:我构建hooks应对经理的新增的小需求的故事可能项目回答的比较流利,笔试题3道,都很简单,相信大家应该都可以手拿把掐😇~~~过过过无hr面后续煎熬等待几天直接hr打电话发offer了,希望大家也可以拿到自己心仪的offer
法力无边年:牛哇,你真是准备得充分,我对你没有嫉妒,都是实打实付出
查看19道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务