1. 程式人生 > 實用技巧 >codefoces 1400E Clear the Multiset

codefoces 1400E Clear the Multiset

https://codeforces.com/contest/1400/problem/E

可以橫著把有連續的數字-1,或者把某一個數字清0,問你最少操作幾次,分治

555 555 555操作3次,1 1 1操作1次

具體看程式碼把

#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 5050+1;
ll list[maxn];
int n;

int cal(int l,int r){
	//cout<<l<<" "<<r<<endl;
	if(l <=-1 || r <= -1) return 0;
	if(l >= r){
		return min(1LL*r - 1LL*l + 1,list[l]);
	}
	ll cnt = 1e15;
	for(int i = l;i<=r;i++){
		cnt = min(list[i],cnt);
	}
	int p = -1;
	for(int i = l;i<=r;i++){
		list[i] -= cnt;
		if(list[i] == 0 && p == -1) p = i; 
	}
	
	return min(cnt +  cal(l,p-1) + cal(p+1,r),1LL*r - 1LL*l + 1);
}


int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>list[i];
	}
	ll ans = cal(1,n);
	
//	cout<<endl;
	cout<<ans<<endl;
	return 0;
}