Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend
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.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array.
Next line contains n integer ai (1 ≤ ai ≤ 109) .
Output
Print the minimum number of operation required to make the array strictly increasing.
Examples
Input
7
2 1 5 11 5 9 11
Output
9
Input
5
5 4 3 2 1
Output
12Note
In the first sample, the array is going to look as follows:
2 3 5 6 7 9 11
|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9
And for the second sample:
1 2 3 4 5
|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12
Task:
给定一个序列的 n 个数,每次可以对其中的一个数加一或减一。求最终使得整个序列单调递增的最小操作数。
Solution:
CF上这题挂了一个flow标签,并不懂是什么意思…(其实是自己功力尚浅)
直接这么做似乎有点困难,我们可以用一个小技巧将这个要求转化一下:
既然将小于变成了小于等于,那么就可以转化为dp问题做了,令 dp[i][j] 为前i个数,最后一个结尾的数大小为 A[j] (已减 j )所需要的代价。复杂度
注意:因为最后的所有值如果在
A
数组中一定不会差于不在
#include<stdio.h>
#include<iostream>
#include<algorithm>
#define ll long long
#define INF (1LL<<62)
#define M 3005
using namespace std;
int A[M],B[M];
ll dp[2][M];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&A[i]);
A[i]-=i;
B[i-1]=A[i];
}
sort(B,B+n);
int m=unique(B,B+n)-B;
for(int i=0;i<m;i++)
dp[1][i]=abs(A[1]-B[i]);
for(int i=2;i<=n;i++){
ll mi=INF;
int cur=i&1;
for(int j=0;j<m;j++){
if(dp[!cur][j]<mi)mi=dp[!cur][j];
dp[cur][j]=mi+abs(A[i]-B[j]);
}
}
ll mi=INF;
for(int i=0;i<m;i++)
if(dp[n&1][i]<mi)mi=dp[n&1][i];
cout<<mi<<endl;
return 0;
}
然而,CF上的各路大神有一种
具体就是每次把当前这个值加入到堆中,从堆中取出最大值。如果最大值大于当前的数:将答案加上这个最大值与当前数的差值,将最大值弹出,而再次加入一个当前数。即相当于将最大值替换为当前数。
说起来有点玄学,本蒟蒻目前也无力证明,只能暂且先当是玄学好了。
#include<stdio.h>
#include<iostream>
#include<queue>
#define ll long long
#define M 3005
using namespace std;
priority_queue<int>Q;
int main(){
int n;ll ans=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
x-=i;
Q.push(x);
int mx=Q.top();
if(mx>x){
ans+=mx-x;
Q.pop();
Q.push(x);
}
}
cout<<ans<<endl;
return 0;
}