【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();
	}
}
相关推荐
生态学者5 小时前
Global Change Biology | 森林越“步调一致”,反而越怕干旱?树木生长与叶片活动的同步之谜
大数据·人工智能·算法·r语言·微信公众平台
lsswear5 小时前
Agent 框架新手入门与实战部署指南
大数据·人工智能·算法
学逆向的5 小时前
C 循环结构的执行效率分析
c语言·开发语言·数据结构·网络安全
Data analyse4567 小时前
rnweb 回放数据量太大怎么办?分片存储与断点续传的实现思路
前端·网络·算法·数据分析
Navigator_Z8 小时前
LeetCode //C - 1293. Shortest Path in a Grid with Obstacles Elimination
c语言·算法·leetcode
Nil2089 小时前
leetcode 64最小路径和
算法·leetcode·职场和发展
光影少年9 小时前
AI Agent、自动化与未来工作
人工智能·算法·架构
牧羊人.3339 小时前
计算机视觉基础 第16章|EigenFace,FisherFace,LBPH传统人脸识别算法
人工智能·算法·计算机视觉
jeo陽9 小时前
04-循环实战:素数、最大公约数、级数求和与枚举法
c语言·开发语言·数据结构
Nil2089 小时前
leetcode 72编辑距离
算法·leetcode·职场和发展