不严格递增

Sonya was unable to think of a story for this problem, so here comes the formal description.

You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0。
这个题的难点就是找到严格递增的数列。就是下面所说的不严格递增的思想。
假设dp[i][j] 表示序列前 i 个数递增且第 i 个数的不大于原数列第 j 大的数的最少操作数。
状态转移方程为:dp[i][j]=min(dp[i][j−1], dp[i−1][j]+abs(a[i], b[j]))dp[i][j]=min(dp[i][j−1], dp[i−1][j]+abs(a[i], b[j])) , a[i] 表示原数列中第 i 个数,b[j] 表示原数列中第 j 大的数要对原数列去重后。
对于不严格递增的情况
不等式 ai<ai+1⟺ai≤ai+1−1⟺ai−i≤ai+1−(i+1)ai<ai+1⟺ai≤ai+1−1⟺ai−i≤ai+1−(i+1) 成立,故对于每个读入的 a[i] ,ai=ai−iai=ai−i ,即可使用上述不严格递增的方式求解。
#include
#include
#include
using namespace std;
const int N=3005;
int n,a[N],b[N];
long long dp[N][N];
int main()
{
memset(dp,0x3f,sizeof(dp));
while(cin>>n)
{
for(int i=1;i<=n;i++)
{
cin>>a[i];
a[i]-=i;
b[i]=a[i];
}
sort(b+1,b+n+1);
for(int j=1;j<=n;j++)
dp[0][j]=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
dp[i][j]=min(dp[i][j-1], dp[i-1][j]+abs(a[i]-b[j]));
cout<<dp[n][n]<<endl;
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值