Coffee and Coursework CodeForces - 1118D2 二分+贪心

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 aiaicaffeine 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 aiaiis 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

5 8
2 3 1 1 2

Output

4

Input

7 10
1 3 4 2 1 4 2

Output

2

Input

5 15
5 5 5 5 5

Output

1

Input

5 16
5 5 5 5 5

Output

2

Input

5 26
5 5 5 5 5

Output

-1

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

题解:二分枚举天数,含量大的在每一天的先喝即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
int a[200010];
int judge(int mid)
{
	ll ans=0;
	int mm=mid,cnt=0;
	for(int i=n;i>=1;i--)
	{
		if(mm==0)
		{
			mm=mid;
			cnt++;	
		}	
		ans+=max(0,a[i]-cnt);
		mm--;
	//	cout<<ans<<" ";
	} 
//	cout<<endl;
//	cout<<mid<<" "<<ans<<endl;
	return ans>=m;
}
int main()
{
	ll sum=0;
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i],sum+=a[i];
	sort(a+1,a+1+n);
	if(sum<m)
	{
		cout<<"-1\n";
		return 0;
	}
	int l=1,r=n;
	int ans;
	while(l<=r)
	{
		int mid=(r+l)>>1;
		if(judge(mid))
		{
			r=mid-1;
			ans=mid;
		}
		else l=mid+1;
	}
	cout<<ans<<endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请阅读下面文字并完成这个数据库的ER图 题目背景以及要求 SirenCD is a digital media company that runs several retail stores that sell CDs to customers. In this coursework, you are asked to design a database for SirenCD to help manage the information of artists, albums, media tracks, and media purchases. The final database must be in 3NF and have no M:N relationships. You will also be asked to write a few queries based on your database design. 数据库设计 The retail stores of SirenCD sell many different CD albums. An album can have one or more soundtracks. Each soundtrack has its track name, release date, genre, length and composer. Each album is associated with an artist. Customers can look up information about all artists, such as date of birth, biography and all his/her albums from the website of the company. The company provides a special service that allows customers to make their own CDs by providing a playlist to the company. These playlists can have tracks from different albums. The selection of soundtracks for these playlists are private to customers and are not visible to others. The price of such a customised CD is the sum of the prices of all soundtracks plus 30. Before being able to make purchases, a customer needs to register an account with the help of a retail store staff. The information needed for registration includes name, phone number, membership card number, address (for delivery, when needed) and the staff id who helped with the registration. The information about staff members consists of the staff id, name, the store he works in and the date of joining the store. After purchasing CDs, the customer will receive an invoice with the list of items he/she purchased. The information of invoices should also be stored in the database. You need to decide what to include in your database.
最新发布
07-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值