Codeforces Round #371 (Div. 2) E 【DP+离散化 LIS 】用最小代价把序列变成严格递增序列
2016-09-15 16:08
447 查看
传送门:codeforces 714 E. Sonya and Problem Wihtout a Legend
描述:
E. Sonya and Problem Wihtout a Legend
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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
output
input
output
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
题意:
给一个序列,可以给每一个数加减一个数,代价为他们改变的数的绝对值,那么要求用最小代价把序列变成严格递增的
思路:
有一个非严格递增的版本:POJ3666 题解见Here
而本题的严格如何转化非严格,直接加一行代码,a[i]-=i;
原理
推导过程大致如下:a[i] < a[i+1] —> a[i] <= a[i+1]-1 —> a[i]-i <= a[i+1]-(i+1)—> b[i]<=b[i+1]
代码:
描述:
E. Sonya and Problem Wihtout a Legend
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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
题意:
给一个序列,可以给每一个数加减一个数,代价为他们改变的数的绝对值,那么要求用最小代价把序列变成严格递增的
思路:
有一个非严格递增的版本:POJ3666 题解见Here
而本题的严格如何转化非严格,直接加一行代码,a[i]-=i;
原理
推导过程大致如下:a[i] < a[i+1] —> a[i] <= a[i+1]-1 —> a[i]-i <= a[i+1]-(i+1)—> b[i]<=b[i+1]
代码:
#include <bits/stdc++.h> #define ll __int64 using namespace std; const int N=3005; int n,a ,b ; ll dp ; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } void solve(){ for(int i=1; i<=n; i++){ ll mn=dp[i-1][1]; for(int j=1; j<=n; j++){ mn=min(mn, dp[i-1][j]); dp[i][j]=abs(a[i]-b[j])+mn; } } ll ans=dp [1]; for(int i=2; i<=n; i++){ ans=min(ans, dp [i]); } printf("%I64d\n",ans); } int main(){ read(n); for(int i=1; i<=n; i++){ read(a[i]); a[i]-=i; b[i]=a[i]; } sort(b+1, b+n+1); solve(); return 0; }
相关文章推荐
- 【codeforces 13 C】【DP + 离散化 + 贪心+滚动数组 】C. Sequence【用最小代价把序列变成非严格递增序列】
- poj 1631 Bridging signals (LIS 最长递增子序列 DP-二分)
- nyoj44 nyoj17 HDU1087 DP动规 连续字符串的和最大值 单调递增最长子序列 单调递增子序列最小个数 非连续最大递增子序列
- 严格上升子序列 dp CF Round_FF div1 A
- Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
- Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题
- zoj1986 Bridging Signals (dp,最长递增序列,LIS)
- 【串和序列处理 7】LIS 最长递增子序列
- hdu 4352 XHXJ's LIS(dp,压缩版递增序列,5级)
- [zz]最长递增子序列的求法 LIS
- 51 Nod 1134 最长递增子序列 LIS NlogN 写法
- nyoj17单调递增最长子序列(dp)
- 动态规划 —— 最长递增子序列(LIS)
- 最长单调递增子序列-LIS问题
- 单调递增最长子序列(南阳oj17)(经典dp)
- [LeetCode] Minimum Swaps To Make Sequences Increasing 使得序列递增的最小交换
- HDU1069 Monkey and Banana DP非连续单调递增序列的最大和的变种
- hdu1087 - Super Jumping! Jumping! Jumping! (dp 求递增子序列的最大和)
- nyoj--17 单调递增最长子序列(LIS)