Codeforces 713 C Sonya and Problem Wihtout a Legend
2016-09-17 19:07
459 查看
Description
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 \le n \le 3000)\) — the length of the array.
Next line contains \(n\) integer \(a_{i}(1 \le a_{i} \le 10^{9})\).
Output
Print the minimum number of operation required to make the array strictly increasing.
Sample Input
7
2 1 5 11 5 9 11
Sample Output
9
与BZOJ1049 数字序列类似,所有我想到了\(O(N^{3})\)做法,果断TLE。标解懂了些,做法太神了,什么维护中位数,但就是不知道转移怎么会没有后效性。
此处介绍另一种做法。首先也是将单调上升变为单调不降(见BZOJ1049 数字序列)。\(f_{i,j}\)表示前\(i\)个数,最大为\(j\)的合法序列最小代价。转移方程
\[f_{i,j} = min\{f_{i-1,k} \}+\mid A_{i}-j \mid(k \le j)\]
当然\(A\)值域太大,我们可以离散化。代码如下:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; typedef long long ll; #define inf (1LL<<50) #define maxn (3010) int N,cnt; ll f[maxn][maxn],ans = inf,A[maxn],B[maxn]; int main() { freopen("E.in","r",stdin); freopen("E.out","w",stdout); scanf("%d",&N); for (int i = 1;i <= N;++i) scanf("%I64d",A+i),A[i] -= i; memcpy(B,A,sizeof(B)); B[N+1] = -inf,B[N+2] = inf; sort(B+1,B+N+3); cnt = unique(B+1,B+N+3)-B-1; A[0] = -inf; A[N+1] = inf; memset(f,0x7,sizeof(f)); f[0][1] = 0; for (int i = 1;i <= N+1;++i) { ll tmp = inf; for (int j = 1;j <= cnt;++j) tmp = min(tmp,f[i-1][j]),f[i][j] = tmp+abs(A[i]-B[j]); } for (int i = 1;i <= cnt;++i) ans = min(ans,f[N+1][i]); printf("%I64d",ans); fclose(stdin); fclose(stdout); return 0; }
相关文章推荐
- 【CodeForces】713 C. 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)(将一个数组变成严格单增数组的最少步骤)
- 【DP】[CodeForces - 713C]Sonya and Problem Wihtout a Legend
- [堆与斜率] Codeforces 713C - Sonya and Problem Wihtout a Legend
- Codeforces 713C C. Sonya and Problem Wihtout a Legend (经典DP)
- hihocoder#1529 : 不上升序列&&Codeforces-713C:Sonya and Problem Wihtout a Legend(思维)
- Codeforces-713C-Sonya and Problem Wihtout a Legend
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend(贪心+DP)
- Codeforces_714E:Sonya_and_Problem_Wihtout_a_Legend(DP+想法题)
- Codeforces Round #371 C. Sonya and Problem Wihtout a Legend (DP)
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心
- 动态规划,离散化(Sonya and Problem Wihtout a Legend,cf 713C)
- Codeforces Round 371 C Sonya and Problem Wihtout a Legend
- cf 714 e Sonya and Problem Wihtout a Legend
- 【CF713C】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)
- Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend (DP/LIS变形)