本文涉及知識點

C++前後綴分解C++堆(優先隊列) 對頂堆

P12570 [UOI 2023] An Array and Medians of Subarrays

題目描述

對於一個長度為 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++ 的數組,我們將其元素按非遞減順序排序後,第 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_02 位的數字稱為該數組的中位數。例如,數組 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_03【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_04【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_05 的中位數分別是 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_06【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#前後綴分解_07【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_08

給定一個長度為偶數 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_09 的整數數組 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_10

判斷是否可以將 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_10 分割成若干個長度為奇數的子數組,使得所有這些子數組的中位數都相等。

形式化地説,你需要判斷是否存在一個整數序列 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#洛谷_12,滿足以下條件:

  • 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#前後綴分解_13
  • 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#洛谷_14
  • 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#前後綴分解_15,其中 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#前後綴分解_16 表示由元素 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_17 組成的子數組,【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_18 表示數組 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_19

輸入格式

第一行包含一個偶數 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_09【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#洛谷_21)——數組的長度。

第二行包含 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_09 個整數 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#前後綴分解_23【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_24)——數組的元素。

保證 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_09

輸出格式

如果可以將 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_10 分割成若干個長度為奇數的子數組,且這些子數組的中位數都相等,則輸出 Yes,否則輸出 No

輸入輸出樣例 #1

輸入 #1

4
1 1 1 1

輸出 #1

Yes

輸入輸出樣例 #2

輸入 #2

6
1 2 3 3 2 1

輸出 #2

Yes

輸入輸出樣例 #3

輸入 #3

6
1 2 1 3 2 3

輸出 #3

No

説明/提示

在第一個樣例中,數組 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_27 可以分割為 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_03【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_29,它們的中位數均為 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_06

在第二個樣例中,數組 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_31 可以分割為 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#洛谷_32【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_33,它們的中位數均為 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_34

在第三個樣例中,數組 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_35

評分標準

  • 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_36 分):【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_37
  • 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_38 分):對於 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#洛谷_39【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#前後綴分解_40
  • 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_41 分):對於 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_42【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#前後綴分解_43
  • 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_44 分):對於 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#洛谷_39【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_46,且每個值在 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_19 中出現的次數不超過 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_48
  • 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_49 分):【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#洛谷_50
  • 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_51 分):【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_52
  • 【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#對頂堆_53

翻譯由 DeepSeek V3 完成。

前後綴分解 + 對頂堆

性質一:三個相鄰子數組中位數相同,合併成一個子數組後。中位數不變。令這三個子數組長度分別位【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#前後綴分解_54,令中位數位K。則合併後:除3個K外,有m+n+o個數【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_55K,m+n+o個數【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_56。故中位數仍然是K。
推論一:只需要考慮分成一個子數組,或兩個子數組。達到三個子數組,任選三個合併。
推論二:由於【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_57,故只需要考慮2個子數組。

實現

pre[n]記錄A長度為n的前綴的中位數,suff[n]記錄A長度位n的後綴的中位數。
求中位數,我至少想到3個方法:
一,利用對頂堆求中位數。
二,mulset記錄子數組,it指向中位數。如果兩個數【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_58,則it++,如果1個數【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#算法_58,不變。如果0個數【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#c++_60*it,–it。
三,二分樹狀數組(線段樹)。

代碼

核心代碼

#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 T1, class T2, class T3, class T4, class T5 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5>& t) {
	in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t);
	return in;
}

template<class T1, class T2, class T3, class T4, class T5, class T6 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5, T6>& t) {
	in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(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();
		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;
};

class Solution {
public:
	bool Ans(vector<int>& A) {
		const int N = A.size();
		auto Pre = [&](const vector<int>& nums) {
			vector<int> ret(N + 1);
			multiset<int> s = { nums[0] };
			auto it = s.begin();
			ret[1] = *it;
			for (int i = 3;i <= N;i += 2) {
				const int iMin = min(nums[i - 1], nums[i - 2]);
				const int iMax = max(nums[i - 1], nums[i - 2]);
				s.emplace(iMin);s.emplace(iMax);
				if (iMin >= *it) {
					it++;
				}
				else if (iMax < *it) {
					--it;
				}
				ret[i] = *it;
			}
			return ret;
		};
		auto pre = Pre(A);
		auto suff = Pre(vector<int>(A.rbegin(), A.rend()));
		for (int i = 1; i <= N;i += 2) {
			if (pre[i] == suff[N - i]) {
				return true;
			}
		}
		return false;
	}
};

int main() {
#ifdef _DEBUG
	freopen("a.in", "r", stdin);
#endif // DEBUG		
	auto A = Read<int>();
#ifdef _DEBUG		
	//printf("K=%d", K);
	Out(A, "A=");
	//Out(abcd, "abcd=");
#endif // DEBUG	
	auto res = Solution().Ans(A);
	cout << (res?"Yes":"No") << "\n";
	return 0;
}

單元測試

vector<int> A;
		TEST_METHOD(TestMethod11)
		{
			A = { 1,1,1,1 };
			auto res = Solution().Ans(A);
			AssertEx(true, res);
		}
		TEST_METHOD(TestMethod12)
		{
			A = { 1,2,3,3,2,1 };
			auto res = Solution().Ans(A);
			AssertEx(true, res);
		}
		TEST_METHOD(TestMethod13)
		{
			A = { 1,2,1,3,2,3 };
			auto res = Solution().Ans(A);
			AssertEx(false, res);
		}



測試環境

操作系統:win7 開發環境: VS2019 C++17

或者 操作系統:win10 開發環境: VS2022 C++17

如無特殊説明,本算法用**C++**實現。

【前後綴分解 對頂堆】P12570 [UOI 2023] An Array and Medians of Subarrays|普及+_#前後綴分解_61