题意:约翰的牛棚损坏了需要修补,每个牛棚的宽度是一样的,一共有S个牛棚,供应商能够提供任意长度的木板(一个单位木板
覆盖一个牛棚),但总共最多有M个木板,现在牛棚里面还有C头牛,下面C行是每头牛的牛棚的位置。
寻找一种方案,使得总共的木板长度最短,覆盖所有的牛。
核心:
理解题意,条件限制:
①最多M个木板
②寻找最小的总木板长度
因为有C头牛,所以总木板长度最小就为C,问题转化为,增加覆盖某些牛棚,那么长度增加对应的长度,同时满足
木板块数不超过M
贪心的思路:找出空白的牛棚位置和数目 例如 34_6_8_14...
那么第一个空白为7,连续空白为1,第三个空白为 [9,13]长度为5
原始状态下有X个空白(X+1个木板快) 每填充一个空白,X减少一个,那么优先选择空白长度小的空白(增加总长度少)
直到木板块数不再超过M为止
注意:题目条件中没有说明有牛的牛棚数是顺序输入的
/*ID: lsswxr1PROG: barn1LANG: C++*/#include #include #include