蓝桥集训之修理牛棚

蓝桥集训之修理牛棚

  • 核心思想:贪心

    • 先把所有牛棚合成一块木板
    • 然后将所有间隙大小求出 排序找到最大的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;
  }
相关推荐
NAGNIP2 小时前
GPT-5.1 发布:更聪明,也更有温度的 AI
人工智能·算法
NAGNIP2 小时前
激活函数有什么用?有哪些常用的激活函数?
人工智能·算法
元亓亓亓3 小时前
LeetCode热题100--416. 分割等和子集--中等
算法·leetcode·职场和发展
BanyeBirth3 小时前
C++差分数组(二维)
开发语言·c++·算法
xu_yule5 小时前
算法基础(数论)—算法基本定理
c++·算法·算数基本定理
CoderCodingNo6 小时前
【GESP】C++五级真题(结构体排序考点) luogu-B3968 [GESP202403 五级] 成绩排序
开发语言·c++·算法
浅川.256 小时前
STL专项:stack 栈
数据结构·stl·stack
YGGP7 小时前
【Golang】LeetCode 32. 最长有效括号
算法·leetcode
自然常数e7 小时前
字符函数和字符串函数
c语言·算法·visual studio