【动态规划】P3609 [USACO17JAN] Hoof, Paper, Scissor G

本文涉及知识点

C++动态规划

P3609 USACO17JAN Hoof, Paper, Scissor G

题目背景

本题与 银组同名题目 在题意上一致,唯一的差别在于对变手势次数的限制。

题目描述

你可能玩过"石头,剪刀,布",这个游戏在奶牛中同样流行,不过它的名字变成了"蹄子,剪刀,布"。

"蹄子,剪刀,布"和"石头,剪刀,布"的规则十分类似,两只奶牛数到三,然后出一个代表蹄子,剪刀或布的手势。蹄子胜过剪刀,剪刀胜过布,布胜过蹄子。特别地,如果两只奶牛的手势相同,则视为平局。

现在 FJ 和 Bassie 要进行 N N N 轮对抗。Bassie 已经预测了 FJ 每一轮要出的手势。然而 Bassie 很懒,她最多只想变换 K K K 次手势。

现在请你帮 Bassie 求出她最多能赢多少轮。

输入格式

第一行输入两个整数 N , K N,K N,K( 1 ≤ N ≤ 10 5 1 \leq N \leq 10^5 1≤N≤105, 0 ≤ K ≤ 20 0 \leq K \leq 20 0≤K≤20)。

接下来 N N N 行,每行一个字母,代表 FJ 这一轮出的手势。H 代表蹄子(Hoof),S 代表剪刀(Scissors),P 代表布(Paper)。

输出格式

输出一个整数,代表 Bassie 在最多变换 K K K 次手势的前提下最多赢多少轮。

输入输出样例 #1

输入 #1

复制代码
5 1
P
P
H
P
S

输出 #1

复制代码
4

P3609 动态规划 状态机动态规划

动态规划的状态表示

dpijk记录如下状态最多可以赢多少轮。i ∈ \in ∈0,N,j ∈ \in ∈0,2 , 分别代表三种状态。 k ,分别代表三种状态。 k ,分别代表三种状态。k\in$0,K,表示改变过多少次手势。

可以用滚动向量优化空间。pre=dpi,cur=dpi+1。空间复杂度:O(NK)

动态规划的填表顺序

枚举前置状态。i = 0 to N-1,j =0 to 2, k = 0 to K 。

动态规划的转移方程

每种状态都有三种新状态,就是当前回合的3种手势。k1 = k +(j != j1),如果k1 > K ,忽略。

单个状态转移时间复杂度:O(1),总时间复杂度:O(NK)

动态规划的初始值

dp00,1,20=0 ,其它全部是-N。

动态规划的返回值

dpN的最大值。

vWini 记录第i轮赢的状态。

代码

核心代码

cpp 复制代码
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>

#include <bitset>
using namespace std;

template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
	in >> pr.first >> pr.second;
	return in;
}

template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
	in >> get<0>(t) >> get<1>(t) >> get<2>(t);
	return in;
}

template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
	in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
	return in;
}

template<class T = int>
vector<T> Read() {
	int n;
	scanf("%d", &n);
	vector<T> ret(n);
	for (int i = 0; i < n; i++) {
		cin >> ret[i];
	}
	return ret;
}

template<class T = int>
vector<T> Read(int n) {
	vector<T> ret(n);
	for (int i = 0; i < n; i++) {
		cin >> ret[i];
	}
	return ret;
}

template<int N = 1'000'000>
class COutBuff
{
public:
	COutBuff() {
		m_p = puffer;
	}
	template<class T>
	void write(T x) {
		int num[28], sp = 0;
		if (x < 0)
			*m_p++ = '-', x = -x;

		if (!x)
			*m_p++ = 48;

		while (x)
			num[++sp] = x % 10, x /= 10;

		while (sp)
			*m_p++ = num[sp--] + 48;
		AuotToFile();
	}
	void writestr(const char* sz) {
		strcpy(m_p, sz);
		m_p += strlen(sz);
		AuotToFile();
	}
	inline void write(char ch)
	{
		*m_p++ = ch;
		AuotToFile();
	}
	inline void ToFile() {
		fwrite(puffer, 1, m_p - puffer, stdout);
		m_p = puffer;
	}
	~COutBuff() {
		ToFile();
	}
private:
	inline void AuotToFile() {
		if (m_p - puffer > N - 100) {
			ToFile();
		}
	}
	char  puffer[N], * m_p;
};

