Coffee and Coursework (Hard Version)

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排序了,直接二分就可以了鸭

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 #define ll long long
 6 int n,m;
 7 vector<int> a;
 8
 9 bool can(int i){
10     ll sum=0;
11     for( int j=0; j<n; j++ ){
12         sum+=max(a[j]-j/i,0);
13     }
14     if(sum>=m)return true;
15     else return false;
16 }
17
18 int main(int argc, char const *argv[])
19 {
20     cin>>n>>m;
21     a=vector<int>(n);
22
23     for( int i=0; i<n; i++ ){
24         cin>>a[i];
25     }
26
27     sort(a.rbegin(),a.rend());
28
29     int l=1,r=n;
30     while(r-1>l){
31         ll mid=(l+r) >> 1;
32         if(can(mid)) r=mid;
33         else l=mid;
34     }
35
36     if(can(l)) cout<<l<<endl;
37     else if(can(r)) cout<<r<<endl;
38     else{
39         cout<<-1<<endl;
40     }
41
42     return 0;
43 }

原文地址:https://www.cnblogs.com/Bravewtz/p/10465896.html

时间: 2024-11-09 04:46:18

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

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

Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选一些题来写吧. B. Tanya and Candies 题意: 在n个数中任意删去一个,如果这个数被删去后,剩余数的奇数和以及偶数和相等,那么就定义这个数为"好数".现在问这n个数中有多少个“好数”. 题解: 预处理出奇数前缀和.偶数前缀和,删去一个数后所有的奇数位置和 就为前面的奇数和

3.3-3.9 周记

3.3-3.10 1. NIM游戏 百度链接:https://baike.baidu.com/item/Nim%E6%B8%B8%E6%88%8F/6737105?fr=aladdin 定义: P局面:先手必败 N局面:先手必胜 P局面的所有子局面都是N局面.N局面的子局面中必有一个是P局面 性质:\(a_1 \ xor \ a_2 \ xor \cdots xor \ a_n = 0\) 则为P局面 证明: 若某个局面异或结果大于0,则一定存在一个操作使得\(a_1 \ xor \ a_2 \

20190927CF训练

A. Trip For Meal 给定一个三角形,你要在三角形上不断地走n步,可以走回头路,问你最短路是多少 贪心地走当前能走的最短边即可,模拟一百能过 代码: #include <bits/stdc++.h> #define int long long #define sc(a) scanf("%lld",&a) #define scc(a,b) scanf("%lld %lld",&a,&b) #define sccc(a,b

【题解】 CF1290D Coffee Varieties (hard version)

题目链接 题目大意 这是一道交互题 有一个长度为\(n\)的未知序列\(a\)和一个大小为\(k\)的队列\(S\).保证\(1\leqslant k\leqslant n\leqslant 1024\),且\(n,k\)都是\(2\)的次幂. 你可以进行以下两种操作: 询问:选择一个数\(i(1\leqslant i\leqslant n)\),并输出? i 交互程序会先检查\(S\)中是否包含\(a_i\),是则输出Y,否则输出N 然后将\(a_i\)加入队尾,若\(|S|>k\),则弹出队

URAL 1907. Coffee and Buns(数论推导+容斥原理)

1907. Coffee and Buns Time limit: 1.0 second Memory limit: 64 MB Planet Ataraxia is known for its education centers. The people who are expected to take high social positions in future are brought up in conditions of continuous training and supervisi

How to GET a Cup of Coffee

We are used to building distributed systems on top of large middleware platforms like those implementing CORBA, the Web Services protocols stack, J2EE, etc. In this article, we take a different approach, treating the protocols and document formats th

COMP0037 Coursework Investigating Path Planning Algorithms

COMP0037 Coursework 1Term 2, 2019"Path Planning in a Known World"Investigating Path Planning AlgorithmsCOMP0037 Assignment 1Simon Julier ([email protected]), Dan Butters ([email protected]), Julius Sustarevas([email protected])Version: 21st Janu

COMP0004 Coursework Part III

COMP0004 Coursework Part IIIPurpose: To develop an application with a GUI to display data.Submission: There is a single submission deadline for all the coursework in this module: Noon22nd March 2019. You can complete this part at anytime before the d

ECON215 Coursework

ECON215 CourseworkThe ECON215 Coursework Exercise is 100% of the module mark. The deadline for submitting the assignment (online only, see below) is 5pm, Thursday 12th December. All exercises in the ECON215 Coursework Exercise need to be completed us