[USACO08JAN]Running

嘟嘟嘟html

 

這很顯然是一道dp題。ios

令dp[i][j]表示第 i 分鐘末,疲勞度爲 j 是的最大跑步距離,則 git

   dp[i][0] = max(dp[i - 1][0], max(dp[i - j][j]))ide

   dp[i][j] = max(dp[i - 1][j - 1] + a[i])post

由於題中說即便疲勞值爲0了,仍能夠休息,因此dp[i][0]也能夠從dp[i - 1][0]轉換過來。spa

 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) memset(a, 0, sizeof(a))
15 typedef long long ll;
16 typedef double db;
17 const int INF = 0x3f3f3f3f;
18 const int eps = 1e-8;
19 const int maxn = 1e4 + 5;
20 inline ll read()
21 {
22     ll ans = 0;
23     char ch = getchar(), last = ' ';
24     while(!isdigit(ch)) {last = ch; ch = getchar();}
25     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
26     if(last == '-') ans = -ans;
27     return ans;
28 }
29 inline void write(ll x)
30 {
31     if(x < 0) x = -x, putchar('-');
32     if(x >= 10) write(x / 10);
33     putchar(x % 10 + '0');
34 }
35 
36 int n, m, a[maxn];
37 int dp[maxn][maxn];
38 
39 int main()
40 {
41     n = read(); m = read();
42     for(int i = 1; i <= n; ++i) a[i] = read();
43     for(int i = 1; i <= n; ++i)
44     {
45         dp[i][0] = dp[i - 1][0];
46         for(int j = 1; j <= min(i, m); ++j) dp[i][0] = max(dp[i][0], dp[i - j][j]);
47         for(int j = 1; j <= min(i, m); ++j) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + a[i]);
48     }
49     write(dp[n][0]); enter;
50     return 0;
51 }
View Code
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息