无归期

朴素搜索 30分 tle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<cstdio>
#include<iostream>
using namespace std;
int n,m;
int q;
int dp[99999];
int a[999999];
int f[1999];
int max1=-9999999;
void dfs(int x,int a,int b,int c,int d,int ans)
{
if(x==n)
{
max1=max(max1,ans);//修改结果
return ;
}
if(a&&x+1<=n)
dfs(x+1,a-1,b,c,d,ans+dp[x+1]);
if(b&&x+2<=n)
dfs(x+2,a,b-1,c,d,ans+dp[x+2]);
if(c&&x+3<=n)
dfs(x+3,a,b,c-1,d,ans+dp[x+3]);
if(d&&x+4<=n)
dfs(x+4,a,b,c,d-1,ans+dp[x+4]);//四个方向

}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&dp[i]);
}
for(int i=1;i<=m;i++)
{
int x;
scanf("%d",&x);
f[x]++;
}
dfs(1,f[1],f[2],f[3],f[4],dp[1]);
printf("%d",max1);
return 0;
}

记忆化搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<cstdio>
#include<iostream>
using namespace std;
int n,m;
int q;
int dp[99999];
int a[999999];
int f[1999];
int max1=-9999999;
int dpp[40][40][40][40];//dp
int dfs(int x,int a,int b,int c,int d)
{
if(x==n)
{
return 0;
}
if(a)
dpp[a][b][c][d]=
max(dpp[a][b][c][d],
(dpp[a-1][b][c][d]?
dpp[a-1][b][c][d]:
dfs(x+1,a-1,b,c,d))+
dp[x+1]);
if(b)
dpp[a][b][c][d]=
max(dpp[a][b][c][d],
(dpp[a][b-1][c][d]?
dpp[a][b-1][c][d]:
dfs(x+2,a,b-1,c,d))+
dp[x+2]);
if(c)
dpp[a][b][c][d]=
max(dpp[a][b][c][d],
(dpp[a][b][c-1][d]?
dpp[a][b][c-1][d]:
dfs(x+3,a,b,c-1,d))+
dp[x+3]);
if(d)
dpp[a][b][c][d]=
max(dpp[a][b][c][d],
(dpp[a][b][c][d-1]?
dpp[a][b][c][d-1]:
dfs(x+4,a,b,c,d-1))+
dp[x+4]);
return dpp[a][b][c][d];
}//四个方向记忆化搜索
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&dp[i]);
}
for(int i=1;i<=m;i++)
{
int x;
scanf("%d",&x);
f[x]++;
}

printf("%d",dfs(1,f[1],f[2],f[3],f[4])+dp[1]);//加上第一个位置的
return 0;
}

动规代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<cstdio>
#include<iostream>
using namespace std;
int n,m;
int q;
int dp[99999];
int a[999999];
int f[1999];
int max1=-9999999;
int dpp[40][40][40][40];//dp
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&dp[i]);
}
for(int i=1;i<=m;i++)
{
int x;
scanf("%d",&x);
f[x]++;
}
dpp[0][0][0][0]=dp[1];
for(int i=0;i<=f[1];i++)
for(int j=0;j<=f[2];j++)
for(int k=0;k<=f[3];k++)
for(int q=0;q<=f[4];q++)
{
dpp[i][j][k][q]=
max(dpp[i-1][j][k][q],
max(dpp[i][j-1][k][q],
max(dpp[i][j][k-1][q],
dpp[i][j][k][q-1])))
+dp[1+i*1+j*2+k*3+q*4];//取这几张牌走到的地址
}
printf("%d",dpp[f[1]][f[2]][f[3]][f[4]]);
return 0;
}

这题我一开始写的动规,不过错了。。。。
思路应该对,可代码错了,我就按搜索->记忆化搜索->动规的方式做的才做出来

最近的文章

能量项链

1234567891011121314151617181920212223242526272829303132#include&lt;cstdio&gt;#include&lt;iostream&gt;using namespace std;int n;int f[1999][1999];int a …

于  dp 继续阅读
更早的文章

烦人的幻灯片

将入度为一的区间(也就是唯一匹配的点和区间)找出来,把这个点所有出度的入度–,给这个点做标记,输出继续执行以上操作123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 …

于  拓补排序 继续阅读