【组合数学】P9418 [POI 2021/2022 R1] Impreza krasnali|普及+

本文涉及知识点

组合数学汇总

P9418 [POI 2021/2022 R1] Impreza krasnali

题目背景

译自 XXIX Olimpiada Informatyczna -- I etap Impreza krasnali

题目描述

有 n n n 个人依次站成一排,每个人手上都有一个数字,这 n n n 个数字形成一个排列。

每个人会二选一地报告他左边或右边的人手上的数字。注意第一个人与第 n n n 个人并不相邻,所以第一个人总是会报告第二个人的数字,第 n n n 个人总是会报告第 n − 1 n-1 n−1 个人的数字。

现在你得到了 n n n 个人报告的数字,求出原先的排列有多少种可能。

输入格式

第一行一个正整数 n n n。

第二行 n n n 个整数,表示每个人报告的数字。

输出格式

一行一个整数,你的答案模 1 0 9 + 7 10^9+7 109+7。

输入输出样例 #1

输入 #1

复制代码
5
3 4 3 4 1

输出 #1

复制代码
2

输入输出样例 #2

输入 #2

复制代码
见附件

输出 #2

复制代码
0

输入输出样例 #3

输入 #3

复制代码
见附件

输出 #3

复制代码
1

说明/提示

对于所有数据, 2 ≤ n ≤ 100000 2\leq n\leq 100000 2≤n≤100000。

子任务编号 附加限制 分数
1 n ≤ 10 n\leq 10 n≤10 12
2 n ≤ 20 n\leq 20 n≤20 30
3 n ≤ 1000 n\leq 1000 n≤1000 30
4 28

[POI 2021/2022 R1] Impreza krasnali 组合数学

a[i]是第i个人报的数,令b是结果,b[i]=-1表示没找到解。

如果有三个人或更多的人报相同的数字无解。如果两个人报同一个数字,且间距不是2,无解。

以下解是确定的:b[1]=a[0] b[N-2]=a[N-1]。a[i]==a[i+2],则b[i+1]=a[i]。当值确定时{i,新b[i]}入队。

循环处理队列:如果新旧b[i]矛盾,返回0。 如果 a [ i − 1 ] ≠ b [ i ] a[i-1]\neq b[i] a[i−1]=b[i],则b[i-2]=a[i-1],{i-1}入队。如果 a [ i + 1 ] ≠ b [ i ] a[i+1] \neq b[i] a[i+1]=b[i]则 b[i+2]=a[i+1],{i+1,对应b值}入队。

可能会新旧b[i]矛盾,比如:{1,2,3},所以要排除。矛盾:旧b[i]不是-1,且和新b[i]不等。

一,b[i]是-1,且i是奇数,相邻的i合并。二,b[i]是-1,且i是偶数,相邻的i合并。只需要记录各区间的长度,令区间数是c1。

令某个区间长度是len1,起始下标是i1。则a[i1-1]要么不存在,要么等于b[i1-2],不会和a[i]相同;同理a[i1+len22-2]不会和b[i1+len2 2-1]相等,即任意区间b多比a多一个数。

任意b区间都需要引入一个数,且这样的数,不属于任意区间,共c1个。分配这c1个数到各区间有c1的阶乘种方案。

任意长度的区间,可以将x引入到任意处,x的位置确定后,区间的其它数也确定了。x确定后,x的邻居确定,形成连锁反应。

故答案: Π ( 各区间 b 的长度 ) × c 1 的阶乘 \Pi (各区间b的长度) \times c1的阶乘 Π(各区间b的长度)×c1的阶乘

代码

核心代码

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;;
};

