描述
描述
It was a dark and stormy night that ripped the roof and gates off the stalls that hold Farmer John's cows. Happily, many of the cows were on vacation, so the barn was not completely full.
The cows spend the night in stalls that are arranged adjacent to each other in a long line. Some stalls have cows in them; some do not. All stalls are the same width.
Farmer John must quickly erect new boards in front of the stalls, since the doors were lost. His new lumber supplier will supply him boards of any length he wishes, but the supplier can only deliver a small number of total boards. Farmer John wishes to minimize the total length of the boards he must purchase.
Given M (1 <= M <= 50), the maximum number of boards that can be purchased; S (1 <= S <= 200), the total number of stalls; C (1 <= C <= S) the number of cows in the stalls, and the C occupied stall numbers (1 <= stall_number <= S), calculate the minimum number of stalls that must be blocked in order to block all the stalls that have cows in them.
Print your answer as the total number of stalls blocked.
输入
Line 1: M, S, and C (space separated)
Lines 2-C+1: Each line contains one integer, the number of an occupied stall.
输出
A single line with one integer that represents the total number of stalls blocked.
样例输入
4 50 18
3
4
6
8
14
15
16
17
21
25
26
27
30
31
40
41
42
43
样例输出
25
解题思路:贪心去掉间隙最大的m-1段
#include <bits/stdc++.h> using namespace std; const int N=5005; int a[N],b[N]; int main() { int n,m,s,c,k=0; cin>>m>>s>>c; for(int i=1;i<=c;i++) cin>>a[i]; sort(a+1,a+1+c); for(int i=2;i<=c;i++){ b[++k]=a[i]-a[i-1]; } sort(b+1,b+1+k); s=a[c]-a[1]+1; for(int i=1;i<m;i++){ if(k-i+1>=1) s=s-b[k-i+1]+1; } cout<<s<<endl; }
原文地址:https://www.cnblogs.com/ww123/p/11640375.html
- 深入剖析Spring(二)——IoC容器的实现
- JDBC上关于数据库中多表操作一对多关系和多对多关系的实现方法
- 深入剖析Spring(一)——IoC的基本概念(从面向对象角度介绍)
- Restful安全认证及权限的解决方案
- Java并发容器大合集
- Java并发编程的艺术(九)——批量获取多条线程的执行结果
- web.xml中load-on-startup的作用
- Java并发编程的艺术(七)——Executors
- Java并发编程的艺术(八)——闭锁、同步屏障、信号量详解
- PowerDesigner使用教程|使用方法
- Java并发编程的艺术(一)——并发编程需要注意的问题
- 梯度下降法快速教程 | 第三章:学习率衰减因子(decay)的原理与Python实现
- Java并发编程的艺术(三)——volatile
- java学习手册-java 新手入门必看的30个题
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- jQuery通过Ajax实现请求后台接口数据
- Git常规操作
- 手把手教你,嘴对嘴传达 ----源码编译安装部署LAMP平台(LAMP平台与编译安装详解,Apache,MySQL与PHP源码编译安装,LAMP平台搭建论坛)
- Vue点击切换样式
- ElementUI引入到vue项目开发
- 手把手教你,嘴对嘴传达------Apache(安全优化防盗链、隐藏版本信息)
- spring框架应用系列三:切面编程(带参数)
- 排障集锦:九九八十一难之第六难!(98)Address already in use: AH00072: make_sock: could not bind to address ::80
- Vue页面中引用自定义组件
- Vue如何引用Vant组件
- js表单验证工具包
- 手把手教你,嘴对嘴传达------Nginx网站服务(访问状态统计,基于域名,端口,IP的虚拟web主机访问)
- lib-flexible引入到Vue做移动端rem布局
- 微信小程序引入VantWeapp开发
- 手把手教你,嘴对嘴传达------源码编译LNMP部署及应用 , 手动搭建discuz论坛