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

Book 动态规划

时间:2015-07-19 23:33:18      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

 

虽然之前学过一点点,但是还是不会------现在好好跟着白书1.4节学一下——————

(1)数字三角形

d(i,j) = max(d(i+1,j),d(i+1,j+1)) + a[i][j]

hdu 2084

技术分享
 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath> 
 5 #include<stack>
 6 #include<vector>
 7 #include<map> 
 8 #include<set>
 9 #include<queue> 
10 #include<algorithm>  
11 using namespace std;
12 
13 typedef long long LL;
14 const int INF = (1<<30)-1;
15 const int mod=1000000007;
16 const int maxn=1000005;
17 
18 int d[1005][1005],a[1005][1005];
19 
20 int main(){
21     int T;
22     scanf("%d",&T);
23     while(T--){
24         int n;
25         scanf("%d",&n);
26         memset(d,0,sizeof(d));
27         for(int i = 1;i<=n;i++)
28             for(int j = 1;j<=i;j++) scanf("%d",&a[i][j]);
29         
30         for(int i = n;i>=1;i--){
31             for(int j = 1;j<=i;j++)
32             d[i][j] = max(d[i+1][j],d[i+1][j+1]) + a[i][j];
33         }
34         printf("%d\n",d[1][1]);
35     }
36     return 0;
37 }
View Code

 

(2)嵌套矩形

把图先建出来,然后d(i) = max(d(j) + 1)

nyoj 16

技术分享
 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath> 
 5 #include<stack>
 6 #include<vector>
 7 #include<map> 
 8 #include<set>
 9 #include<queue> 
10 #include<algorithm>  
11 using namespace std;
12 
13 typedef long long LL;
14 const int INF = (1<<30)-1;
15 const int mod=1000000007;
16 const int maxn=1000005;
17 
18 int g[1005][1005];
19 int d[1005];
20 int n;
21 
22 struct node{
23     int x,y;
24 }a[maxn];
25 
26 int dp(int i){
27     int& ans = d[i];
28     if(ans > 0) return ans;
29     ans = 1;
30     for(int j = 1;j <= n;j++) 
31     if(g[i][j]) ans = max(ans,dp(j) + 1);
32     
33     return ans;
34 }
35 
36 int work(){
37     int res = -1;
38     for(int i = 1;i <= n;i++) res = max(res,dp(i));
39     return res;
40 }
41 
42 int main(){
43     int T;
44     scanf("%d",&T);
45     while(T--){
46         scanf("%d",&n);
47         for(int i = 1;i <= n;i++) scanf("%d %d",&a[i].x,&a[i].y);
48         memset(g,0,sizeof(g));
49         memset(d,0,sizeof(d));
50         
51         for(int i = 1;i <= n;i++){
52             for(int j = 1;j <= n;j++){
53                 if((a[i].x > a[j].x && a[i].y > a[j].y) || (a[i].x > a[j].y && a[i].y > a[j].x ))
54                 g[i][j] = 1;
55             }
56         }
57         
58         printf("%d\n",work());
59     }
60     return 0;
61 }
View Code

 

Book 动态规划

标签:

原文地址:http://www.cnblogs.com/wuyuewoniu/p/4660004.html

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