admin 管理员组

文章数量: 1184232

本文涉及知识点

C++动态规划
C++贪心

P9468 [EGOI 2023] Candy / 糖果

题目背景

Day 2 Problem B.

题面译自 EGOI2023 candy。

题目描述

在伊卡古城,据说有一座宫殿,其财富超乎想象。在其中,一个走廊中有 N N N 盒来自世界各地的糖果。路过的旅行者只要用黄金支付糖果的重量,就可以拿走任意多的糖果。

装糖果的盒子被从左到右编号为 0 0 0 N − 1 N-1 N1。在盒子 i i i 中,剩余有 a i a_i ai 单位的糖果,其中 a i a_i ai 是一个非负整数。

作为宫殿的守护者,你需要移动这些盒子,使得有很多糖果的盒子更靠近入口。

你已知数组 a 0 , a 1 , ⋯   , a N − 1 a_0,a_1,\cdots,a_{N-1} a0,a1,,aN1,以及整数 F F F T T T。在一次操作中,你被允许交换 a 0 , a 1 , ⋯   , a N − 1 a_0,a_1,\cdots,a_{N-1} a0,a1,,aN1 中的任意两个相邻元素。要使得数组前 F F F 个元素的和至少为 T T T,至少需要多少次操作?

输入格式

第一行三个整数 N , F , T N,F,T N,F,T

第二行 N N N 个整数 a 0 , a 1 , ⋯   , a N − 1 a_0,a_1,\cdots,a_{N-1} a0,a1,,aN1

输出格式

如果不可能达成目标,输出 NO

否则,输出一个整数,表示最少操作数。

输入输出样例 #1

输入 #1

6 2 27
10 4 20 6 3 3

输出 #1

1

输入输出样例 #2

输入 #2

6 5 5000000000
1000000000 1000000000 0 1000000000 1000000000 1000000000

输出 #2

3

输入输出样例 #3

输入 #3

3 2 100
20 30 60

输出 #3

NO

输入输出样例 #4

输入 #4

1 1 100
100

输出 #4

0

说明/提示

样例 1 1 1 解释

在样例 1 1 1 中,前两个元素的和应当至少为 27 27 27。一次相邻元素的交换就可以达成目标:交换 4 4 4 20 20 20。在这次交换后,数组变成 10 20 4 6 3 3,前两个元素的和为 10 + 20 = 30 ≥ 27 10+20=30\ge 27 10+20=3027


样例 2 2 2 解释

在样例 2 2 2 中,这个 0 0 0 必须一路移动到数组末尾;这需要 3 3 3 次交换。


样例 3 3 3 解释

在样例 3 3 3 中,不可能使得前两个元素和至少为 100 100 100;我们能做到的最好结果是 60 + 30 = 90 60+30=90 60+30=90


数据范围

对于全部数据, 1 ≤ N ≤ 100 1\le N\le 100 1N100 1 ≤ F ≤ N 1\le F\le N 1FN 0 ≤ T ≤ 1 0 11 0\le T\le 10^{11} 0T1011 0 ≤ a i ≤ 1 0 9 0\le a_i\le 10^9 0ai109

  • 子任务一( 6 6 6 分): N ≤ 2 N\le 2 N2 a i ≤ 100 a_i\le 100 ai100 T ≤ 1 0 9 T\le 10^9 T109
  • 子任务二( 19 19 19 分): a i ≤ 1 a_i\le 1 ai1
  • 子任务三( 16 16 16 分): N ≤ 20 N\le 20 N20
  • 子任务四( 30 30 30 分): a i ≤ 100 a_i\le 100 ai100
  • 子任务五( 29 29 29 分):无特殊限制。

提示

答案可能不在 32 32 32 位整型范围内,如果你使用 C++ 语言,请注意溢出的可能。

[EGOI 2023] Candy / 糖果 动态规划 贪心之临项交换

本题的难度在于描述动态规划的状态。
性质一:选择的F个数,相对顺序不变。处理完的数据是b,如果b[i]的原始下标i1,b[i+1]的原始下标i2,i1 > i2,且i最小:
则 i <=i1,i+1 <=i2。保持相对顺序是:i1-i+i2-(i+1)=i1+i2-2i-1 ,逆序是:i2-i + |i1-(i+1)|,
//如果i1 >=i+1不变。如果i1 < i+1,此时i1==i。逆序是:i2-i+1 ,相对顺序是:i2-i-1,逆序多2。故保持相对顺序,不劣于逆序。

