码迷,mamicode.com
首页 > 其他好文 > 详细

[USACO11DEC]Umbrellas for Cows

时间:2018-11-05 19:07:54      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:==   display   char   href   string   lap   img   closed   queue   

嘟嘟嘟

 

我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

标签:==   display   char   href   string   lap   img   closed   queue   

原文地址:https://www.cnblogs.com/mrclr/p/9910500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!