D2. Coffee and Coursework (Hard version) (二分 贪心)

D1. Coffee and Coursework (Hard version)

The only difference between easy and hard versions is the constraints.

Polycarp has to write a coursework. The coursework consists of m pages.

Polycarp also has n cups of coffee. The coffee in the i-th cup has ai 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 usually being written in a single day (in a perfect world of Berland, at least). Some of them require multiple days of hard work.

Let’s consider some day of Polycarp’s work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are ai1,ai2,…,aik. Then the first cup he drinks gives him energy to write ai1 pages of coursework, the second cup gives him energy to write max(0,ai2−1) pages, the third cup gives him energy to write max(0,ai3−2) pages, …, the k-th cup gives him energy to write 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 n and m (1≤n≤100, 1≤m≤104) — the number of cups of coffee and the number of pages in the coursework.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤100), where ai is the caffeine dosage of coffee in the i-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

inputCopy
5 8
2 3 1 1 2
outputCopy
4
inputCopy
7 10
1 3 4 2 1 4 2
outputCopy
2
inputCopy
5 15
5 5 5 5 5
outputCopy
1
inputCopy
5 16
5 5 5 5 5
outputCopy
2
inputCopy
5 26
5 5 5 5 5
outputCopy
-1

Hint

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

In the second example Polycarp can drink third, fourth and second cups during first day (and write 4+(2−1)+(3−2)=6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. 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)=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)=14 pages of coursework and during second day he will write 5 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页论文,每杯咖啡有自己的咖啡因含量a_{1}~a_{n},对应着喝完这杯咖啡能写页多少论文。在一天内喝第一杯咖啡,效果等于原值(ai的咖啡因量能肝ai页论文),每多喝一杯,这杯咖啡能产生的效果就会减一。问最少多少天完成论文?

题解:

D1, D2是一道题
这道题目其实并不难…大概是由于英文理解的问题…打比赛时候我理解错误题意了, 现在看了, 一是要仔细理解题意, 二是如果一时想不出来就立马换题
我们二分查找(其实枚举也可以)天数, 对应check函数我们使用贪心的方法枚举一遍, 从大到小或者从小到大都可以, 判断能不能放下即可
注意开long long


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const LL maxn = 2*1e5+10;

int n, m, a[maxn];
bool cmp(int a, int b) {return a>b;}
bool check(int x)
{   //x天内能否写完m页论文
    LL sum = 0;
    for(int i = 1, t = 0; i <= n; i++){
        sum += (a[i]-t);
        if(i%x==0) t++;
        if(a[i] <= t) break; //剪枝
    }
    return sum>=m;
}
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    sort(a+1, a+1+n, cmp);

    int l = 1, r = n+1, ans = 1<<30;
    while(l < r){
        int mid = (l+r)/2;
        if(check(mid))
            r = mid, ans = min(ans, mid);
        else
            l = mid+1;
    }
    if(ans!=1<<30) cout << ans << endl;
    else cout << "-1" <<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/a1097304791/article/details/87874948