[USACO11DEC]Umbrellas for Cows

Posted mrclr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[USACO11DEC]Umbrellas for Cows相关的知识,希望对你有一定的参考价值。

嘟嘟嘟

 

我dp真是太弱了,这么简单dp都不会。

令dp[i]表示前 i 头牛头被遮住了的最低成本。则dp[i] = min{dp[i], dp[j - 1] + c[a[i] - a[j] + 1]} (1 <= j <= i)

然后别忘了预处理后缀最小值。

技术分享图片
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cctype>
 8 #include<vector>
 9 #include<stack>
10 #include<queue>
11 using namespace std;
12 #define enter puts("") 
13 #define space putchar(‘ ‘)
14 #define Mem(a, x) memset(a, x, sizeof(a))
15 #define rg register
16 typedef long long ll;
17 typedef double db;
18 const ll INF = 1e12;
19 const db eps = 1e-8;
20 const int maxn = 5e3 + 5;
21 const int maxm = 1e5 + 5;
22 inline ll read()
23 {
24   ll ans = 0;
25   char ch = getchar(), last =  ;
26   while(!isdigit(ch)) last = ch, ch = getchar();
27   while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - 0, ch = getchar();
28   if(last == -) ans = -ans;
29   return ans;
30 }
31 inline void write(ll x)
32 {
33   if(x < 0) x = -x, putchar(-);
34   if(x >= 10) write(x / 10);
35   putchar(x % 10 + 0);
36 }
37 
38 int n, m, a[maxn], c[maxm];
39 ll dp[maxn];
40 
41 int main()
42 {
43   n = read(); m = read();
44   for(int i = 1; i <= n; ++i) a[i] = read();
45   sort(a + 1, a + n + 1);
46   for(int i = 1; i <= m; ++i) c[i] = read();
47   for(int i = m - 1; i; --i) c[i] = min(c[i], c[i + 1]);
48   for(int i = 1; i <= n; ++i) dp[i] = INF;
49   for(int i = 1; i <= n; ++i)
50     for(int j = 1; j <= i; ++j)
51       dp[i] = min(dp[i], dp[j - 1] + c[a[i] - a[j] + 1]);
52   write(dp[n]), enter;
53   return 0;
54 }
View Code

 

以上是关于[USACO11DEC]Umbrellas for Cows的主要内容,如果未能解决你的问题,请参考以下文章

bzo1606: [Usaco2008 Dec]Hay For Sale 购买干草

[bzoj 1606] [Usaco2008 Dec]Hay For Sale 购买干草 (dp)

P2925 [USACO08DEC]干草出售Hay For Sale

洛谷P2925 [USACO08DEC]干草出售Hay For Sale

洛谷P2925 [USACO08DEC]干草出售Hay For Sale

洛谷P2925 [USACO08DEC]干草出售Hay For Sale