D. Jellyfish and Mex Codeforces Round 901 (Div. 2)

Problem - D - Codeforces

题目大意:有一个n个数的数组a,数m初始为0,每次操作可以删除任意一个数,然后m加上那个数,求n次操作和m的最小值

1<=n<=5000;0<=a[i]<=1e9

思路:可以发现,如果我们要删除某个数,那么一定要把所有和这个数相等的数全部删去,这样才能使MEX变小,同时,所有大于MEX的数删去的花费都是0,所以我们每次操作的数的大小都是递减的,且只会操作MEX到0。

那么我们令dp[i]等于MEX等于i时的最小花费,我们从MEX到0枚举i,同时枚举该删哪个数,也就是从0到i-1遍历,当前最小花费就是不删这个数dp[j],或者删这个数也就是dp[i]+当前MEX*(这个数数量-1)再加这个数,转移方程为dp[j]=min(dp[j],dp[i]+i*(cnt[j]-1)+j)

cpp 复制代码
//#include<__msvc_all_public_headers.hpp>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e3 + 5;
ll n;
ll a[N];
ll cost[N];
ll cnt[N];
void init()
{
	for (int i = 0; i <= n; i++)
	{
		cost[i] = 1e18;
		cnt[i] = 0;
	}
}
ll gcd(ll a, ll b)
{
	return b ? gcd(b, a % b) : a;
}
ll lowbit(ll x)
{
	return x & (-x);
}
void solve()
{
	ll m;
	cin >> n;
	init();
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		if (a[i] <= n)
		{//MEX最大为n,大于n的都可以随便删
			cnt[a[i]]++;
		}
	}
	int mex = 0;
	while (cnt[mex])
	{//找当前的MEX
		mex++;
	}
	cost[mex] = 0;
	for (ll i = mex; i >= 0; i--)
	{
		for (ll j = 0; j < i; j++)
		{
			cost[j] = min(cost[j], cost[i] + i * (cnt[j] - 1) + j);
		}
	}
	cout << cost[0] << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}
相关推荐
BHXDML1 天前
第七章:类与对象(c++)
开发语言·c++
玄冥剑尊1 天前
贪心算法进阶
算法·贪心算法
玄冥剑尊1 天前
贪心算法深化 I
算法·贪心算法
52Hz1181 天前
力扣73.矩阵置零、54.螺旋矩阵、48.旋转图像
python·算法·leetcode·矩阵
BHXDML1 天前
第一章:线性回归& 逻辑回归
算法·逻辑回归·线性回归
yyf198905251 天前
C++ 跨平台开发的挑战与应对策略
c++
iAkuya1 天前
(leetcode)力扣100 二叉搜索树种第K小的元素(中序遍历||记录子树的节点数)
算法·leetcode·职场和发展
又见野草1 天前
C++类和对象(中)
开发语言·c++
Remember_9931 天前
【LeetCode精选算法】滑动窗口专题二
java·开发语言·数据结构·算法·leetcode
Gorgous—l1 天前
数据结构算法学习:LeetCode热题100-动态规划篇(下)(单词拆分、最长递增子序列、乘积最大子数组、分割等和子集、最长有效括号)
数据结构·学习·算法