NOIP DP 20题训练

第1题 Luogu P2734 游戏 A Game

Luogu P2734 游戏 A Game
题意:有一个序列,有两个玩家,每个玩家用最优策略在两端拿数,求先手拿到数字和的最大值。
解:由于两个玩家都采取最优策略,我们设$dp(i,j)$为区间$[i,j]$先手能得到的最优解。然后方程为$dp(i,j)=max(sum(i,j) - dp(i + 1, j), sum(i,j)-dp(i,j-1))$。$sum$是这一段的和。原理是区间DP的原理,整个区间$[i,j]$的先手,在$(i,j],[i,j)$是后手,$dp(i + 1, j), dp(i,j-1)$是$(i,j],[i,j)$的先手最优值

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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
int n, a[105], dp[105][105], sum[105][105];
void clean() {
ms(dp, 0);
}
int solve() {
clean();
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), dp[i][i] = a[i];
for (int i = 1; i <= n; i++) {
sum[i][i] = a[i];
for (int j = i + 1; j <= n; j++) sum[i][j] = sum[i][j - 1] + a[j];
}
for (int i = n; i; i--) {
for (int j = i + 1; j <= n; j++) {
dp[i][j] = max(sum[i][j] - dp[i + 1][j], sum[i][j] - dp[i][j - 1]);
}
}
printf("%d %d\n", dp[1][n], sum[1][n] - dp[1][n]);
return 0;
}
int main() {
scanf("%d", &n), solve();
return 0;
}

第2题 Luogu P3800 Power收集

Luogu P3800 Power收集
题意:$N$行$M$列的格子,某些点有一些权值,每秒钟可以左右移动至多$T$个单位长度(瞬间完成),每秒必须向下走一行(不能折返),求一条路径使得权值和最大
解:朴素的DP是$O(n^3)$的,无法通过测试,我们将有权值的点按横坐标排序,然后设$dp(i)$为第$i$个权值点的最优解,那么就有 $dp(i)=dp(j)+v(i)$当且仅当$j$能移动到$i$位置,$v(i)$为$i$权值点的权值
然后初始化第一层的值就行

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
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
struct data {
int x, y, v, dp;
bool operator < (const data &b) const {
return x < b.x;
}
}v[4000 + 5];
int n, m, k, t;
int abss(int x) {return x > 0 ? x : -x;}
void clean() {}
int solve() {
clean();
for (int i = 1; i <= k; i++) scanf("%d%d%d", &v[i].x, &v[i].y, &v[i].v), v[i].dp = 0;
sort(v + 1, v + 1 + k);
for (int i = 1; ; i++) if (v[i].x != v[i - 1].x && i != 1) break; else v[i].dp = v[i].v;
for (int i = 1; i <= k; i++)
for (int j = 0; j < i; j++)
if (abss(v[i].y - v[j].y) <= t * (v[i].x - v[j].x)) v[i].dp = max(v[i].dp, v[j].dp + v[i].v);
int ans = 0;
for (int i = 1; i <= k; i++) ans = max(ans, v[i].dp);
printf("%d\n", ans);
return 0;
}
int main() {
scanf("%d%d%d%d", &n, &m, &k, &t), solve();
return 0;
}

第3题 Luogu P2889 挤奶的时间Milking Time

Luogu P2889 挤奶的时间Milking Time
题意:有$m$个区间,区间有一个权值,现在要选择一些区间使得权值和最大,选取的每两个区间之间不能覆盖且距离为$r$
解:区间按右端点排序,消除后效性,设$dp(i)$为$i$前$i$个区间的最优值,转移方程
$$dp(i)=max(dp(j)+e(i)|x_i-r \geq y_j)$$

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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
struct data {
int x, y, v;
bool operator < (const data &b) const {
return y < b.y;
}
}inv[1005];
int n, m, r, dp[1005];
void clean() {
ms(dp, 0);
}
int solve() {
clean();
for (int i = 1; i <= m; i++) scanf("%d%d%d", &inv[i].x, &inv[i].y, &inv[i].v);
sort(inv + 1, inv + 1 + m);
for (int i = 1; i <= m; i++) dp[i] = inv[i].v;
for (int i = 1; i <= m; i++)
for (int j = 1; j < i; j++)
if (inv[i].x - r >= inv[j].y) dp[i] = max(dp[i], dp[j] + inv[i].v);
else dp[i] = max(dp[i], dp[j]);
printf("%d\n", dp[m]);
return 0;
}
int main() {
scanf("%d%d%d", &n, &m, &r), solve();
return 0;
}

第4题 Luogu P2233 [HNOI2002]公交车路线

Luogu P2233 [HNOI2002]公交车路线

题意:有一个ABCDEFGH组成的环,求A到E路程为$n$的方案数,要求到达E之前不能到达E
解:遇到环就拆成链,在E点切开,那么成了一条链FGHABCD,到达E点路径总数等于F点和D点的路径总数,把链节点抽象为数字点,设$dp(i,j)$为$j$路程后到$i$点的方案数,则$dp(i,j)=dp(i+1, j-1), dp(i - 1,j-1)$,初始化$dp(4, 0)$为1(从1开始标号)。

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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
const int MO = 1000;
int n, dp[8][2];
void clean() {}
int solve() {
clean();
dp[4][0] = 1;
int pos = 0;
for (int i = 1; i < n; i++) {
pos ^= 1;
for (int j = 1; j <= 7; j++) dp[j][pos] = (dp[j - 1][pos ^ 1] + dp[j + 1][pos ^ 1]) % MO;
}
printf("%d\n", (dp[1][pos] + dp[7][pos]) % MO);
return 0;
}
int main() {
scanf("%d", &n), solve();
return 0;
}
------ 本文结束 ------