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;
}

 

 

全部评论

相关推荐

04-02 10:09
门头沟学院 Java
用微笑面对困难:这里面问题还是很多的,我也不清楚为啥大家会感觉没啥问题。首先就是全栈开发实习9个月的内容都没有java实习生的内容多,1整个技术栈没看出太核心和难点的内容,感觉好像被拉过去打杂了,而且全栈基本上很容易被毙。里面能问的bug是在太多了比如L:继承 BaseMapper 可直接使用内置方法’。请问你的 BaseMapper 是如何扫描实体类注解如果瞬时产生 100 个上传任务,MySQL 的索引设计是否会有瓶颈?你做过分库分表或者索引优化吗?全栈的内容可以针对动态难点去搞,技能特长写在下面吧,你写了这么多技能,项目和实习体现了多少?你可以在项目里多做文章然后把这个放下去,从大致来看实习不算太水,有含金量你也要写上内容针对哨兵里面的节点变化能问出一万个问题,这个很容易就爆了。
提前批简历挂麻了怎么办
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务