usaco.section1.3.Barn Repair
来源:互联网 发布:微信域名要备案吗 编辑:程序博客网 时间:2024/06/06 05:35
在长度为 s 的排成一行的牛棚中,有n 个需要被木板覆盖,但是只有 m 块任意长度的木板,最小化所需木板总长度
贪心,先将 n 个需要覆盖的牛棚用一块木板覆盖,这样的话其中比存在覆盖了多余的一些空白区域,然后按从大到小的顺序依次删除m 个空白,就得到了答案。正确性显然,要总的最小,就把最大的 m 个删掉。
还有一种动态规划的算法,没有想到,但是 nocow 的题解上有写,可以借鉴一下。
#include <cstdio>#include <algorithm>using namespace std;const int MAX_N = 205;int n, m, s, a[MAX_N], b[MAX_N];void init(){scanf("%d%d%d", &m, &s, &n);for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);}inline bool cmp(int a, int b){return a > b;}void doit(){sort(a + 1, a + n + 1);for(int i = 1; i <= n - 1; i ++)b[i] = a[i + 1] - a[i] - 1;sort(b + 1, b + n, cmp);int ans = a[n] - a[1] + 1;for(int i = 1; i < m; i ++)ans -= b[i];printf("%d\n", ans);}int main(){freopen("barn1.in", "r", stdin);freopen("barn1.out", "w", stdout);init(); doit();return 0;}
0 0
- usaco.section1.3.Barn Repair
- USACO section1.3.2 Barn Repair
- USACO Section1.3 Barn Repair
- USACO-Section1.3 Barn Repair
- USACO-Section1.3 Barn Repair
- USACO-section1.3 Barn Repair
- USACO section1.3 Barn Repair
- USACO-Section1.3 Barn Repair [贪心算法]
- USACO-Section1.3 Barn Repair【贪心算法】
- USCAO section1.3 Barn Repair
- Barn Repair(usaco)
- USACO 1.3-Barn Repair
- [USACO] Barn Repair
- USACO 1.3 Barn Repair
- USACO Barn Repair
- USACO/Barn Repair(贪心)
- usaco Barn Repair
- [USACO]Barn Repair
- text-shadow,background-origin,background-size,background-clip
- access
- 使用eclipse找回eclipse中误删的文件
- Spark集群配置遇到的问题
- 信息流,资金流和物流杂谈
- usaco.section1.3.Barn Repair
- db2数据库抛错:Result set already closed
- CLRS 9.2期望为线性时间的选择算法
- 使用CXF框架学习搭建WebService(四)
- ora-01000的处理思路
- python zeros() 和 ones()
- 史上最详细的android的点9(.9)图片制作教程,菜鸟看完也保证可以学会
- 计算相似度评价值体系:欧几里德距离和皮尔逊相关度
- vs2008+ObjectARX2010第一个ARX程序hello arx