• 题意:有\(n\)个数,每次可以选\(k(1\le k\le n)\)个数,并且得到\(a_1+max(0,a_2-1)+max(0,a_3-2)+...+max(0,a_k-k+1)\)的贡献,问最少选多少次使得总贡献不小于\(m\).

  • 题解:我们从大到小排序,然后二分答案,贪心,如果答案是\(k\)天,那么对于前\(k\)个数,我们一定单独选它们分配到不同的\(k\)个集合中,然后再重复这个过程,从\(k+1\)个数开始循环分配到不同j集合中,这样一定能得到最大的贡献.

  • 代码:

    ll n,m;
    ll a[N];
    ll sum; bool check(ll x){
    ll res=0;
    ll cnt=0;
    ll num=0;
    for(int i=1;i<=n;++i){
    res+=max((ll)0,a[i]-num);
    cnt++;
    if(cnt==x) num++,cnt=0;
    }
    if(res>=m) return true;
    return false;
    } bool cmp(ll a,ll b){
    return a>b;
    } int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;++i){
    cin>>a[i];
    sum+=a[i];
    } if(sum<m){
    cout<<-1<<endl;
    return 0;
    }
    sort(a+1,a+1+n,cmp);
    ll l=1,r=n;
    while(l<r){
    int mid=(l+r)>>1;
    if(check(mid)){
    r=mid;
    }
    else l=mid+1;
    } cout<<l<<endl; return 0;
    }

Codeforces Round #540 (Div. 3) D2. Coffee and Coursework (Hard Version) (二分,贪心)的更多相关文章

  1. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】

    任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...

  2. Codeforces Round #540 (Div. 3)--1118D2 - Coffee and Coursework (Hard Version)

    https://codeforces.com/contest/1118/problem/D2 和easy version的主要区别是,数据增加了. easy version采用的是线性查找,效率低 在 ...

  3. Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)

    https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...

  4. Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)

    题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...

  5. Codeforces Round #540 (Div. 3) A,B,C,D2,E,F1

    A. Water Buying 链接:http://codeforces.com/contest/1118/problem/A 实现代码: #include<bits/stdc++.h> ...

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

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  7. Codeforces Round #350 (Div. 2) D2. Magic Powder - 2

    题目链接: http://codeforces.com/contest/670/problem/D2 题解: 二分答案. #include<iostream> #include<cs ...

  8. Codeforces Round #527 (Div. 3) D2. Great Vova Wall (Version 2) 【思维】

    传送门:http://codeforces.com/contest/1092/problem/D2 D2. Great Vova Wall (Version 2) time limit per tes ...

  9. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

随机推荐

  1. 前端面试:Http协议与浏览器

    Http与Https的区别 Http是明文传输的,Https协议是在Http协议上添加了SSL的加密协议,可以进行加密传输和身份验证. 其实就是说Http对网络传输完全是裸奔状态,也就没办法防范中间人 ...

  2. 微信小程序request请求的封装

    目录 1,前言 2,实现思路 3,实现过程 3.1,request的封装 3.2,api的封装 4,实际使用 1,前言 在开发微信小程序的过程中,避免不了和服务端请求数据,微信小程序给我们提供了wx. ...

  3. MySQL where 条件字句查询

    where 条件字句 搜索条件可由一个或多个逻辑表达式组成 , 结果一般为布尔值 逻辑运算符 运算符 语法 描述 and && a and b a && b 逻辑与 两 ...

  4. linux下安装nacos

    一.安装 1.下载安装包: https://github.com/alibaba/nacos/releases 2.解压 : tar -xzvf nacos-server-1.2.1.tar.gz 3 ...

  5. 使用npm install安装项目依赖的时候报错

    使用npm install安装项目依赖的时候报错: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@4.14.1 postin ...

  6. elasticsearch-head:5连接elasticsearch 6.x无法显示浏览数据的解决方案

    问题 在docker安装了elasticsearch-head:5和elasticsearch:6.3.2 打开界面连接es时发现数据无法展示. 解决方案 因docker使用的版本问题,可能名称略有变 ...

  7. 前端面试之ES6中的继承!

    前端面试之ES6中的继承! ES6之前并没有给我们提供 extends继承.我们可以通过构造函数+原型对象模拟实现继承,被称为组合继承. 1 call() 两个作用: 1 调用这个函数! 2 修改函数 ...

  8. MySQL调优性能监控之show profile

    用show profile查询工具指定具体的type show profile在mysql5.7之后过时 show profile命令用于跟踪执行过的sql语句的资源消耗信息,可以帮助查看sql语句的 ...

  9. JVM虚拟机垃圾回收(GC)算法及优缺点

    一.什么是GC   GC是jvm的垃圾回收,垃圾回收的规律和原则为:   次数上频繁收集新生区(Young)   次数上较少收集养老区(Old)   基本上不动永久区(Perm) 二.GC算法(分代收 ...

  10. (010)每日SQL学习:按字母顺序排列字符串

    需求:一串字母'ADFGH',需要按照顺序来显示:A D F G H 第一步:先把字符串拆分 with test as( select 'ADFGH' as a from dual ) select ...