usaco1.3Barn Repair( 贪心, 动归)

题目大意就是用最多M 块木板去修天花板, 要求最后用掉的木板长度最小。

这道题最好的是用贪心解。 做的时候我用了dp

想法比较简单, 参考nocow 贪心的想法比较巧妙感觉

题目:

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.]

【以下思路复制于NOCOW】

思路一

要使木板总长度最少,就要使未盖木板的长度最大。

我们先用一块木板盖住牛棚,然后,每次从盖住的范围内选一个最大的空隙,以空隙为界将木板分成两块,重复直到分成m块或没有空隙。

可以用二叉堆来优化算法。

贪心的证明略。

思路二

显然,当所有木板均用上时,长度最短(证明....)。 正向思维,初始状态有c块木板,每块木板只盖住一个牛棚。

由c块倒推至m块木板,每次寻找这样的两个牛棚:其间距在所有未连接的木板中最小。 当这两个牛棚的木板连接时,总木板数减1,总长度增加最小

 思路三

还可以用动态规划求解,将所有牛的牛棚序号a[]排序后,设f[i,j]表示用前i个木版修到第j头牛所用的最短长度.

 f[i,j]=min{f[i-1,j-1]+1,f[i,j-1]+a[j]-a[j-1]}  

f[i-1][j-1]+1表示在第j个牛棚使用一块新的木板(长度为1)覆盖, f[i][j-1]+a[j]-a[j-1]表示延长第i块木板至第j个牛棚。

思路四

显然,当所有木板均用上时,长度最短。用上m块木板时有m-1各间隙。现在的目标是让总间隙最大。将相邻两个有牛的牛棚之间间隔的牛棚数排序,选取最大的m-1个作为间隙,其余地方用木板盖住即可。

我的代码:

 1 /*
 2 ID:doubles3
 3 PROB:barn1
 4 LANG:C++
 5 */
 6 #include<iostream>
 7 #include<cstdlib>
 8 #include<stdio.h>
 9 #include<fstream>
10 #include<math.h>
11 #include <algorithm>
12 #include<string.h>
13 #include<string>
14 #include<vector>
15 using namespace std;
16 #define MAX 5000
17 #define INF 99999999
18 
19 int M,S,C;
20 
21 int L[200+10];
22 int dp[200+10][60];
23 int main()
24 {
25     freopen("barn1.in","r",stdin);
26     freopen("barn1.out","w",stdout);
27 
28     cin>>M>>S>>C;
29 
30     for(int i=0;i<C;i++)
31     {
32         cin>>L[i];
33     }
34     sort(L,L+C);
35     for(int i=0;i<=C;i++)
36     {
37         for(int j=0;j<=M;j++)
38         {
39             dp[i][j] = INF;
40         }
41     }
42 
43     for(int i=1;i<=M;i++)
44     {
45         dp[0][i] = 1;
46     }
47     for(int i=0;i<C;i++)
48     {
49         dp[i][1] = L[i]-L[0] +1;
50     }
51 
52     for(int i=1;i<C;i++)
53     {
54 
55             for(int k = 1; k<=M;k++)
56             {
57                 dp[i][k]  = min(dp[i][k], dp[i-1][k-1]+1);
58                  for(int j = 0;j<i;j++)
59                 {
60                     dp[i][k] = min( dp[i][k], dp[j][k-1]+L[i]-L[j+1]+1);
61                 }
62         }
63     }
64 //    for(int i=0;i<C;i++)
65 //    {
66 //        for(int k=1;k<=M;k++)
67 //        {
68 //            cout<<dp[i][k]<<" ";
69 //        }
70 //        cout<<endl;
71 //    }
72 
73     cout<<dp[C-1][M]<<endl;
74     return 0;
75 }
原文地址:https://www.cnblogs.com/doubleshik/p/3536829.html