Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend
2016-09-24 09:44
721 查看
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
12
Note
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标签,并不懂是什么意思…(其实是自己功力尚浅)
直接这么做似乎有点困难,我们可以用一个小技巧将这个要求转化一下:
A[i]<A[i+1]
A[i]−i<=A[i+1]−(i+1)
既然将小于变成了小于等于,那么就可以转化为dp问题做了,令dp[i][j]为前i个数,最后一个结尾的数大小为A[j](已减j)所需要的代价。复杂度O(n2)。
注意:因为最后的所有值如果在A数组中一定不会差于不在A数组中的情况,所以我们的第二维只需要开到n即可,而不是无上限地增加。
然而,CF上的各路大神有一种O(nlogn)的解法。Orz…
具体就是每次把当前这个值加入到堆中,从堆中取出最大值。如果最大值大于当前的数:将答案加上这个最大值与当前数的差值,将最大值弹出,而再次加入一个当前数。即相当于将最大值替换为当前数。
说起来有点玄学,本蒟蒻目前也无力证明,只能暂且先当是玄学好了。
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
12
Note
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标签,并不懂是什么意思…(其实是自己功力尚浅)
直接这么做似乎有点困难,我们可以用一个小技巧将这个要求转化一下:
A[i]<A[i+1]
A[i]−i<=A[i+1]−(i+1)
既然将小于变成了小于等于,那么就可以转化为dp问题做了,令dp[i][j]为前i个数,最后一个结尾的数大小为A[j](已减j)所需要的代价。复杂度O(n2)。
注意:因为最后的所有值如果在A数组中一定不会差于不在A数组中的情况,所以我们的第二维只需要开到n即可,而不是无上限地增加。
#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上的各路大神有一种O(nlogn)的解法。Orz…
具体就是每次把当前这个值加入到堆中,从堆中取出最大值。如果最大值大于当前的数:将答案加上这个最大值与当前数的差值,将最大值弹出,而再次加入一个当前数。即相当于将最大值替换为当前数。
说起来有点玄学,本蒟蒻目前也无力证明,只能暂且先当是玄学好了。
#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; }
相关文章推荐
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend(贪心+DP)
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心
- Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend(技巧 + 离散化dp)
- cf 372 div2 E. Sonya and Problem Wihtout a Legend dp
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
- Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend (DP/LIS变形)
- 动态规划,离散化(Sonya and Problem Wihtout a Legend,cf 713C)
- cf/Codeforces Round #371 714E - Sonya and Problem Wihtout a Legend - dp +lis
- hihocoder#1529 : 不上升序列&&Codeforces-713C:Sonya and Problem Wihtout a Legend(思维)
- cf 714 e Sonya and Problem Wihtout a Legend
- 【CF713C】Sonya and Problem Wihtout a Legend(离散化,DP)
- Codeforces Round #371 C. Sonya and Problem Wihtout a Legend (DP)
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)
- [堆与斜率] Codeforces 713C - Sonya and Problem Wihtout a Legend
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)
- Codeforces 713C C. Sonya and Problem Wihtout a Legend (经典DP)
- Codeforces-713C-Sonya and Problem Wihtout a Legend
- 【CodeForces】713 C. Sonya and Problem Wihtout a Legend
- 【DP】[CodeForces - 713C]Sonya and Problem Wihtout a Legend
- Codeforces Round 371 C Sonya and Problem Wihtout a Legend