CodeForces_1400E Clear the Multiset(分治)

本文探讨了CleartheMultiset算法问题,旨在通过两种操作清除包含特定数量整数的多重集。介绍了算法背景,详细解释了解决方案思路,包括如何通过操作减少元素,以及递归处理剩余元素的方法。

Clear the Multiset

time limit per test:2 seconds
memory limit per test:256 megabytes
Problem Description

You have a multiset containing several integers. Initially, it contains a1a_1a1 elements equal to 1, a2a_2a2 elements equal to 2, …, ana_nan elements equal to n.

You may apply two types of operations:

  • choose two integers lll and rrr (l≤rl≤rlr), then remove one occurrence of lll, one occurrence of l+1l+1l+1, …, one occurrence of rrr from the multiset. This operation can be applied only if each number from lll to rrr occurs at least once in the multiset;
  • choose two integers iii and xxx (x≥1x≥1x1), then remove xxx occurrences of iii from the multiset. This operation can be applied only if the multiset contains at least xxx occurrences of iii.

What is the minimum number of operations required to delete all elements from the multiset?

Input

The first line contains one integer nnn (1≤n≤50001≤n≤50001n5000).

The second line contains n integers a1a_1a1, a2a_2a2, …, ana_nan (0≤ai≤1090≤a_i≤10^90ai109).

Output

Print one integer — the minimum number of operations required to delete all elements from the multiset.

Sample Input

4
1 4 1 1

Sample Output

2

题意

有一个多重集,其中值为iii的数字有aia_iai(1≤i≤n)(1\le i \le n)(1in)。可以进行两种操作:

  • 选择两个数lllr(l≤r)r(l\le r)r(lr),从集合删除值为lllrrr的各删除一个。需保证lllrrr中每种值至少有一个。
  • 选择两个数i,xi,xi,x,从集合中删除xxx个值为iii的数。需保证当前集合中值为iii的数至少有xxx个。

求最少的操作次数使集合为空。

题解

只考虑操作2,所以最多需要n次操作。
mi=min(a1,a2,...,an)mi = min(a_1, a_2, ..., a_n)mi=min(a1,a2,...,an).若要执行操作1,则一定是执行mi次l=1,r=nl=1,r=nl=1,r=n(若少于mi次,则实际上还是需要n次操作2,若多于mi次,则不符合条件)。
执行mi次操作后,剩下的ai>mia_i > miai>mi的则被分成若干段连续区间。对于每一段区间,递归处理,考虑其只需要使用操作2,或需要若干次操作1的最小操作数使集合为空。

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define eps 1e-8
   
using namespace std;
typedef long long LL;  
typedef pair<int, int> P;
const int maxn = 5020;
const int mod = 1000000007;
int a[maxn];
int dfs(int l, int r, int up);

int main()
{
    int n, m, i, j, k;
    scanf("%d", &n);
    for(i=1;i<=n;i++)
        scanf("%d", &a[i]);
    printf("%d", dfs(1, n, 0));
    return 0;
}

int dfs(int l, int r, int up)
{
    int mi = INF, i, j, sum;
    for(i=l;i<=r;i++)
        if(a[i] < mi)mi = a[i];
    sum = mi-up, i = l;
    while(i<=r){
        if(a[i]>mi){
            j=i;
            while(a[j]>mi && j<=r)
                j++;
            sum += dfs(i, j-1, mi);
            i = j;
        }
        else i++;
    }
    return min(sum, r-l+1);
}
内容概要:本文档详细介绍了一款基于C语言的IGBT变频电源设计与实现的项目实例。该项目旨在结合单片机控制系统,利用高效稳定的单片机特性,精确控制IGBT的开关操作,实现对电源频率和电压的调节。文档涵盖了项目背景、目标、挑战及解决方案、特点与创新、应用领域、软件模型架构、硬件电路设计、功能模块及代码实现、调试与优化、GUI界面设计等方面。项目通过精确控制频率和电压,增强了抗干扰能力,提高了能源利用效率,增强了系统稳定性和可靠性,降低了生产成本,提供了灵活的应用方案,提升了系统的可扩展性,并推动了智能化制造发展。适合人群:具备一定单片机开发基础,对电力电子、嵌入式系统有兴趣的研发人员,尤其是从事工业自动化控制领域的工程师。使用场景及目标:①了解IGBT变频电源的核心技术,包括高精度频率与电压控制、高效的IGBT驱动电路设计;②掌握智能化控制与远程监控的设计思路;③学习如何增强系统的抗干扰能力,确保在复杂环境下稳定工作;④掌握模块化设计与可扩展性的实现方法;⑤理解节能与环保的设计理念,提高能源利用效率;⑥熟悉完善的故障检测与保护机制的设计。其他说明:本项目不仅为IGBT变频电源的设计提供了高效的解决方案,还为电力自动化和工业应用中的电源管理系统提供了实用的参考。通过后期的不断优化和技术升级,系统的性能和功能将得到进一步提升。文档中提供的完整代码和详细的硬件电路图设计,有助于读者更好地理解和实践该项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值