【CodeForces】713 C. Sonya and Problem Wihtout a Legend
2018-01-17 19:22
453 查看
【题目】C. Sonya and Problem Wihtout a Legend
【题意】给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增。n<=10^5。
【算法】动态规划+前缀和优化
【题解】★令b[i]=a[i]-i,则a[i]递增等价于b[i]不递减。
这样做之后,显然数字加减只能到b[i]中出现的数字,而不会出现其它数字。
令f[i][j]表示前i个数,第i个数字大小为c[j](第j大的数字)的最少操作次数。
f[i][j]=abs(b[i]-c[j])+min{f[i-1][k]},k<=j
令g[i][j]表示min{f[i][k]},k<=j,则有:
g[i][j]=min{ g[i][j-1],abs(b[i]-c[j])+g[i-1][j] }
初始g[0][i]=0。
复杂度O(n^2)。
#include<cstdio> #include<cstring> #include<cctype> #include<cmath> #include<queue> #include<stack> #include<set> #include<vector> #include<algorithm> #define ll long long #define lowbit(x) x&-x using namespace std; int read(){ char c;int s=0,t=1; while(!isdigit(c=getchar()))if(c=='-')t=-1; do{s=s*10+c-'0';}while(isdigit(c=getchar())); return s*t; } int min(int a,int b){return a<b?a:b;} int max(int a,int b){return a<b?b:a;} int ab(int x){return x>0?x:-x;} //int MO(int x){return x>=MOD?x-MOD:x;} //void insert(int u,int v){tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;} /*------------------------------------------------------------*/ const int inf=0x3f3f3f3f,maxn=3010; int n,a[maxn],b[maxn]; ll f[maxn][maxn]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)a[i]=read()-i,b[i]=a[i]; sort(b+1,b+n+1); int x=0; for(int i=1;i<=n;i++)f[x][i]=0; for(int i=1;i<=n;i++){ x=1-x;f[x][0]=1ll<<60; for(int j=1;j<=n;j++)f[x][j]=min(f[x][j-1],ab(b[j]-a[i])+f[1-x][j]); } printf("%lld",f[x] ); return 0; }View Code
相关文章推荐
- Codeforces 713 C Sonya and Problem Wihtout a Legend
- Codeforces-713C-Sonya and Problem Wihtout a Legend
- hihocoder#1529 : 不上升序列&&Codeforces-713C:Sonya and Problem Wihtout a Legend(思维)
- 【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)
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心
- cf 714 e Sonya and Problem Wihtout a Legend
- Codeforces Round #371 C. 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变形)
- Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend(技巧 + 离散化dp)
- 【CF713C】Sonya and Problem Wihtout a Legend(离散化,DP)
- Codeforces Round 371 C Sonya and Problem Wihtout a Legend
- 【CF 713C】Sonya and Problem Wihtout a Legend(离散化+dp)<POJ 3666变形>
- cf/Codeforces Round #371 714E - Sonya and Problem Wihtout a Legend - dp +lis
- cf 372 div2 E. Sonya and Problem Wihtout a Legend dp
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend(贪心+DP)