USACO Training Section 1.3 Barn Repair 解题报告&AC代码

解题报告:

这是一道典型的贪心法问题,贪心思路是这样的。如果只有一块木板,我们当然选择从第一个有牛的牛栏覆盖到最后一个有牛的。如果有两块呢?我们可以在第一块的基础上找到最长的一段无牛区域,然后把整个牛棚分成三份:无牛区域以前、无牛区域和无牛区语以后,这样我们用两块木板分别覆盖之前和之后就行。对于更多的木板,我们每次都做同样的事情,就可以得到最优解了。

现在我们来证明一下这样做是正确的:如果我们每次取的不是最长的无牛区域,那么我们这么做:把那个不是最长的区域前后的木板连起来(就是重新覆盖那块区域,但是这样我们就少用了一块木板),然后再把最长无牛区域割裂开(这样我们把那块少用的木板用掉了)。于是我们使用了相同的木板,但是因为覆盖的无牛区域变少而总长度缩短,所以如果不取最长无牛区域,答案会出错!那么如果有两个一样长的最长无牛区域,我们选哪个?按照相同的证明方法,我们可以知道:怎么取都是一样的!所以我们证明了这个贪心法的成立性。

在做这道题的过程中我发现了一个有趣的现象(我的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.]

这里是测试数据:
Here are the test data inputs:

------- test 1 ----
4 50 17
3
4
6
8
14
15
16
17
25
26
27
30
31
40
41
42
43
------- test 2 ----
2 10 4
2
4
6
8
------- test 3 ----
3 27 16
2
3
5
6
8
9
10
13
14
15
16
19
20
21
22
27
------- test 4 ----
1 200 8
101
105
102
106
103
107
104
99
------- test 5 ----
50 200 10
18
69
195
38
73
28
6
172
53
99
------- test 6 ----
50 30 6
30
25
20
15
10
5
------- test 7 ----
20 200 80
65
178
64
70
18
32
88
90
98
20
152
31
118
117
127
81
175
73
136
161
165
63
130
133
190
10
4
138
200
43
189
37
86
182
145
110
67
126
114
153
99
25
155
119
176
55
48
197
62
147
125
60
12
23
112
96
27
122
35
50
36
49
149
108
100
188
77
191
6
121
166
132
82
95
150
89
22
40
128
56
------- test 8 ----
4 200 100
72
180
46
198
196
131
165
112
52
133
187
93
57
35
128
65
127
130
12
49
88
155
122
193
101
164
98
143
54
149
38
84
45
139
79
16
102
20
14
150
188
33
176
135
29
80
19
74
11
114
95
185
137
59
32
189
66
67
191
91
77
134
18
10
7
200
8
13
55
24
142
184
17
6
109
105
43
181
85
94
151
160
115
25
116
111
37
104
144
97
90
141
120
119
152
182
123
172
40
23
------- test 9 ----
20 195 100
1
2
3
4
5
11
12
13
14
15
21
22
23
24
25
31
32
33
34
35
41
42
43
44
45
51
52
53
54
55
61
62
63
64
65
71
72
73
74
75
81
82
83
84
85
91
92
93
94
95
101
102
103
104
105
111
112
113
114
115
121
122
123
124
125
131
132
133
134
135
141
142
143
144
145
151
152
153
154
155
161
162
163
164
165
171
172
173
174
175
181
182
183
184
185
191
192
193
194
195
------- test 10 ----
1 200 2
1
200

Keep up the good work!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值