蓝桥集训之修理牛棚

蓝桥集训之修理牛棚

  • 核心思想:贪心

    • 先把所有牛棚合成一块木板
    • 然后将所有间隙大小求出 排序找到最大的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;
  }
相关推荐
洛水水10 分钟前
【力扣100题】33.验证二叉搜索树
算法·leetcode·职场和发展
SimpleLearingAI23 分钟前
聚类算法详解
算法·数据挖掘·聚类
刀法如飞1 小时前
Go 字符串查找的 20 种实现方式,用不同思路解决问题
算法·面试·程序员
Dlrb12113 小时前
C语言-指针数组与数组指针
c语言·数据结构·算法·指针·数组指针·指针数组·二级指针
WL_Aurora3 小时前
Python 算法基础篇之集合
python·算法
平行侠3 小时前
A15 工业路由器IP前缀高速检索与内存压缩系统
网络·tcp/ip·算法
阿旭超级学得完4 小时前
C++11包装器(function和bind)
java·开发语言·c++·算法·哈希算法·散列表
li星野4 小时前
位运算 & 数学 & 高频进阶九题通关(Python + C++)
c++·python·学习·算法
jerryinwuhan5 小时前
hello算法,简单讲(1)
算法·排序算法
y = xⁿ5 小时前
20天速通LeetCodeday15:BFS广度优先搜索
算法·宽度优先