USACO 1.3 Barn Repair(DP)

这是贪心专题吗,这个题用DP搞的(实在是没看出怎么贪心),数据里,不是按顺序给出的,而且有m>c的情况,我被坑啦。。。DP状态转移还是很好想的。

 1 /*

 2  ID: cuizhe

 3  LANG: C++

 4  TASK: barn1

 5 */

 6 #include <iostream>

 7 #include <cstdio>

 8 #include <cstring>

 9 #include <cstdlib>

10 #include <map>

11 #include <algorithm>

12 using namespace std;

13 #define N 100000000

14 int dp[51][201],sum[201],o[201],p[201];

15 int main()

16 {

17     int n,m,s,i,j,c,k;

18     freopen("barn1.in","r",stdin);

19     freopen("barn1.out","w",stdout);

20     scanf("%d%d%d",&m,&s,&c);

21     for(i = 1;i <= c;i ++)

22     {

23         scanf("%d",&n);

24         p[i] = n;

25         o[n] = 1;

26     }

27     if(m > c)

28     {

29         printf("%d\n",c);

30         return 0;

31     }

32     sort(p+1,p+c+1);

33     for(i = 1;i <= s;i ++)

34     {

35         if(o[n])

36         sum[i] = sum[i-1]+1;

37         else

38         sum[i] = sum[i-1];

39     }

40     for(i = 1;i <= c;i ++)

41     {

42         dp[1][i] = p[i]-p[1]+1;

43     }

44     for(i = 2;i <= m;i ++)

45     {

46         for(j = 1;j <= c;j ++)

47         {

48             dp[i][j] = N;

49             for(k = 1;k <= j-1;k ++)

50             {

51                 if(j-k > 0)

52                 {

53                     if(dp[i][j] > dp[i-1][k]+sum[p[j]]-sum[p[k+1]]+1)

54                     dp[i][j] = dp[i-1][k]+sum[p[j]]-sum[p[k+1]]+1;

55                 }

56             }

57         }

58     }

59     printf("%d\n",dp[m][c]);

60     return 0;

61 }

你可能感兴趣的:(USACO)