蓝桥集训之修理牛棚

蓝桥集训之修理牛棚

  • 核心思想:贪心

    • 先把所有牛棚合成一块木板
    • 然后将所有间隙大小求出 排序找到最大的n-1个
    • 总长度 - n-1个间隙 得到剩下n个木板总长度
cpp 复制代码
  #include <iostream>
  #include <cstring>
  #include <algorithm>
  
  using namespace std;
  const int N = 210;
  
  int a[N],space[N];
  int n,m,c;
  
  int main()
  {
      cin>>n>>m>>c;
      for(int i=0;i<c;i++) cin>>a[i];
      sort(a,a+c); 
      for(int i=1;i<c;i++) space[i] = a[i]-a[i-1]-1;  //求间隙长度
      int res = a[c-1] - a[0] + 1;  //总长度
      //从大到小排间隙
      sort(space+1,space+c,greater<int>());
      for(int i=1;i<n;i++) res -= space[i];
      cout<<res<<endl;
      return 0;
  }
相关推荐
阿豪学编程8 小时前
LeetCode724.:寻找数组的中心下标
算法·leetcode
墨韵流芳8 小时前
CCF-CSP第41次认证第三题——进程通信
c++·人工智能·算法·机器学习·csp·ccf
csdn_aspnet9 小时前
C# 求n边凸多边形的对角线数量(Find number of diagonals in n sided convex polygon)
开发语言·算法·c#
凌波粒9 小时前
LeetCode--349.两个数组的交集(哈希表)
java·算法·leetcode·散列表
paeamecium11 小时前
【PAT甲级真题】- Student List for Course (25)
数据结构·c++·算法·list·pat考试
Book思议-11 小时前
【数据结构】栈与队列全方位对比 + C 语言完整实现
c语言·数据结构·算法··队列
SteveSenna11 小时前
项目:Trossen Arm MuJoCo
人工智能·学习·算法
NAGNIP11 小时前
一文搞懂CNN经典架构-DenseNet!
算法·面试
道法自然|~11 小时前
BugCTF黄道十二宫
算法·密码学
WHS-_-202212 小时前
Python 算法题学习笔记一
python·学习·算法