DP测试总结

T1:三取方格数

题目描述

设有N*N的方格图,我们将其中的某些方格填入正整数,
而其他的方格中放入0。
某人从图得左上角出发,可以向下走,也可以向右走,直到到达右下角。
在走过的路上,他取走了方格中的数。(取走后方格中数字变为0)
此人从左上角到右下角共走3次,试找出3条路径,使得取得的数总和最大。

输入

第一行:N (4<=N<=20)
接下来一个N*N的矩阵,矩阵中每个元素不超过80,不小于0

输出

一行,表示最大的总和。

样例输入

4 1 2 3 4 2 1 3 4 1 2 3 4 1 3 2 4

样例输出

39
 
题解:
F[i][j][k][g]表示一共走了i步,第1,2,3条路线的行坐标 知道行坐标 列坐标就是i-x+1
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int N=21;
 7 int gi(){
 8     int str=0;char ch=getchar();
 9     while(ch>'9' || ch<'0')ch=getchar();
10     while(ch>='0' && ch<='9')str=str*10+ch-48,ch=getchar();
11     return str;
12 }
13 int f[N*2][N][N][N],a[N][N];
14 int main()
15 {
16     //freopen("pp.in","r",stdin);
17     int n=gi();
18     for(int i=1;i<=n;i++)
19     for(int j=1;j<=n;j++)
20     a[i][j]=gi();
21     int tmp=0,from,to;
22     for(int i=1;i<n+n;i++)
23     {
24         from=i-n+1>1?i-n+1:1;
25         to=i<n?i:n;
26         for(int j=from;j<=to;j++)
27         {
28             for(int k=from;k<=to;k++)
29             {
30                 for(int g=from;g<=to;g++)
31                 {
32                     tmp=0;
33                     if(f[i-1][j][k][g]>tmp)tmp=f[i-1][j][k][g];
34                     if(f[i-1][j-1][k][g]>tmp)tmp=f[i-1][j-1][k][g];
35                     if(f[i-1][j][k-1][g]>tmp)tmp=f[i-1][j][k-1][g];
36                     if(f[i-1][j][k][g-1]>tmp)tmp=f[i-1][j][k][g-1];
37                     if(f[i-1][j-1][k-1][g]>tmp)tmp=f[i-1][j-1][k-1][g];
38                     if(f[i-1][j-1][k][g-1]>tmp)tmp=f[i-1][j-1][k][g-1];
39                     if(f[i-1][j][k-1][g-1]>tmp)tmp=f[i-1][j][k-1][g-1];
40                     if(f[i-1][j-1][k-1][g-1]>tmp)tmp=f[i-1][j-1][k-1][g-1];
41                     f[i][j][k][g]=tmp+a[j][i-j+1];
42                     if(j!=k)f[i][j][k][g]+=a[k][i-k+1];
43                     if(g!=k && g!=j)f[i][j][k][g]+=a[g][i-g+1]; 
44                 }
45             }
46         }
47     }
48     printf("%d",f[n+n-1][n][n][n]);
49     return 0;
50 }

T2:建造栅栏:

题目描述

勤奋的Farmer John想要建造一个四面的栅栏来关住牛们。他有一块长为n(4<=n<=2500)的木板,他想把这块本板切成4块。这四块小木板可以是任 何一个长度只要Farmer John能够把它们围成一个合理的四边形。他能够切出多少种不同的合理方案。注意: *只要大木板的切割点不同就当成是不同的方案(像全排列那样),不要担心另外的特殊情况,go ahead。 *栅栏的面积要大于0. *输出保证答案在longint范围内。 *整块木板都要用完。

输入

*第一行:一个数n

输出

*第一行:合理的方案总数

样例输入

6

样例输出

6

提示

Farmer John能够切出所有的情况为: (1, 1, 1,3); (1, 1, 2, 2); (1, 1, 3, 1); (1, 2, 1, 2); (1, 2, 2, 1); (1, 3,1, 1);