动态规划的状态

dp[k][i][j]记录选择的数的最大值(long long),k表示a长度为k的前缀已经处理;i表示已经选择了i个数据,即a[0…i-1];j表示移动次数。
k ∈ [ 0 , N ] , i ∈ [ 0 , F ] , j ∈ [ 0 , F ( N − F ) ] k\in[0,N],i\in[0,F],j\in[0,F(N-F)] k[0,N],i[0,F],j[0,F(NF)]。极端情况下,选择的是最后F个数,即将下标[N-F \sim N-1]移到[0…F-1],即F(N-F) 空间复杂度:O(nnnn)。可以用滚动向量优化空间。
F(N-F)就是边长固定,求最大面积。边长分别为:N/2和N-N/2。

动态规划的填表顺序

枚举前置状态,k从0到N-1,i,j任意。i > j无意义,故无需枚举。

动态规划的转移方程

如果a[k]选择则MaxSelf(dp[k+1][i+1][j+(k-i)],dp[k][i][j]+a[k]),如果不选择MaxSelf(dp[k+1][i][j],dp[k][i][j])。时间复杂度:等于空间复杂度。

动态规划的初始值

dp[0][0][0]=0,其它 LLONG_MIN/2。

动态规划的返回值

dp[k][i][j] >= T的最小j。

代码

核心代码

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

class Solution {
public:
	int Ans(const int N, const int F, const long long T, vector<int>& a) {
		const int MJ = (N - F) * F;
		vector<vector<long long>> pre(F + 1, vector<long long>(MJ + 1, LLONG_MIN / 2));
		pre[0][0] = 0;
		for (int i = 0; i < N; i++) {
			auto cur = pre;
			for (int sel = 0; sel <= min(i, F - 1); sel++) {
				for (int j = 0; j <= MJ; j++) {
					const int j1 = j + (i - sel);
					if (j1 > MJ) { break; }
					cur[sel + 1][j1] = max(cur[sel + 1][j1], pre[sel][j] + a[i]);
				}
			}
			pre.swap(cur);
		}
		for (int j = 0; j <= MJ; j++) {
			if (pre.back()[j] >= T) { return j; }
		}
		return -1;
	}
};

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;
	int N, F;
	long long T;
	cin >> N >>F  >> T;
	auto a = Read<int>(N);
#ifdef _DEBUG		
		printf("N=%d,F=%d,T=%lld", N,F,T);
		Out(a,",a=");
		//Out(ab, ",ab=");
		//Out(B, "B=");
		//Out(que, "que=");
		//Out(B, "B=");
#endif // DEBUG		
		auto res = Solution().Ans(N,F,T,a);
		if (-1 == res) {
			cout << "NO";
		}
		else {
			cout << res << "\n";
		}

		
	return 0;
};

单元测试

int N, F;
		long long T;
		vector<int> a;
		TEST_METHOD(TestMethod11)
		{
			N = 6, F = 2, T = 27, a = { 10,4,20,6,3,3 };
			auto res = Solution().Ans(N, F, T, a);
			AssertEx(1, res);
		}
		TEST_METHOD(TestMethod12)
		{
			N = 6, F = 5, T = 5000000000, a = { 1000000000,1000000000,0,1000000000,1000000000,1000000000
			};
			auto res = Solution().Ans(N, F, T, a);
			AssertEx(3, res);
		}
		TEST_METHOD(TestMethod13)
		{
			N = 3, F = 2, T = 100, a = { 20,30,60 };
			auto res = Solution().Ans(N, F, T, a);
			AssertEx(-1, res);
		}
		TEST_METHOD(TestMethod14)
		{
			N = 1, F = 1, T = 100, a = { 100 };
			auto res = Solution().Ans(N, F, T, a);
			AssertEx(0, res);
		}

扩展阅读

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

视频课程

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

测试环境

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

本文标签: 贪心 糖果 动态 Candy EGOI