解题报告:
这是一道典型的贪心法问题,贪心思路是这样的。如果只有一块木板,我们当然选择从第一个有牛的牛栏覆盖到最后一个有牛的。如果有两块呢?我们可以在第一块的基础上找到最长的一段无牛区域,然后把整个牛棚分成三份:无牛区域以前、无牛区域和无牛区语以后,这样我们用两块木板分别覆盖之前和之后就行。对于更多的木板,我们每次都做同样的事情,就可以得到最优解了。
现在我们来证明一下这样做是正确的:如果我们每次取的不是最长的无牛区域,那么我们这么做:把那个不是最长的区域前后的木板连起来(就是重新覆盖那块区域,但是这样我们就少用了一块木板),然后再把最长无牛区域割裂开(这样我们把那块少用的木板用掉了)。于是我们使用了相同的木板,但是因为覆盖的无牛区域变少而总长度缩短,所以如果不取最长无牛区域,答案会出错!那么如果有两个一样长的最长无牛区域,我们选哪个?按照相同的证明方法,我们可以知道:怎么取都是一样的!所以我们证明了这个贪心法的成立性。
在做这道题的过程中我发现了一个有趣的现象(我的IDE是Code::Blocks),当我们使用如下代码时
bool * temp;
temp = new bool [100];
memset(temp, false, sizeof(temp));
for(int i = 0; i != 100; ++i)
cout << temp << endl;
我们会发现temp并不真的都是0!我猜想这个可能与sizeof(temp)有关,当数组是被new出来而非直接定义时,sizeof会迷惑于这个东西到底有多大…
AC代码:
/*
ID: yuanmz91
PROG: barn1
LANG: C++
*/
#include <fstream>
using namespace std;
int maxbuffer = 0, minbuffer = 2147483647; //用来记录第一只和最后一只牛的位置
int findmax(bool *, int); //找到最长无牛区域的长度
int main()
{
ifstream fin("barn1.in");
ofstream fout("barn1.out");
int boardnum, stallnum, cownum; //分别是木板数量、牛棚总数和奶牛总数
bool * state;
fin >> boardnum >> stallnum >> cownum;
state = new bool [stallnum]; //用来记录哪些牛棚还没考虑(false是还没考虑,true是考虑过了或者那里有一头牛!)
for(int i = 0; i != stallnum; ++i)
{
state[i] = 0;
} //数组初始化,有人可能会问为什么不用memset?我的回答是:你可以试试,然后输出这个数组的每一个元素,可能会有奇妙的事情发生!~>_<~
int buffer;
for(int i = 0; i != cownum; ++i)
{
fin >> buffer;
buffer--; //题目中的编号从1开始,但是我从0开始,这里调整一下
state[buffer] = true; //这里有牛啦!
if(buffer < minbuffer)
{
minbuffer = buffer;
}
if(buffer > maxbuffer)
{
maxbuffer = buffer;
} //找到那两只边界牛
}
for(int i = 0; i != minbuffer; ++i)
{
state[i] = true;
}
for(int i = maxbuffer; i != stallnum; ++i)
{
state[i] = true;
} //边界牛以外的牛栏我们不考虑,因为用不着
int answer = maxbuffer - minbuffer + 1; //还是那句话,边界牛以外的牛栏我们不考虑,因为用不着
while(boardnum != 1)
{
answer -= findmax(state, stallnum); //每次省下的就是最长无牛区域的长度
boardnum--; //用掉一块板子啊……
}
fout << answer << endl;
delete state; //new了记得删,这是好习惯…
return 0;
}
int findmax(bool * array, int size)
{
int result = 0, maxstart = 0;
for(int i = 0; i != size; ++i)
{
int mark = 0;
while(array[i + mark] == false)
{
mark++;
if(i + mark == size)
{
break;
}
} //这里的方法是:遍历每一个元素开始有几个空牛栏,从而比较出最大的那个
if(mark > result)
{
result = mark;
maxstart = i;
}
}
for(int i = maxstart; i != maxstart + result; ++i)
{
array[i] = true;
} //这些牛栏就被视为“考虑过的了”。
return result;
}
下面是题目。
英文版:
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.]
中文版:
描述
在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶、门被吹飞了。 好在许多牛正在度假,所以牛棚没有住满。 牛棚一个紧挨着另一个被排成一行,牛就住在里面过夜。 有些牛棚里有牛,有些没有。 所有的牛棚有相同的宽度。 自门遗失以后,farmer John必须尽快在牛棚之前竖立起新的木板。 他的新木材供应商将会供应他任何他想要的长度,但是吝啬的供应商只能提供有限数目的木板。 farmer John想将他购买的木板总长度减到最少。
给出:可能买到的木板最大的数目M(1<= M<=50);牛棚的总数S(1<= S<=200); 牛棚里牛的总数C(1 <= C <=S);和牛所在的牛棚的编号stall_number(1 <= stall_number <= S),计算拦住所有有牛的牛棚所需木板的最小总长度。 输出所需木板的最小总长度作为答案。
格式
PROGRAM NAME: barn1
INPUT FORMAT:
(file barn1.in)
1 行: 木板最大的数目M ,牛棚的总数S 和 牛的总数C(用空格分开) 2 到 C+1行: 每行包含一个整数,表示牛所占的牛棚的编号。
OUTPUT FORMAT:
(file barn1.out)
单独的一行包含一个整数表示所需木板的最小总长度。
SAMPLE INPUT
4 50 18 3 4 6 8 14 15 16 17 21 25 26 27 30 31 40 41 42 43
SAMPLE OUTPUT
25
[ 一种最优的安排是用板拦牛棚3-8,14-21,25-31,40-43.]