【HDUOJ】几道递推DP

就不写题解了。很基础的递推。


HDU2084数塔

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084

代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 const int maxn = 105;
 8 
 9 int dp[maxn][maxn];
10 int a[maxn][maxn];
11 
12 int main(){
13     int T;
14     cin>>T;
15     while(T--){
16         memset(a,0,sizeof(a));
17         memset(dp,0,sizeof(dp));
18         int n;
19         cin>>n;
20         for(int i = 1; i <= n ;i++){
21             for(int j = 1; j <= i; j++){
22                 cin>>a[i][j];
23             }
24         }
25 
26         for(int i = n; i > 0; i--){
27             for(int j = 1; j <= n ;j++){
28                 dp[i][j] = max(dp[i+1][j] ,dp[i+1][j+1]) + a[i][j];
29             }
30         }
31         cout<<dp[1][1]<<endl;
32     }
33 
34 
35     return 0;
36 }

HDU2018母牛的故事

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2018

代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 const int maxn = 105;
 8 int dp[maxn];
 9 
10 void init(){
11     dp[1] = 1;
12     dp[2] = 2;
13     dp[3] = 3;
14     dp[4] = 4;
15     dp[5] = 6;
16     for(int i = 6; i <= 55; i++){
17         dp[i] = dp[i-1] + dp[i-3];
18     }
19 }
20 
21 int main(){
22     int n;
23     init();
24     while(cin>>n && n){
25         cout<<dp[n]<<endl;
26     }
27 
28     return 0;
29 }

HDU2044小蜜蜂的故事

同类型HDU2041

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2044

代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #define ll long long
 6 using namespace std;
 7 const int maxn = 55;
 8 ll fib[maxn];
 9 
10 void init(){
11     fib[0] = 0;
12     fib[1] = 1;
13     fib[2] = 2;
14     for(int i = 3 ;i <= maxn ;i++){
15         fib[i] = fib[i-1] + fib[i-2];
16     }
17 }
18 
19 int main(){
20     init();
21     int n,m;
22     int T;
23     cin>>T;
24     while(T--){
25         cin>>n>>m;
26         cout<<fib[m-n]<<endl;
27     }
28 
29 
30     return 0;
31 }

HDU2050

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2050

代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #define ll long long
 6 using namespace std;
 7 const int maxn = 10005;
 8 ll dp[maxn];
 9 
10 void init(){
11     dp[1] = 2;
12     dp[2] = 7;
13     for(int i = 3; i <= maxn; i++){
14         dp[i] = dp[i-1] + 4*(i-1) + 1;
15     }
16 }
17 
18 int main(){
19     int T;
20     cin>>T;
21     int n;
22     init();
23     while(T--){
24         cin>>n;
25         cout<<dp[n]<<endl;
26     }
27 
28     return 0;
29 }

猜你喜欢

转载自www.cnblogs.com/Asumi/p/9769934.html