蓝桥集训之修理牛棚

蓝桥集训之修理牛棚

  • 核心思想:贪心

    • 先把所有牛棚合成一块木板
    • 然后将所有间隙大小求出 排序找到最大的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;
  }
相关推荐
春日见5 小时前
如何避免代码冲突,拉取分支
linux·人工智能·算法·机器学习·自动驾驶
副露のmagic5 小时前
更弱智的算法学习 day59
算法
u0109272716 小时前
C++中的RAII技术深入
开发语言·c++·算法
青桔柠薯片6 小时前
数据结构:顺序表与链表
数据结构·链表
2401_832131957 小时前
模板错误消息优化
开发语言·c++·算法
金枪不摆鳍7 小时前
算法--二叉搜索树
数据结构·c++·算法
近津薪荼7 小时前
优选算法——双指针6(单调性)
c++·学习·算法
向哆哆7 小时前
画栈 · 跨端画师接稿平台:基于 Flutter × OpenHarmony 的整体设计与数据结构解析
数据结构·flutter·开源·鸿蒙·openharmony·开源鸿蒙
helloworldandy7 小时前
高性能图像处理库
开发语言·c++·算法
2401_836563187 小时前
C++中的枚举类高级用法
开发语言·c++·算法