Barn Repair
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.
PROGRAM NAME: barn1
INPUT FORMAT
Line 1: | M, S, and C (space separated) |
Lines 2-C+1: | Each line contains one integer, the number of an occupied stall. |
SAMPLE INPUT (file barn1.in)
4 50 18 3 4 6 8 14 15 16 17 21 25 26 27 30 31 40 41 42 43
OUTPUT FORMAT
A single line with one integer that represents the total number of stalls blocked.
SAMPLE OUTPUT (file barn1.out)
25
[One minimum arrangement is one board covering stalls 3-8, one covering 14-21, one covering 25-31, and one covering 40-43.]
题意:
最大供应木块数为M(1到50)(每块木头的提供的长度无限长),牛棚数一共有S(1到200)个,总共有C头牛(大于1小于S),给出没头牛所以在牛棚编号,求在不超过最大供应木块数的情况下,木块能把全部牛都封住的最小长度。
思路:
将木头数与牛的数量进行对比,分成三种情况:
1.当木头数M>牛的数量C(木头数不等于1),则最小长度就是每头牛都有自己的一扇门,故最小长度就是牛的数量;
2.当木头数M=1的时候,故只能把门从头修到尾,故最小长度就是编号最大的牛-编号最小的牛+1;
3.当木头数M<牛的数量C(木头数不等于1),则最小长度应该是全部木板都能用到的时候,则求法为:在C-1个空隙中,取最大空隙长度的前M-1个间隔,然后分别算每段木头的长度加和起来就可以了。
First and test:
/* TASK:barn1 LANG:C++ ID:sum-g1 */ #include<cstdio> #include<map> #include<algorithm> using namespace std; int main() { freopen("barn1.in","r",stdin); freopen("barn1.out","w",stdout); int M,S,C,i,sum; int number[205]; int cut[55]; multimap<int,int> m; scanf("%d%d%d",&M,&S,&C); for(i=1;i<=C;i++) scanf("%d",&number[i]); sort(number+1,number+C+1); if(M==1) printf("%d\n",number[C]-number[1]+1); else { for(i=2;i<=C;i++) m.insert(pair<int,int>(number[i]-number[i-1],i-1)); multimap<int,int>::iterator it; it=m.end(); for(i=0,it--;i<M-1;i++,it--) cut[i]=it->second; sort(cut,cut+M-1); sum=number[cut[0]]-number[1]+1; for(i=1;i<M-1;i++) sum+=(number[cut[i]]-number[cut[i-1]+1]+1); sum+=(number[C]-number[cut[M-2]+1]+1); printf("%d\n",sum); } exit(0); } //忽略了两个情况 //提供的木板不一定要全部用完 //当只有一块木板提供的时候
AC:
/* TASK:barn1 LANG:C++ ID:sum-g1 */ #include<cstdio> #include<map> #include<algorithm> using namespace std; int main() { freopen("barn1.in","r",stdin); freopen("barn1.out","w",stdout); int M,S,C,i,sum; int number[205]; int cut[55]; multimap<int,int> m; //用multimap的对应关系来存储每个棚间的间隙和间隙左侧的棚序号 //不用map是因为map会去重 scanf("%d%d%d",&M,&S,&C); for(i=1;i<=C;i++) scanf("%d",&number[i]); sort(number+1,number+C+1); //对棚的序号进行由小到大的排序 if(M==1) printf("%d\n",number[C]-number[1]+1); //下面的条件要加上M!=1 if(M!=1&&M>=C) printf("%d\n",C); if(M!=1&&M<C) { for(i=2;i<=C;i++) m.insert(pair<int,int>(number[i]-number[i-1],i-1)); //multimap的赋值处理要注意 multimap<int,int>::iterator it; it=m.end(); for(i=0,it--;i<M-1;i++,it--) cut[i]=it->second; //因为切点间隙已经由小到大排好序了,所以从最后一项开始向前取M-1个空隙的左序号放在cut数组中 sort(cut,cut+M-1); //对cut进行由小到大排序 sum=number[cut[0]]-number[1]+1; for(i=1;i<M-1;i++) sum+=(number[cut[i]]-number[cut[i-1]+1]+1); sum+=(number[C]-number[cut[M-2]+1]+1); //总加和操作即可求出得数 //注意边界问题,哪里该+1还是不该+1 printf("%d\n",sum); } exit(0); }
总结:
题目不难,但是却也是过了一天才回过头再去看这个题。一开始是因为读不懂题意,没有耐心看下去,所以才一直被搁置在一旁。静下来很重要,慢慢的发现在实验室和在宿舍真的会有差别,平静很重要,专心更加重要。
相关推荐
从文件 barn1.in 中读入数据。 第 1 行: M , S 和 C(用空格分开) 第 2 到 C+1行: 每行包含一个整数,表示牛所占的牛棚的编号。 输出 输出到文件 barn1.out 中。 单独的一行包含一个整数表示所需木板的最小总长度。 ...
usaco上的题目barn1的答案,绝对正确
Barn-Burning中文版.doc
Big Barn 巨大的牛棚(usaco动规含测试数据)
USACO 官网第一到 五章 练习题中文语言官方数据 fps格式支持导入所有OJ ...11 [1.3] 修理牛棚 Barn Repair 12 [1.3] 牛式 Prime Cryptarithm 13 [1.3] 虫洞 wormhole 14 [1.3] 滑雪课程设计Ski Course Design
usaco 上的题目barn1,beads,calfflac,可到那里查看具体题目
前端项目-barn,本地存储之上的快速原子持久存储层
C#实现的半透明窗体实例,可完全透明,拖动左侧的滑块向下就可改变透明度,滑块向下透明度提高,向上则透明度降低
Game crystal mines for nintendo with source code
Barn - Laravel 应用程序的 Ansible 剧本 这个存储库提供了专门针对 Laravel 应用程序的 Ansible playbook。 如果您不熟悉 Ansible,它是一个最常用于配置、配置管理和部署的开源工具。 与 Ansible 和这些 playbook ...
chlakh font Thank you for download the font. This font is for PERSONAL USE ONLY
The Advanced Peripheral Bus (APB) is part of the Advanced Microcontroller Bus Architecture (AMBA) protocol family. It defines a low-cost interface that is optimized for minimal power consumption and ...
butch-the-barn-man.github.io:Butch the Barn Man的公共艺术网站
语言:English Bulk and Barn Checkout是一个助手,它在讨论Bulk and Barn网站时增加了购物车的概念 Bulk and Barn Checkout是一个助手,它在讨论Bulk and Barn网站时增加了购物车的概念
c51单片机做一个电子时钟,用到两个四位数码管,加入了蜂鸣器
OJ题,有关搜索算法的一道经典的题,算法通过组合数输出的思想一样
基于单片机的孵化环境温湿度监控系统,在家禽孵化行业可以有效发挥。
人员疏散时考虑因素 人员行走的模糊规则 利用matlab进行模糊化 考虑人员行走规律
Barn2 Media WooCommerce Protected Categories Barn2 Media Woocommerce保护类别" ---------- 泰森云每天更新发布最新WordPress主题、HTML主题、WordPress插件、shopify主题、opencart主题、PHP项目源码、安卓项目...