template<int N = 1'000'000>
class CInBuff
{
public:
	inline CInBuff() {}
	inline CInBuff<N>& operator>>(char& ch) {
		FileToBuf();
		ch = *S++;
		return *this;
	}
	inline CInBuff<N>& operator>>(int& val) {
		FileToBuf();
		int x(0), f(0);
		while (!isdigit(*S))
			f |= (*S++ == '-');
		while (isdigit(*S))
			x = (x << 1) + (x << 3) + (*S++ ^ 48);
		val = f ? -x : x; S++;//忽略空格换行		
		return *this;
	}
	inline CInBuff& operator>>(long long& val) {
		FileToBuf();
		long long x(0); int f(0);
		while (!isdigit(*S))
			f |= (*S++ == '-');
		while (isdigit(*S))
			x = (x << 1) + (x << 3) + (*S++ ^ 48);
		val = f ? -x : x; S++;//忽略空格换行
		return *this;
	}
	template<class T1,class T2>
	inline CInBuff& operator>>(pair<T1,T2>& val) {
		*this >> val.first >> val.second;
		return *this;
	}
	template<class T1, class T2,class T3>
	inline CInBuff& operator>>(tuple<T1, T2,T3>& val) {
		*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
		return *this;
	}
	template<class T1, class T2, class T3,class T4>
	inline CInBuff& operator>>(tuple<T1, T2, T3,T4>& val) {
		*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
		return *this;
	}
	template<class T = int>
	inline CInBuff& operator>>(vector<T>& val) {
		int n;
		*this >> n;
		val.resize(n);
		for (int i = 0; i < n; i++) {
			*this >> val[i];
		}
		return *this;
	}
	template<class T = int>
	vector<T> Read(int n) {
		vector<T> ret(n);
		for (int i = 0; i < n; i++) {
			*this >> ret[i];
		}
		return ret;
	}
private:
	inline void FileToBuf() {
		const int canRead = m_iWritePos - (S - buffer);
		if (canRead >= 100) { return; }
		if (m_bFinish) { return; }
		for(int i = 0 ;i < canRead;i++)
		{
			buffer[i] = S[i];//memcpy出错			
		}
		m_iWritePos = canRead;
		buffer[m_iWritePos] = 0;
		S = buffer;
		int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
		if (readCnt <= 0) { m_bFinish = true; return; }
		m_iWritePos += readCnt;
		buffer[m_iWritePos] = 0;
		S = buffer;
	}
	int m_iWritePos = 0; bool m_bFinish = false;
	char buffer[N + 10], * S = buffer;
};

class Solution {
public:
	int Ans(const vector<char>& a, int K) {
		const int N = a.size();
		vector<int> b;
		for (const auto& ch : a) {
			if ('H' == ch) { b.emplace_back(0); }
			if ('S' == ch) { b.emplace_back(1); }
			if ('P' == ch) { b.emplace_back(2); }
		}
		vector<vector<int>> pre(3, vector<int>(K + 1, -N));
		pre[0][0] = pre[1][0] = pre[2][0] = 0;
		for (int i = 0; i < N; i++) {
			vector<vector<int>> cur(3, vector<int>(K + 1, -N));
			for (int j = 0; j < 3; j++) {
				for (int k = 0; k <= K; k++) {
					for (int j2 = 0; j2 < 3; j2++) {
						const int k2 = k + (j != j2);
						if (k2 > K) { continue; }
						cur[j2][k2] = max(cur[j2][k2], pre[j][k] + (j2 == b[i]));
					}
				}
			}
			pre.swap(cur);
		}
		int ans = 0;
		for (const auto& v : pre) {
			for (const auto& i : v) {
				ans = max(ans, i);
			}
		}
		return ans;
	}
};

int main() {	
#ifdef _DEBUG
	freopen("a.in", "r", stdin);
#endif // DEBUG	
	CInBuff<> ib;
	int n, k;
	ib >> n >> k;
	vector<char> a(n);
	char tmp;
	for (int i = 0; i < n; i++) {
		ib >> a[i] >> tmp;
	}
#ifdef _DEBUG		
	printf("k=%d,", k);
	Out(a, ",a=");
	/*Out(edge, "edge=");
	Out(que, "que=");*/
#endif // DEBUG	
	auto res = Solution().Ans(a,k);
	cout << res;
	return 0;
}

单元测试

cpp 复制代码
	TEST_METHOD(TestMethod1)
		{
			k = 1, a = { 'P','P','H','P','S' };
			auto res = Solution().Ans(a, k);
			AssertEx(4, res);
		}

扩展阅读

算法为骨,CAD为魂
亲士工具箱:支持中望CAD2024、AutoCad2013及以上,多年承接CAD项目的精华
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
活到老,学到老。明朝中后期,大约50%的进士能当上堂官(副部及更高);能当上堂官的举人只有十余人。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。

https://edu.csdn.net/course/detail/38771

如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程

https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17

或者 操作系统:win10 开发环境: VS2022 C++17

如无特殊说明,本算法用**C++**实现。

相关推荐
倒头就睡的小比特3 天前
算法竞赛C++常用的STL
c++·算法
weilx12343 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼3 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
Smileyqp沛沛3 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光3 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark3 天前
大规模并行计算中的负载均衡算法研究4
算法
吞下星星的少年·-·3 天前
C++ 萌新语法入门篇
c++·算法比赛
Because_of_Her13 天前
并查集-听课笔记
笔记·算法·并查集