usaco BarnRepair题目解答

引用博客:http://blog.youkuaiyun.com/lisc741/article/details/8272454

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.


题目分析:

从题目中可以看出有关的是M(木板数目),C(牛的数目)[题目描述中提到牛是一个挨着一个,其实就是指一个牛一个棚];需要求解的是木板的最短长度。思路是最短长度可以看做将第一个牛棚到最后一个牛棚都先连起来,然后去除最长的M-1个间隔(M块木板最多有M-1个间隔),剩下的木板长度就是最短长度。


代码:

bool cmp(int a, int b)
{
return a > b;
}

int main() {
    ofstream fout ("barn1.out");
    ifstream fin ("barn1.in");
int S = 0;
int C = 0;
int M = 0;
int ans = 0;
fin >> M >> S >> C;
int* stalls = new int[C];
int* gaps = new int[C];
for (int i = 0; i < C;i++)
{
fin >> stalls[i];
gaps[i] = 0;
}
sort(stalls, stalls + C);
for (int j = 0; j < C;j++)
{
if (j==0)
{
continue;
}
gaps[j - 1] = stalls[j] - stalls[j - 1]-1;//间隔去除首尾
}
sort(gaps, gaps + C, cmp);
ans = stalls[C-1] - stalls[0] + 1;
for (int k = 0; k <M-1;k++)
{
ans -= gaps[k];
if (gaps[k]==0)//当剩下的距离都是0的时候就说明剩下的牛棚都是连着的
{
break;
}
}
fout << ans << endl;
    return 0;
}


内容概要:本文档详细介绍了在三台CentOS 7服务器(IP地址分别为192.168.0.157、192.168.0.158和192.168.0.159)上安装和配置Hadoop、Flink及其他大数据组件(如Hive、MySQL、Sqoop、Kafka、Zookeeper、HBase、Spark、Scala)的具体步骤。首先,文档说明了环境准备,包括配置主机名映射、SSH免密登录、JDK安装等。接着,详细描述了Hadoop集群的安装配置,包括SSH免密登录、JDK配置、Hadoop环境变量设置、HDFS和YARN配置文件修改、集群启动与测试。随后,依次介绍了MySQL、Hive、Sqoop、Kafka、Zookeeper、HBase、Spark、Scala和Flink的安装配置过程,包括解压、环境变量配置、配置文件修改、服务启动等关键步骤。最后,文档提供了每个组件的基本测试方法,确保安装成功。 适合人群:具备一定Linux基础和大数据组件基础知识的运维人员、大数据开发工程师以及系统管理员。 使用场景及目标:①为大数据平台搭建提供详细的安装指南,确保各组件能够顺利安装和配置;②帮助技术人员快速掌握Hadoop、Flink等大数据组件的安装与配置,提升工作效率;③适用于企业级大数据平台的搭建与维护,确保集群稳定运行。 其他说明:本文档不仅提供了详细的安装步骤,还涵盖了常见的配置项解释和故障排查建议。建议读者在安装过程中仔细阅读每一步骤,并根据实际情况调整配置参数。此外,文档中的命令和配置文件路径均为示例,实际操作时需根据具体环境进行适当修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值