template<class T >
class CFactorial
{
public:
	CFactorial(int n) :m_res(n + 1) {
		m_res[0] = 1;
		for (int i = 1; i <= n; i++) {
			m_res[i] = m_res[i - 1] * i;
		}
	}
	T Com(int iSel, int iCanSel)const {
		return m_res[iCanSel] / m_res[iSel] / m_res[iCanSel - iSel];
	}
	T Com(const vector<int>& cnt)const {
		T biRet = 1;
		int iCanSel = std::accumulate(cnt.begin(), cnt.end(), 0);
		for (int j = 0; j < cnt.size(); j++) {
			biRet *= Com(cnt[j], iCanSel);
			iCanSel -= cnt[j];
		}
		return biRet;
	}
	vector<T> m_res;
};

	typedef C1097Int<> BI;
		class Solution {
		public:
			int Ans(vector<int>& a) {		
				const int N = a.size();
				vector<int> b(N, -1);
				queue<pair<int, int>> que;
				que.emplace(1, a[0]);
				que.emplace(N - 2, a[N - 1]);
				vector<vector<int>> inx(N + 1);
				for (int i = 0; i < N; i++) {
					inx[a[i]].emplace_back(i);
				}
				for (const auto& v : inx) {
					if (v.size() > 2) { return 0; }
					if (2 == v.size()) {
						if (v[0] + 2 != v[1]) { return 0; }
						que.emplace(v[0]+ 1, a[v[0]]);
					}
				}
				while (que.size()) {
					const auto [i, val] = que.front(); que.pop();
					if ((-1 != b[i]) && (val != b[i])) { return 0; }
					b[i] = val;
					if ((i > 0) && (a[i - 1] != b[i])) {
						que.emplace(i - 2, a[i - 1]);
					}
					if ((i + 1 < N) && (a[i + 1] != b[i])) {
						que.emplace(i + 2, a[i + 1]);
					}
				}
				auto v1 = Do(b, 0);
				auto v2 = Do(b, 1);
				v1.insert(v1.end(), v2.begin(), v2.end());
				BI ret(1);
				for (const auto& [t, len] : v1) {
					ret *= len;
				}
				CFactorial<BI> fac(v1.size());
				return (ret * fac.m_res.back()).ToInt();
			}
			vector<pair<int, int>> Do(const vector<int>& b, int begin) {
				vector<pair<int, int>> beginLen;
				for (int i = begin; i < b.size(); i += 2) {
					if (-1 != b[i]) { continue; }
					if (beginLen.size() && (beginLen.back().first + 2 * beginLen.back().second == i)) {
						beginLen.back().second++;
					}
					else {
						beginLen.emplace_back(i, 1);
					}
				}
				return beginLen;
			}
		};

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 a = Read<int>();
#ifdef _DEBUG		
		//printf("N=%d", N);
		Out(a,",a=");
		//Out(ab, ",ab=");
		//Out(B, "B=");
		//Out(que, "que=");
		//Out(B, "B=");
#endif // DEBUG		
		auto res = Solution().Ans(a);
		cout << res << "\n";
		
	return 0;
};

单元测试

cpp 复制代码
vector<int> a;
		TEST_METHOD(TestMethod01)
		{
			a = { 1,2,3 };
			auto res = Solution().Ans(a);
			AssertEx(0, res);
		}
		TEST_METHOD(TestMethod02)
		{
			a = { 1,1,3 };
			auto res = Solution().Ans(a);
			AssertEx(0, res);
		}
		TEST_METHOD(TestMethod03)
		{
			a = { 1,1,1 };
			auto res = Solution().Ans(a);
			AssertEx(0, res);
		}
		TEST_METHOD(TestMethod04)
		{
			a = { 3,4,3,4,1 };
			auto res = Solution().Ans(a);
			AssertEx(2, res);
		}
		TEST_METHOD(TestMethod05)
		{
			a = { 1,2};
			auto res = Solution().Ans(a);
			AssertEx(1, res);
		}
		TEST_METHOD(TestMethod06)
		{
			a = { 7,9,7,8,1,3,6,5,6 };
			auto res = Solution().Ans(a);
			AssertEx(20, 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++**实现。

相关推荐
晨曦夜月2 小时前
头文件与目标文件的关系
linux·开发语言·c++
刃神太酷啦2 小时前
C++ list 容器全解析:从构造到模拟实现的深度探索----《Hello C++ Wrold!》(16)--(C/C++)
java·c语言·c++·qt·算法·leetcode·list
有点。3 小时前
C++ ⼀级 2023 年09 ⽉
c++
LXS_3573 小时前
Day 16 C++提高之模板
开发语言·c++·笔记·学习方法
wyw00003 小时前
鸿蒙开发-如何将C++侧接收的PixelMap转换成cv::mat格式
c++·华为·harmonyos
liulilittle3 小时前
C++ CRTP 替代虚函数
数据结构·c++·算法
buyue__3 小时前
C++实现数据结构——队列
数据结构·c++
NZT-483 小时前
C++基础笔记(二)队列deque,queue和堆priority_queue
java·c++·笔记
玉树临风ives4 小时前
atcoder ABC436 题解
c++·算法·leetcode·atcoder·信息学奥赛