【树状树状 滑动窗口】P11012 「ALFR Round 4」B 颜料|普及+

本文涉及知识点

【C++】树状数组的使用、原理、封装类、样例
C++算法:滑动窗口及双指针总结

P11012 「ALFR Round 4」B 颜料

题目背景

在小山的观念里,画展因色彩不同而绚丽。

题目描述

小山一共有 n n n 副画作,每副画作都有其主要的颜料。具体的,第 i i i 副画作的主要颜料的种类为 a i a_i ai。小山可以选择一段编号连续的画作组成一个画展,而画展的绚丽程度为(设该画展由第 l l l 到第 r r r 副画组成): ∑ i = 1 W ∑ j = i + 1 W min ⁡ ( c i , c j ) \sum_{i=1}^W\sum_{j=i+1}^W\min(c_i,c_j) i=1Wj=i+1Wmin(ci,cj),其中 c i c_i ci 表示种类为 i i i 的颜料在画展中出现的次数, W W W 为所有颜料种类的值域。

现在小山想知道,若要画展的绚丽程度至少为 k k k,应至少选出多少副连续的画作?若无绚丽程度至少为 k k k 的画展,则答案为 − 1 -1 1

输入格式

共两行,第一行两个整数 n , k n,k n,k,含义见题目描述

第二行 n n n 个整数,第 i i i 个数为 a i a_i ai,表示第 i i i 副画的主要颜料的种类。

输出格式

一行一个整数表示答案。

输入输出样例 #1

输入 #1

10 6
2 3 4 3 3 4 2 4 9 2

输出 #1

5

说明/提示

样例解释

选择第 5 5 5 至第 9 9 9 副画作组成画展,则 c 1 = 0 , c 2 = 1 , c 3 = 1 , c 4 = 2 , c 5 = 0 , c 6 = 0 , c 7 = 0 , c 8 = 0 , c 9 = 1 , ∑ i = 1 9 ∑ j = i + 1 9 min ⁡ ( c i , c j ) = 6 c_1=0,c_2=1,c_3=1,c_4=2,c_5=0,c_6=0,c_7=0,c_8=0,c_9=1,\sum_{i=1}^9\sum_{j=i+1}^9\min(c_i,c_j)=6 c1=0,c2=1,c3=1,c4=2,c5=0,c6=0,c7=0,c8=0,c9=1,i=19j=i+19min(ci,cj)=6。容易得知 5 5 5 是符合要求的区间的最短长度。

数据范围

子任务分值限制
0 0 0 10 10 10所有的 a i ( 1 ≤ i ≤ n ) a_i(1\le i\le n) ai(1in) 都相同
1 1 1 20 20 20 n , a i ≤ 1 0 2 n,a_i\le10^2 n,ai102
2 2 2 70 70 70-

对于 100 % 100\% 100% 的数据, 1 ≤ n , a i ≤ 2 × 1 0 6 1\le n,a_i\le2\times10^6 1n,ai2×106 1 ≤ k ≤ 1 0 15 1\le k\le 10^{15} 1k1015

树状数组+滑动窗口

f(left,r)=第left到r幅图组成的画展绚丽程度。
性质一:f(left,r+1) ≥ \ge f(left,r)。故如果f(left,r1) ≥ \ge K,则无需判断f(left,r2),r2 > r1。
性质二:f(left,r-1) ≤ \le f(left,r) < K。
结论: 滑动窗口,从0到大枚举left。对任意left,求最小符合下述条件的r。
一,r == N。二, f(left,r) ≥ K \ge K K
根据性质二,left++时,r无需复位。故时间复杂度:O(N)。

f(left,r)迭代到f(left,r+1)

mColorCnt 记录各颜色的数量。
bit1[i] 记录 颜色数量为i的颜色的数量。如:某颜色的数量为i,则bit1[i]++。
bit2[i] = b i t 1 [ i ] × i bit1[i] \times i bit1[i]×i
x= mColorCnt[ a i a_i ai]。
则 颜色 a i a_i ai 对f(left,r)的贡献为:g(x)=bit2.Sum(x-1)+ (bit1.sum(x ⋯ \cdots )-1) × \times × x
f(left,r)迭代成f(left,r+1)
iSub = g(x)
修改bit1,bit2
iAdd = g(x+1)
cur += iAdd - iSub