(2, 1, 1, 2); (2, 1, 2, 1); (2, 2, 1, 1); or (3, 1, 1, 1).

下面四种 -- (1, 1, 1, 3), (1, 1, 3, 1), (1, 3, 1, 1), and (3,1, 1, 1) – 不能够组成一个四边形.

题解:

F[i][j]表示把i分成j份的方案数,要保证合法,就得保证所有的方案中不包括>n/2的边

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long ll;
 7 ll f[2505][5];
 8 int main()
 9 {
10     int n;
11     cin>>n;
12     int from,to,k,pp;
13     if(n%2)k=(n>>1);
14     else k=(n>>1)-1;
15     f[0][0]=1;
16     for(int i=1;i<=n;i++)
17     {
18         from=i<4?i:4;
19         for(int j=1;j<=from;j++)
20         {
21             to=j-1<i-k?i-k:j-1;
22             for(int g=to;g<=i-1;g++)
23             f[i][j]+=f[g][j-1];
24         }
25     }
26     printf("%lld",f[n][4]);
27     return 0;
28 }

T3:

题目描述

Some of you may have played a game called 'Blocks'. There are n blocks in a row, each box has a color. Here is an example: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold.
The corresponding picture will be as shown below

:

If some adjacent boxes are all of the same color, and both the box to its left(if it exists) and its right(if it exists) are of some other color, we call it a 'box segment'. There are 4 box segments. That is: gold, silver, bronze, gold. There are 1, 4, 3, 1 box(es) in the segments respectively.

Every time, you can click a box, then the whole segment containing that box DISAPPEARS. If that segment is composed of k boxes, you will get k*k points. for example, if you click on a silver box, the silver segment disappears, you got 4*4=16 points.

Now let's look at the picture below:

N 个不同颜色的方块,排成一行,左右相邻颜色相同的方块属于同一段。上图有 4 段长度分
别为 1,4,3,1。
每次单击一个方块,包含这个方块的整段将消失。如果该段由 k 个方块组成,则将得到 k* k
分 。 例 如 , 如 果 你 点 击 一 个 银 盒 , 银 色 段 消 失 , 你 有 4 * 4 = 16 分 。

The first one is OPTIMAL.

Find the highest score you can get, given an initial state of this game.

输入

Each case contains two lines. The first line contains an integer n(1<=n<=200), the number of boxes. The second line contains n integers, representing the colors of each box. The integers are in the range 1~n.

输出

For each test case, print the case number and the highest possible score.

样例输入

9 1 2 2 2 2 3 3 3 1

样例输出

29
 
题解:F[i][j][k]表示把i到j和后面和第一段颜色相同的 长度为k的一段合并的最大的得分
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int N=205;
 7 int a[N],ls[N],col[N],f[N][N][N];
 8 int dfs(int l,int r,int len)
 9 {
10     if(l>r)return 0;
11     if(f[l][r][len]!=-1)return f[l][r][len];
12     f[l][r][len]=dfs(l,r-1,0)+(ls[r]+len)*(ls[r]+len);
13     for(int i=l;i<r;i++)
14     if(col[i]==col[r])f[l][r][len]=max(f[l][r][len],dfs(l,i,len+ls[r])+dfs(i+1,r-1,0));
15     return f[l][r][len];
16 }
17 int main()
18 {
19     //freopen("pp.in","r",stdin);
20     int n,to=0,m=0;
21     scanf("%d",&n);
22     memset(f,-1,sizeof(f));
23     a[n+1]=-1;
24     for(int i=1;i<=n;i++)scanf("%d",&a[i]);
25     for(int i=1;i<=n;i++)
26     {
27         if(a[i]==a[i-1])continue;
28         to=i;col[++m]=a[i];ls[m]=1;
29         while(a[i]==a[to+1])to++,ls[m]++;
30     }
31     printf("%d",dfs(1,m,0));
32 }

 T4:[USACO15JAN]电影移动Moovie Mooving

题目描述

