CF1118D2 Coffee and Coursework (Hard Version)(二分)
本文最后更新于:3 years ago
题目描述
Input
The first line of the input contains two integers n and m (1≤n≤2⋅105, 1≤m≤109) — 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≤109), 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
input
output
input
output
input
output
input
output
input
output
题目大意:
有n杯咖啡,要做m份作业,每杯咖啡有一个值a,表示喝完能做的作业数量,但是如果一天之内喝了超过一杯,那么每喝一杯就会少做k-1件作业(即第一杯为ai,第二杯为max(0,ai-1),第n杯为max(0,ai-n)),问最少多少天能做完作业
解题思路:
要使咖啡收益最大化,那么就得使值大的在每天作为第一杯咖啡使用,那么可以二分答案,每次取一个天数mid,跑for遍历咖啡,每mid杯咖啡后更新减的值(每天一杯,那么每mid天之后的就是要减去值的),最后输出最少的天数即可,如果咖啡的值加起来也无法超过作业的数量的话,那么显然是无法完成的,输出-1即可
题外话:
python的效率是比较慢的,在打acm过程中是很吃亏的,如果oj有pypy选项的,建议使用pypy提交,pypy虽然库支持比较少,但是运行效率会比python快很多
代码如下:
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!