f(left,r)迭代到f(left+1,r)

类似 cur -= iSub
修改bit1,bit2
cur += g(x-1)

代码

核心代码

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

template<class ELE = int >
class ITreeArrSumOpe
{
public:
	virtual void Assign(ELE& dest, const ELE& src) = 0;
	virtual ELE Back(const ELE& n1, const ELE& n2) = 0;
};

template<class ELE = int >
class CTreeArrAddOpe :public ITreeArrSumOpe<ELE>
{
public:
	virtual void Assign(ELE& dest, const ELE& src) {
		dest += src;
	}
	virtual ELE Back(const ELE& n1, const ELE& n2) {
		return n1 - n2;
	}
};

template<class ELE = int, class ELEOpe = CTreeArrAddOpe<ELE> >
class CTreeArr
{
public:
	CTreeArr(int iSize) :m_vData(iSize + 1)
	{

	}
	void Add(int index, ELE value)
	{
		if ((index < 0) || (index >= m_vData.size() - 1)) { return; }
		index++;
		while (index < m_vData.size())
		{
			m_ope.Assign(m_vData[index], value);
			index += index & (-index);
		}
	}
	ELE Sum(int index)//[0...index]之和
	{
		index++;
		ELE ret = 0;
		while (index)
		{
			m_ope.Assign(ret, m_vData[index]);
			index -= index & (-index);
		}
		return ret;
	}
	ELE Sum() { return Sum(m_vData.size() - 2); }
	ELE Get(int index)
	{
		return m_ope.Back(Sum(index), Sum(index - 1));
	}
private:
	ELEOpe m_ope;
	vector<ELE> m_vData;
};

class Solution {
		public:
			int Ans(const long long K, vector<int>& A) {
				const int N = A.size();
				const int M = *max_element(A.begin(), A.end());
				vector<int> vColorCnt(M + 1);
				CTreeArr<long long> bit1(N + 1), bit2(N + 1);
				auto G = [&](int x) {
					const long long iMoreEqual = bit1.Sum() - bit1.Sum(x - 1)-1;
					return bit2.Sum(x - 1) + x * iMoreEqual;
				};
				int ans = INT_MAX / 2;
				long long cur = 0;
				for (int left = 0, r = 0;left < N;left++) {
					while ((cur<K)&&(r<N)) {
						int& x = vColorCnt[A[r]]; r++;
						const long long iSub = G(x);
						bit1.Add(x, -1);
						bit2.Add(x, -x);
						x++;
						bit1.Add(x , 1);						
						bit2.Add(x , x );
						cur += G(x) - iSub;
					}
					if (cur >= K) {
						ans = min(ans, r - left);
					}
					{//left迭代成left+1
						int& x = vColorCnt[A[left]];
						const long long iSub = G(x);
						bit1.Add(x, -1);
						bit2.Add(x, -x);
						x--;
						bit1.Add(x, 1);
						bit2.Add(x, x);
						cur += G(x) - iSub;
					}
				}
				return ans > N ? -1:ans;
			}
		};

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

单元测试

	long long K;
		vector<int> A;
		TEST_METHOD(TestMethod11)
		{
			K = 6, A = { 2,3,4,3,3,4,2,4,9,2 };
			auto res = Solution().Ans(K,A);
			AssertEx(5, res);
		}
		TEST_METHOD(TestMethod12)
		{
			K = 99'999*50'000LL, A.assign(1e6, 1);
			for (int i = 0;i < A.size();i++) {
				A[i] = i + 1;
			}
			auto res = Solution().Ans(K, A);
			AssertEx(100'000, res);
		}

扩展阅读

我想对大家说的话
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
员工说:技术至上,老板不信;投资人的代表说:技术至上,老板会信。
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛
失败+反思=成功 成功+反思=成功

视频课程

先学简单的课程,请移步优快云学院,听白银讲师(也就是鄙人)的讲解。
https://edu.youkuaiyun.com/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.youkuaiyun.com/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件架构师何志丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值