Fascinating Partitions (dp优化,单调栈)

题意

n个物品,划成k个连续段,每一段的贡献是这个区间最大的数,对k从1~n求最小和最大的贡献。

分析

题意很简单,首先看最大贡献,最大贡献就是划的时候,把大的数尽量不在一个段里就可以了。即从大往小加起来。

接下来看最小贡献,最小贡献很显然有一个n^3的dp转移,我们记dp[i][j]为前i个数分成j段的最小代价,于是有dp[i][j]=min_{1<=p<i}(dp[p][j-1]+max_{p+1<=k<=i}A_k),这里n是8000,n^3

的转移我们无法接受,于是考虑优化。

通过观察我们发现对于i来说, 可以分为两种情况,j作为当前最后一段的最大值,或者不是最大值。

i不是最大值,那我们考虑前面比a[i]大的离他最近的数a[k],我们发现dp[k][j]是一个可以被接受的状态,从k+1~i之间的所有数直接挂到k所在区间即可,更之前的数同样不需要考虑,因为我们总是满足i,k同区间。

接下来考虑i是最大值,即i,k不同区间的情况,这一段似乎只能枚举,但是考虑到不是最大值的时候我们可以使用单调栈来维护信息,那么这里我们只需要在单调栈里再加一维,记录单调栈内两个数之间的最小的dp[k][j]即可。

下面代码

//#include<algorithm>
//#include<bitset>
//#include<cassert>
//#include<cctype>
//#include<chrono>
//#include<cmath>
//#include<cstdio>
//#include<cstring>
//#include<functional>
//#include<iomanip>
//#include<iostream>
//#include<map>
//#include<queue>
//#include<random>
//#include<set>
//#include<sstream>
//#include<stack>
//#include<string>
//#include<unordered_map>
//#include<utility>
//#include<vector>
//#include<memory.h>
#include<bits/stdc++.h>
#define rep(i,a,b) for(auto i=(a);i<=(b);++i)
#define per(a,b) for(auto i=(a);i>=(b);--i)
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long,long long>
#define db double
#define IL inline
#define fir first
#define sec second
#define eps (1e-10)
#define mkp make_pair
//#define rep((a),(b)) for(int i=(a);i<=(b);++i)
#define lowbit(x) (x&(-x))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef  unsigned long long ull;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const int dx[8]={0,1,0,-1,1,1,-1,-1},dy[8]={1,0,-1,0,1,-1,1,-1};
const ll mod=998244353;//1e9+7;
const int N=1e4+10,M=4e4+10;

ll qpow(ll a,ll b)
{
	ll res=1;
	while(b)
	{
		if(b&1)res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res;
} 
ll gcd(ll a,ll b)
{
	if(b)return gcd(b,a%b);
	return a;
}


int n;
ll a[N];
ll dp[2][N];
ll ans[N][2];
void solve()
{
	int n;cin>>n;
	vector<int>b;
	for(int i=1;i<=n;++i)cin>>a[i],b.push_back(a[i]);
	sort(b.begin(),b.end(),greater<int>());
	for(int i=0;i<n;++i)
	{
		ans[i+1][1]=ans[i][1]+b[i];
	}
	memset(dp,0x3f,sizeof(dp));
	dp[0][0]=0;
	for(int j=1;j<=n;++j)
	{
		stack<pair<int,ll> >st;
		for(int i=j;i<=n;++i)
		{
			ll w=LINF,v=LINF;
			w=dp[(j-1)&1][i-1];
			while(!st.empty()&&a[st.top().first]<a[i])
			{
				w=min(w,st.top().second);
				st.pop();
			}
			if(st.empty())
			{
				if(i==1)w=0;
			}
			else 
			{
				v=dp[j&1][st.top().first];
			}
			st.push({i,w});
			dp[j&1][i]=min(w+a[i],v);
		}
		ans[j][0]=dp[j&1][n];
	}
	for(int i=1;i<=n;++i)cout<<ans[i][0]<<' '<<ans[i][1]<<'\n'; 
}
int main()
{
	IOS;
	int T=1;
//	init();
//	cin>>T;
//	scanf("%d",&T);
//	read(T);
	while(T--)solve();
	return 0;
}

/*\
100 200
7 15 8

*/

 

 

 

Recently, the renowned actor Zhang Songwen has sparked a fascinating phenomenon known as "two-way rejection", which has captured the attention of many and inspired the masses. The roots of this phenomenon are complex, with one of the fundamental causes being the fear of failure that plagues most of us. Rejection can instill a sense of inadequacy and a fear of being perceived as a failure, which can be challenging to overcome. However, the concept of "two-way rejection" teaches us that rejection is a natural part of life, and it's acceptable to reject and be rejected in return. This empowers us to recognize that life is not just about failures, but also about perseverance, and striving to achieve our aspirations, which may include fame and fortune. Despite the distractions we may encounter, the concept of "two-way rejection" reminds us to turn away from erroneous opportunities and remain steadfast in our principles and moral compass. While there are both advantages and drawbacks to this approach, "two-way rejection" ultimately inspires us to embrace rejection, learn from it, and emerge stronger and more self-assured. However, it is essential to distinguish between a sound and an unsound opportunity to avoid blindly rejecting the right ones. In conclusion, the concept of "two-way rejection" should be approached with discretion, but it can prove to be a valuable tool in enabling us to adhere to our goals and persevere through rejection. It teaches us to embrace rejection, learn from it, and move forward with confidence, ultimately empowering us to achieve our dreams and aspirations.结合双向拒绝进行内容补充
05-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值