蓝桥集训之修理牛棚

蓝桥集训之修理牛棚

  • 核心思想:贪心

    • 先把所有牛棚合成一块木板
    • 然后将所有间隙大小求出 排序找到最大的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;
  }
相关推荐
sinat_2869451918 小时前
AI Coding LSP
人工智能·算法·prompt·transformer
星马梦缘19 小时前
算法与数据结构
数据结构·c++·算法·动态规划·克鲁斯卡尔·kahn
2501_9434691519 小时前
【无标题】
数据结构·算法
_codemonster19 小时前
计算机视觉入门到实战系列(八)Harris角点检测算法
python·算法·计算机视觉
Snow_day.19 小时前
有关排列排列组合(1)
数据结构·算法·贪心算法·动态规划·图论
dora19 小时前
【开发火星地平线辅助】智商不够,编程来凑
算法
im_AMBER19 小时前
Leetcode 100 在链表中插入最大公约数
数据结构·c++·笔记·学习·算法·leetcode·链表
Z1Jxxx20 小时前
删除字符串2
开发语言·c++·算法
踩坑记录20 小时前
leetcode hot100 15. 三数之和 medium
算法·leetcode·职场和发展
独自破碎E20 小时前
【二分法】旋转数组的最小数字
数据结构·算法·排序算法