本文涉及知識點
C++前後綴分解C++堆(優先隊列) 對頂堆
P12570 [UOI 2023] An Array and Medians of Subarrays
題目描述
對於一個長度為 的數組,我們將其元素按非遞減順序排序後,第
位的數字稱為該數組的中位數。例如,數組
、
和
的中位數分別是
、
和
。
給定一個長度為偶數 的整數數組
。
判斷是否可以將 分割成若干個長度為奇數的子數組,使得所有這些子數組的中位數都相等。
形式化地説,你需要判斷是否存在一個整數序列 ,滿足以下條件:
;
;
,其中
表示由元素
組成的子數組,
表示數組
輸入格式
第一行包含一個偶數 (
)——數組的長度。
第二行包含 個整數
(
)——數組的元素。
保證
輸出格式
如果可以將 分割成若干個長度為奇數的子數組,且這些子數組的中位數都相等,則輸出
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
説明/提示
在第一個樣例中,數組 可以分割為
和
,它們的中位數均為
。
在第二個樣例中,數組 可以分割為
和
,它們的中位數均為
。
在第三個樣例中,數組
評分標準
- (
分):
;
- (
分):對於
,
;
- (
分):對於
,
;
- (
分):對於
,
,且每個值在
中出現的次數不超過
;
- (
分):
;
- (
分):
;
- (
翻譯由 DeepSeek V3 完成。
前後綴分解 + 對頂堆
性質一:三個相鄰子數組中位數相同,合併成一個子數組後。中位數不變。令這三個子數組長度分別位,令中位數位K。則合併後:除3個K外,有m+n+o個數
K,m+n+o個數
。故中位數仍然是K。
推論一:只需要考慮分成一個子數組,或兩個子數組。達到三個子數組,任選三個合併。
推論二:由於,故只需要考慮2個子數組。
實現
pre[n]記錄A長度為n的前綴的中位數,suff[n]記錄A長度位n的後綴的中位數。
求中位數,我至少想到3個方法:
一,利用對頂堆求中位數。
二,mulset記錄子數組,it指向中位數。如果兩個數,則it++,如果1個數
,不變。如果0個數
*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++**實現。