Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend (DP/LIS变形)
2016-09-14 17:02
417 查看
题目点我点我点我
题目大意:给一个序列,每次操作能对其进行加1或减1操作,求令序列严格递增的最小操作数。
解题思路:首先新的序列对于原序列来说要花费最小,所以新序列是基于原序列进行变换的(貌似这是废话=。=,额,事实没表达出这个)。
b[i]=a[i]-i,然后对b[ ] sort 一下,
dp[i][j]表示处理第i个数时,第i个数是变成b[j]的最小花费(为什么呀?就是上面那句话所说的,基于原序列变换),最后min一下就可。
POJ3666 与这题基本一样,就是非严格递增,故a[i]不需要减去i。
传送门~
题目大意:给一个序列,每次操作能对其进行加1或减1操作,求令序列严格递增的最小操作数。
解题思路:首先新的序列对于原序列来说要花费最小,所以新序列是基于原序列进行变换的(貌似这是废话=。=,额,事实没表达出这个)。
b[i]=a[i]-i,然后对b[ ] sort 一下,
dp[i][j]表示处理第i个数时,第i个数是变成b[j]的最小花费(为什么呀?就是上面那句话所说的,基于原序列变换),最后min一下就可。
POJ3666 与这题基本一样,就是非严格递增,故a[i]不需要减去i。
传送门~
/* *********************************************** ┆ ┏┓ ┏┓ ┆ ┆┏┛┻━━━┛┻┓ ┆ ┆┃ ┃ ┆ ┆┃ ━ ┃ ┆ ┆┃ ┳┛ ┗┳ ┃ ┆ ┆┃ ┃ ┆ ┆┃ ┻ ┃ ┆ ┆┗━┓ 马 ┏━┛ ┆ ┆ ┃ 勒 ┃ ┆ ┆ ┃ 戈 ┗━━━┓ ┆ ┆ ┃ 壁 ┣┓┆ ┆ ┃ 的草泥马 ┏┛┆ ┆ ┗┓┓┏━┳┓┏┛ ┆ ┆ ┃┫┫ ┃┫┫ ┆ ┆ ┗┻┛ ┗┻┛ ┆ ************************************************ */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <bitset> using namespace std; #define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++) #define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--) #define pb push_back #define mp make_pair const int inf_int = 2e9; const long long inf_ll = 2e18; #define inf_add 0x3f3f3f3f #define mod 1000000007 #define LL long long #define ULL unsigned long long #define MS0(X) memset((X), 0, sizeof((X))) #define SelfType int SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);} SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;} #define Sd(X) int (X); scanf("%d", &X) #define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin()) #define all(a) a.begin(), a.end() typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef vector<int> vi; typedef vector<long long> vll; inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;} //#pragma comment(linker, "/STACK:102400000,102400000") LL dp[3005][3005]; int a[3005],b[3005]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); ios::sync_with_stdio(0); cin.tie(0); int n; n = read(); for(int i=1;i<=n;i++) { a[i] = read(); a[i] -= i; b[i] = a[i]; } sort(b+1,b+1+n); for(int j=1;j<=n;j++) dp[1][j] = abs(a[1]-b[j]); for(int i=2;i<=n;i++) { LL mi = inf_ll; for(int j=1;j<=n;j++) { mi = min(mi,dp[i-1][j]); dp[i][j] = mi + abs(a[i]-b[j]); } } LL ans = inf_ll; for(int i=1;i<=n;i++) ans = min(ans,dp [i]); printf("%I64d\n",ans); return 0; }
相关文章推荐
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend(贪心+DP)
- cf 372 div2 E. Sonya and Problem Wihtout a Legend dp
- cf/Codeforces Round #371 714E - Sonya and Problem Wihtout a Legend - dp +lis
- Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend(技巧 + 离散化dp)
- 【CF 713C】Sonya and Problem Wihtout a Legend(离散化+dp)<POJ 3666变形>
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)
- 【CF713C】Sonya and Problem Wihtout a Legend(离散化,DP)
- Codeforces Round #371 (Div. 1) 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 Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend
- Codeforces 713C C. Sonya and Problem Wihtout a Legend (经典DP)
- Codeforces Round #371 C. Sonya and Problem Wihtout a Legend (DP)
- Codeforces-713C-Sonya and Problem Wihtout a Legend
- 动态规划,离散化(Sonya and Problem Wihtout a Legend,cf 713C)
- Codeforces_714E:Sonya_and_Problem_Wihtout_a_Legend(DP+想法题)
- cf 714 e Sonya and Problem Wihtout a Legend
- [堆与斜率] Codeforces 713C - Sonya and Problem Wihtout a Legend
- hihocoder#1529 : 不上升序列&&Codeforces-713C:Sonya and Problem Wihtout a Legend(思维)