【cf】EPIC Institute of Technology Round Summer 2024(Div. 1 + Div. 2)题解 C - D

C. Basil's Garden(dp)

发现最后一个变为 0 的一定是第一个,对于每一个位置而言,如果它后面的位置没变为 0 ,那么它就不可能变为 0,所以它变为 0 的时刻最小应该是在它后一个位置变为 0 的时刻加 1,如果这个位置太高的话,就需要 a[i] 个时间变为 0,所以当前位置变为 0 的时刻就是二者取最大值

设计 dp[i] 表示第 i 个位置变为 0 的时刻,有转移方程:dp[i] = max(dp[i + 1] + 1, a[i])

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

#define int long long
using i64 = long long;

typedef pair<int, int> PII;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;
typedef pair<int, pair<int, bool>> PIIB;

const int N = 1e5 + 10;
const int maxn = 1e6 + 10;
const int mod = 998244353;
const int mod1 = 954169327;
const int mod2 = 906097321;
const int INF = 0x3f3f3f3f3f3f3f3f;

void solve()
{
	int n;
	cin >> n;
	vector<int> a(n + 1);
	for (int i = 1; i <= n; i ++ ) cin >> a[i];
	reverse(a.begin() + 1, a.end());
	vector<int> ans(n + 1);
	ans[1] = a[1];
	for (int i = 2; i <= n; i ++ ) ans[i] = max(ans[i - 1] + 1, a[i]);
	cout << ans[n] << '\n';
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t = 1;
	cin >> t;
	while (t--)
	{
		solve();
	}
}

D. World is Mine(dp)

关于博弈问题的一般思路:

首先思考二者分别的最优策略:

Alice 一定是能取小的就取小的

Bob 需要取一定的数使得 Alice 没法取这个数

设计 dp[i][j] 表示 Bob 在前 i 种数里选完了 j 种(注意是种不是个)的时候需要进行的操作次数

如果不取第 i + 1 种的话, dp[i + 1][j + 1] 等于 dp[i][j + 1]

如果要取第 i + 1 种,首先需要满足一个条件,即 dp[i][j] + 第i+1种数字的个数 <= i - j,因为要保证 Alice 的操作次数比 Bob 多,在满足该条件的基础下,dp[i + 1][j + 1] 等于 min(dp[i + 1][j + 1], dp[i][j] + 第i+1种数字的个数)

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

#define int long long
using i64 = long long;

typedef pair<int, int> PII;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;
typedef pair<int, pair<int, bool>> PIIB;

const int N = 1e5 + 10;
const int maxn = 1e6 + 10;
const int mod = 998244353;
const int mod1 = 954169327;
const int mod2 = 906097321;
const int INF = 0x3f3f3f3f3f3f3f3f;

void solve()
{
	int n;
	cin >> n;
	vector<int> cnt(n + 1);
	for (int i = 0; i < n; i ++ )
	{
		int x; cin >> x;
		cnt[x] ++ ;
	}
	vector<int> v;
	for (int i = 1; i <= n; i ++ )
	{
		if (cnt[i] > 0) v.push_back(cnt[i]);
	}
	int m = v.size();
	vector<vector<int>> dp(m + 1, vector<int>(m + 1, INF));
	for (int i = 0; i <= m; i ++ ) dp[i][0] = 0;
	for (int i = 0; i < m; i ++ )
	{
		for (int j = 0; j < i; j ++ )
		{
			dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j + 1]);
			if (dp[i][j] + v[i] <= i - j)
			{
				dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + v[i]);
			}
		}
	}
	int ans = INF;
	for (int i = 0; i <= m; i ++ )
	{
		if (dp[m][i] != INF) ans = min(ans, m - i);
	}
	cout << ans << '\n';
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t = 1;
	cin >> t;
	while (t--)
	{
		solve();
	}
}
相关推荐
lingggggaaaa2 分钟前
免杀对抗——C2远控篇&C&C++&DLL注入&过内存核晶&镂空新增&白加黑链&签名程序劫持
c语言·c++·学习·安全·网络安全·免杀对抗
gfdhy32 分钟前
【c++】哈希算法深度解析:实现、核心作用与工业级应用
c语言·开发语言·c++·算法·密码学·哈希算法·哈希
百***06011 小时前
SpringMVC 请求参数接收
前端·javascript·算法
我不会插花弄玉1 小时前
vs2022调试基础篇【由浅入深-C语言】
c语言
一个不知名程序员www2 小时前
算法学习入门---vector(C++)
c++·算法
云飞云共享云桌面2 小时前
无需配置传统电脑——智能装备工厂10个SolidWorks共享一台工作站
运维·服务器·前端·网络·算法·电脑
福尔摩斯张2 小时前
《C 语言指针从入门到精通:全面笔记 + 实战习题深度解析》(超详细)
linux·运维·服务器·c语言·开发语言·c++·算法
fashion 道格2 小时前
数据结构实战:深入理解队列的链式结构与实现
c语言·数据结构
橘颂TA2 小时前
【剑斩OFFER】算法的暴力美学——两整数之和
算法·leetcode·职场和发展
xxxxxxllllllshi3 小时前
【LeetCode Hot100----14-贪心算法(01-05),包含多种方法,详细思路与代码,让你一篇文章看懂所有!】
java·数据结构·算法·leetcode·贪心算法