Bessie is out at the movies. Being mischievous as always, she has decided to hide from Farmer John for L (1 <= L <= 100,000,000) minutes, during which time she wants to watch movies continuously. She has N (1 <= N <= 20) movies to choose from, each of which has a certain duration and a set of showtimes during the day. Bessie may enter and exit a movie at any time during one if its showtimes, but she does not want to ever visit the same movie twice, and she cannot switch to another showtime of the same movie that overlaps the current showtime.

Help Bessie by determining if it is possible for her to achieve her goal of watching movies continuously from time 0 through time L. If it is, determine the minimum number of movies she needs to see to achieve this goal (Bessie gets confused with plot lines if she watches too many movies).

奶牛贝西想连续看L (1 <= L <= 100,000,000)分钟的电影,有 N (1 <= N <= 20)部电影可供选择,每部电影会在一天的不同时段放映。

贝西可以在一部电影播放过程中的任何时间进入或退出放映厅。但她不愿意重复看到一部电影,所以每部电影她最多看到一次。她也不能在看一部电影的过程中,换到另一个正在播放相同电影的放映厅。

请帮贝西计算她能够做到从0到L分钟连续不断地观看电影,如果能,请计算她最少看几部电影就行了。

输入

The first line of input contains N and L.

The next N lines each describe a movie. They begin with its integer duration, D (1 <= D <= L) and the number of showtimes, C (1 <= C <= 1000). The remaining C integers on the same line are each in the range 0..L, and give the starting time of one of the showings of the movie. Showtimes are distinct, in the range 0..L, and given in increasing order.

输出

A single integer indicating the minimum number of movies that Bessie

needs to see to achieve her goal. If this is impossible output -1

instead.

样例输入

4 100 50 3 15 30 55 40 2 0 65 30 2 20 90 20 1 0

样例输出

3

提示

Bessie should attend the first showing of the fourth movie from time 0 to time 20. Then she watches the first showing of the first movie

from time 20 to time 65. Finally she watches the last showing of the second movie from time 65 to time 100.

题解:

水的状压DP,F[i]表示i状态下最大能消耗的时间 二分优化下就过了

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int N=20;
 7 int gi(){
 8     int str=0;char ch=getchar();
 9     while(ch>'9' || ch<'0')ch=getchar();
10     while(ch>='0' && ch<='9')str=str*10+ch-48,ch=getchar();
11     return str;
12 }
13 int f[1<<N],t[N],c[N],d[N][1005];
14 int pf(int i,int x)
15 {
16     int l=1,r=c[i],mid,ans=0;
17     while(l<=r)
18     {
19         mid=(l+r)>>1;
20         if(d[i][mid]<=x)ans=d[i][mid],l=mid+1;
21         else r=mid-1;
22     }
23     return ans;
24 }
25 int main()
26 {
27     //freopen("pp.in","r",stdin);
28     int n=gi(),l=gi();
29     for(int i=1;i<=n;i++)
30     {
31         t[i]=gi();c[i]=gi();
32         for(int j=1;j<=c[i];j++)d[i][j]=gi();
33     }
34     int to,tmp;
35     for(int j=0;j<=(1<<n)-1;j++)
36     {
37         for(int i=1;i<=n;i++)
38         {
39             if(((1<<(i-1))&j))continue;
40             to=(j|(1<<(i-1)));
41             tmp=pf(i,f[j]);
42             if(tmp+t[i]>f[to])f[to]=tmp+t[i];
43         }
44     }
45     int ans=25,sum=0;
46     for(int j=(1<<n)-1;j>=0;j--)
47     {
48         if(f[j]>l)
49         {
50             sum=0;
51             tmp=j;
52             while(tmp>0)tmp-=(tmp&(-tmp)),sum++;
53             if(sum<ans)ans=sum;
54         }
55     }
56     if(ans!=25)
57     printf("%d",ans);
58     else printf("-1");
59     return 0;
60 }
原文地址:https://www.cnblogs.com/Yuzao/p/6952672.html