洛谷 #4401. 矿工配餐

本文探讨了一个关于矿车调度的问题,通过动态规划(DP)算法优化矿车在两个矿洞间的运行,以最大化近三次不同食物类型的矿产量。文章详细介绍了状态转移方程的设计与实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意

n辆矿车去两个矿洞,最近3次食物不同的种数为矿产量,有3种食物,问最大矿产量

题解

Dp,f[i][a1][a2][a3][a4] 表示处理到第i辆矿车,1号矿洞和2号矿洞前两次的食物为a1,a2,a3,a4

调试记录

算val时要把当前的放在最前面(因为函数的判断)
#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 100005

using namespace std;

int f[2][4][4][4][4];
int n, a[maxn];

int val(int a, int b, int c){
	int res = 1;
	if (a && a != b && a != c) res++;
	if (b && b != c) res++;
	return res;
}

int main(){
	scanf("%d", &n);
	
	memset(f, -1, sizeof f);
	char s[maxn];
	scanf("%s", s + 1);
	for (int i = 1; i <= n; i++)
		if (s[i] == 'M') a[i] = 1;
		else if (s[i] == 'B') a[i] = 2;
		else if (s[i] == 'F') a[i] = 3;
	
	f[0][0][0][0][0] = 0;
	for (int i = 1; i <= n; i++){
		for (int pre1 = 0; pre1 < 4; pre1++)
			for (int pre2 = 0; pre2 < 4; pre2++)
				for (int pre3 = 0; pre3 < 4; pre3++)
					for (int pre4 = 0; pre4 < 4; pre4++){
						if (f[(i + 1) % 2][pre1][pre2][pre3][pre4] == -1) continue;
						f[i % 2][pre2][a[i]][pre3][pre4] = max(f[i % 2][pre2][a[i]][pre3][pre4], f[(i + 1) % 2][pre1][pre2][pre3][pre4] + val(pre1, pre2, a[i]));
						f[i % 2][pre1][pre2][pre4][a[i]] = max(f[i % 2][pre1][pre2][pre4][a[i]], f[(i + 1) % 2][pre1][pre2][pre3][pre4] + val(pre3, pre4, a[i]));
					}
		memset(f[(i + 1) % 2], -1, sizeof(f[(i + 1) % 2]));
	}
	int ans = 0;
	for (int pre1 = 0; pre1 < 4; pre1++)
			for (int pre2 = 0; pre2 < 4; pre2++)
				for (int pre3 = 0; pre3 < 4; pre3++)
					for (int pre4 = 0; pre4 < 4; pre4++)
						ans = max(ans, f[n % 2][pre1][pre2][pre3][pre4]);
	
	printf("%d\n", ans);
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值