CF 1400E Clear the Multiset (贪心+分治)

You have a multiset containing several integers. Initially, it contains a1a1 elements equal to 11, a2a2 elements equal to 22, ..., anan elements equal to nn.

You may apply two types of operations:

  • choose two integers ll and rr (l≤rl≤r), then remove one occurrence of ll, one occurrence of l+1l+1, ..., one occurrence of rr from the multiset. This operation can be applied only if each number from ll to rr occurs at least once in the multiset;
  • choose two integers ii and xx (x≥1x≥1), then remove xx occurrences of ii from the multiset. This operation can be applied only if the multiset contains at least xx occurrences of ii.

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

Input

The first line contains one integer nn (1≤n≤50001≤n≤5000).

The second line contains nn integers a1a1, a2a2, ..., anan (0≤ai≤1090≤ai≤109).

Output

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

Examples

input

Copy

4
1 4 1 1

output

Copy

2

input

Copy

5
1 0 1 0 1

output

Copy

3

题解:操作1是将一个连续不含0的区间减1,操作2是将任意一个数清零。容易想到,对于操作1,如果不能把区间中的某个数减为0,相当于这些操作都白给。因此,对于某个区间,我们将整个区间减去它的最小值,再对不含0的子区间做相同的操作,统计的时候和区间长度比较取最小即可。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string.h>
#include<string>
using namespace std;
const int INF=1000000001,maxn=5002;
int a[maxn];

int get(int l,int r)
{
	if (l>r) return 0;
	if (l==r)
	{
		
		return min(a[l],1); 
	}
	int x=INF,sum=0;
	for (int i=l;i<=r;i++)
		if (x>a[i]) x=a[i];
	for (int i=l;i<=r;i++)
		a[i]-=x;
	int pl=l-1;
	for (int i=l;i<=r;i++)
	{
		if (a[i]==0) 
		{
			sum+=get(pl+1,i-1);
			pl=i;
		}
	}	
	if (pl!=r) sum+=get(pl+1,r);
	return min(sum+x,r-l+1);
	return min(get(l,x-1)+get(x+1,r),r-l+1);
}

int main()
{
	int n;
	cin>>n;
	for (int i=1;i<=n;i++)
		cin>>a[i];
	int l=0,ans=INF;
	ans=min(get(1,n),n);
	cout<<ans;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值