本文涉及知识点
C++算法:前缀和、前缀乘积、前缀异或的原理、源码及测试用例 包括课程视频
P8810 [蓝桥杯 2022 国 C] 数组个数
题目描述
小蓝有一个长度为 n n n 的数组 B = ( b 0 , b 1 , ⋯ , b n − 1 ) B = (b_0,b_1,\cdots,b_{n−1}) B=(b0,b1,⋯,bn−1),数组 B B B 是由另一个长度为 n n n 的环形数组 A = ( a 0 , a 1 , ⋯ , a n − 1 ) A = (a_0,a_1,\cdots,a_{n−1}) A=(a0,a1,⋯,an−1) 经过一次相邻最大化操作得到的,其中 a i a_i ai 与 a i + 1 a_{i+1} ai+1 相邻, a 0 a_0 a0 与 a n − 1 a_{n−1} an−1 相邻。
形式化描述为:
b i = { max { a n − 1 , a 0 , a 1 } i = 0 max { a i − 1 , a i , a i + 1 } 0 < i < n − 1 max { a n − 2 , a n − 1 , a 0 } i = n − 1 b_i= \begin{cases} \max\{a_{n-1},a_0,a_1\}& i=0\\ \max\{a_{i-1},a_i,a_{i+1}\}& 0<i<n-1\\ \max\{a_{n-2},a_{n-1},a_0\}& i=n-1\\ \end{cases} bi=⎩ ⎨ ⎧max{an−1,a0,a1}max{ai−1,ai,ai+1}max{an−2,an−1,a0}i=00<i<n−1i=n−1
小蓝想知道,可能有多少个满足条件的数组 A A A,经过一次相邻最大化操作后能得到数组 B B B,注意 A A A 中的每个元素都要求为非负整数。
输入格式
输入的第一行包含一个整数 n n n,表示数组长度。
第二行包含 n n n 个整数 b 0 , b 1 , ⋯ , b n − 1 b_0,b_1,\cdots,b_{n−1} b0,b1,⋯,bn−1,相邻两个整数之间用一个空格分隔。
输出格式
输出一行包含一个整数表示答案,答案可能很大,请输出答案除以 1000000007 1000000007 1000000007(即 10 9 + 7 10^9+7 109+7)后的余数。
输入输出样例 #1
输入 #1
5
8 6 1 8 8
输出 #1
7
说明/提示
【样例说明】
可能的 A A A 数组有 7 7 7 个 : ( 6 , 0 , 0 , 1 , 8 ) (6,0,0,1,8) (6,0,0,1,8)、 ( 6 , 0 , 1 , 0 , 8 ) (6,0,1,0,8) (6,0,1,0,8)、 ( 6 , 0 , 1 , 1 , 8 ) (6,0,1,1,8) (6,0,1,1,8)、 ( 6 , 1 , 0 , 0 , 8 ) (6,1,0,0,8) (6,1,0,0,8)、 ( 6 , 1 , 0 , 1 , 8 ) (6,1,0,1,8) (6,1,0,1,8)、 ( 6 , 1 , 1 , 0 , 8 ) (6,1,1,0,8) (6,1,1,0,8)、 ( 6 , 1 , 1 , 1 , 8 ) (6,1,1,1,8) (6,1,1,1,8)。
【评测用例规模与约定】
对于 30 % 30\% 30% 的评测用例, 3 ≤ n ≤ 10 3≤n≤10 3≤n≤10;
对于 60 % 60\% 60% 的评测用例, 3 ≤ n ≤ 100 3≤n≤100 3≤n≤100;
对于所有评测用例, 3 ≤ n ≤ 1000 3 ≤ n ≤ 1000 3≤n≤1000, 0 ≤ b i ≤ 10 0 ≤ b_i ≤ 10 0≤bi≤10。
蓝桥杯 2022 国赛 C 组 G 题。
P8810 动态规划+前缀和+化环为链
注意 : 0 ≤ b i ≤ 10 0 ≤ b_i ≤ 10 0≤bi≤10。 a中元素不可能大于10,否则b中至少有三个元素大于10。
假定不是环:动态规划
动态规划的状态表示
dp[n][i][j]记录不矛盾的数量,n数组已经处理的长度,i是a[n-2]的值,j是a[n-1]的值, n ∈ [ 2 , N ] , i , j ∈ [ 0 , 10 ] n\in[2,N], i,j\in[0,10] n∈[2,N],i,j∈[0,10] 空间复杂度:O(N100)。
动态规划的填表顺序
枚举前置状态, n=2 to N-1,i,j从小到大
动态规划的转移方程
通过j1枚举a[n] 如果b[n-2]=max(i,j,j1),则加上数量。否则忽略。单个状态时间复杂度:O(10),总时间复杂度O(N1000)。
动态规划的初始值
dp[2][i][j]=1
动态规划的返回值
dp.back()之和。
性能优化
枚举后序状态:dp[j][j1]
如果max(j,j1)=b[n-2],则i ∈ [ 0 , b [ n − 2 ] ] \in[0,b[n-2]] ∈[0,b[n−2]] 如果max(j,j1)>b[n-2],无解;如果max(j,j1) <b[n-2],则i只能是b[n-2]。
利用前缀和,单个状态转移时间复杂度降到O(1),总时间复杂度:O(100N)。
化环为链
利用a0和a1枚举a[0]和a[1]。枚举长度N+2的非矛盾串。dp.back[a0][a1]之和便是答案。枚举a0,a1时间复杂度O(100),总时间复杂度:O(10000N)
代码
核心代码
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<array>
#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;
cin >> n;
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
vector<T> ret;
T tmp;
while (cin >> tmp) {
ret.emplace_back(tmp);
if ('\n' == cin.get()) { break; }
}
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();
while (('\r' == *S) || ('\n' == *S) || (' ' == *S)) { S++; }//忽略空格和回车
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;
}
template<class T = int>
vector<T> Read() {
vector<T> ret;
*this >> ret;
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;
};
template<long long MOD = 1000000007, class T1 = int, class T2 = long long>
class C1097Int
{
public:
C1097Int(T1 iData = 0) :m_iData(iData% MOD)
{
}
C1097Int(T2 llData) :m_iData(llData% MOD) {
}
C1097Int operator+(const C1097Int& o)const
{
return C1097Int(((T2)m_iData + o.m_iData) % MOD);
}
C1097Int& operator+=(const C1097Int& o)
{
m_iData = ((T2)m_iData + o.m_iData) % MOD;
return *this;
}
C1097Int& operator-=(const C1097Int& o)
{
m_iData = ((T2)MOD + m_iData - o.m_iData) % MOD;
return *this;
}
C1097Int operator-(const C1097Int& o)const
{
return C1097Int(((T2)MOD + m_iData - o.m_iData) % MOD);
}
C1097Int operator*(const C1097Int& o)const
{
return((T2)m_iData * o.m_iData) % MOD;
}
C1097Int& operator*=(const C1097Int& o)
{
m_iData = ((T2)m_iData * o.m_iData) % MOD;
return *this;
}
C1097Int operator/(const C1097Int& o)const
{
return *this * o.PowNegative1();
}
C1097Int& operator/=(const C1097Int& o)
{
*this /= o.PowNegative1();
return *this;
}
bool operator==(const C1097Int& o)const
{
return m_iData == o.m_iData;
}
bool operator<(const C1097Int& o)const
{
return m_iData < o.m_iData;
}
C1097Int pow(T2 n)const
{
C1097Int iRet = (T1)1, iCur = *this;
while (n)
{
if (n & 1)
{
iRet *= iCur;
}
iCur *= iCur;
n >>= 1;
}
return iRet;
}
C1097Int PowNegative1()const
{
return pow(MOD - 2);
}
T1 ToInt()const
{
return ((T2)m_iData + MOD) % MOD;
}
private:
T1 m_iData = 0;;
};
typedef C1097Int<> BI;
class Solution {
public:
int Ans(vector<int> b) {
N = b.size();
b.emplace_back(b[0]);
BI ans = 0;
for (int a0 = 0; a0 <= 10; a0++) {
for (int a1 = 0; a1 <= 10; a1++) {
auto dp = Do(a0, a1, b);
ans += dp[a0][a1];
}
}
return ans.ToInt();
}
vector<vector<BI>> Do(int a0, int a1, const vector<int>& b) {
vector<vector<BI>> pre(11, vector<BI>(11));
pre[a0][a1] = 1;
for (int n = 3; n <= N + 2; n++) {
vector<vector<BI>> dp(11, vector<BI>(11));
for (int j = 0; j <= 10; j++) {
BI preSum[12];
preSum[0] = 0;
for (int i = 0; i <= 10; i++) {
preSum[i + 1] = preSum[i] + pre[i][j];
}
for (int j1 = 0; j1 <= 10; j1++) {
const int jm = max(j, j1);
const int cb = b[n - 2];
if (jm == cb) { dp[j][j1] = preSum[jm + 1]; continue; }
if (jm > cb) { continue; }
dp[j][j1] = pre[cb][j];
}
}
pre.swap(dp);
}
return pre;
}
int N;
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
//ios::sync_with_stdio(0); cin.tie(nullptr);
CInBuff<> in; COutBuff<10'000'000> ob;
auto b = in.Read<int>();
#ifdef _DEBUG
//printf("D=%d", D);
//Out(b, ",b=");
//Out(que, "que=");
//Out(B, "B=");
//Out(que, "que=");
//Out(B, "B=");
#endif // DEBUG
auto res = Solution().Ans(b);
cout << res << "\n";
return 0;
}
单元测试
cpp
TEST_METHOD(TestMethod11)
{
auto res = Solution().Ans({ 8,6,1,8,8 });
AssertEx(7, res);
}
扩展阅读
| 我想对大家说的话 |
|---|
| 工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
| 学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
| 有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
| 闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
| 子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
| 如果程序是一条龙,那算法就是他的是睛 |
| 失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步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++**实现。