题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列。

题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数。那么O(n^2)复杂度dp一下即可。

dp[i][j]表示第i个数变成第j小的数,dp[i][j] = min (dp[i-1][1 ... j])+abs(a[i]-b[j]).

那么对于严格递增序列,将a[i]变成a[i]-i后,再照非严格递增序列跑一遍dp即可。

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[][];
int a[], b[];
int main(){
int n;
cin >> n;
for(int i = ; i <= n; i++) {
cin >> a[i];
a[i] -= i;
b[i] = a[i];
}
sort(b+, b+n+);
int tot = unique(b+, b+n+)-b;
memset(dp, 0x3f, sizeof(dp));
for(int j = ; j < tot; j++) dp[][j] = ;
for(int i = ; i <= n; i++)
for(int j = ; j < tot; j++){
dp[i][j] = abs(a[i]-b[j])+dp[i-][j];
dp[i][j] = min(dp[i][j], dp[i][j-]);
}
printf("%lld\n", dp[n][tot-]);
return ;
}

更优秀的复杂度是O(nlogn)的解法。同样需要转为非严格递增。

 int n;
priority_queue<long long> q; int main() {
scanf("%d",&n);
long long ans = ;
for(int i = ;i < n;i++) {
long long x;
scanf("%lld",&x);
x -= i;
q.push(x);
if(q.top() > x) {
ans += q.top()-x;
q.pop();
q.push(x);
}
}
printf("%lld\n",ans);
return ;
}

Codeforces 713C Sonya and Problem Wihtout a Legend的更多相关文章

  1. Codeforces 713C Sonya and Problem Wihtout a Legend DP

    C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  2. Codeforces 713C Sonya and Problem Wihtout a Legend(DP)

    题目链接   Sonya and Problem Wihtout a Legend 题意  给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...

  3. Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)

    [题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...

  4. codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)

    题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...

  5. Codeforces C. Sonya and Problem Wihtout a Legend(DP)

    Description Sonya was unable to think of a story for this problem, so here comes the formal descript ...

  6. CodeForces 714E Sonya and Problem Wihtout a Legend(单调数列和DP的小研究)

    题意:给你n个数字,每个数字可以加减任何数字,付出变化差值的代价,求最后整个序列是严格单调递增的最小的代价. 首先我们要将这个题目进行转化,因为严格单调下是无法用下面这个dp的方法的,因此我们转化成非 ...

  7. codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)

    题目链接: C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 ...

  8. codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)

    E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  9. Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心

    C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...

随机推荐

  1. XCode5 真机调试及发布应用

    一.XCODE 真机测试 Xcode5已经很智能,只需生成一个开发证书,安装后,插入设备会自动添加,注意,当Mac系统升级后,证书需要重新生成. 证书生成步骤: 1.生成 CertificateSig ...

  2. 进程外session

    进程外session A  SqlServer 1.管理员身份运行cmd 2.更换目录  cd c:\Windows\Microsoft.NET\Framework\v4.0.30319> 3. ...

  3. YTU 2987: 调整表中元素顺序(线性表)

    2987: 调整表中元素顺序(线性表) 时间限制: 1 Sec  内存限制: 2 MB 提交: 1  解决: 1 题目描述 若一个线性表L采用顺序存储结构存储,其中所有元素都为整数.设计一个算法,将所 ...

  4. Ramsey theorem in Combinarotics

  5. Java学习之路(二)

    什么是变量? 1:计算机是一种嫉妒精确的机器 2:要将信息存储在计算机当中,就必须指明信息存储的位置和所需的内存空间: 3:在Java当中 使用声明语句来完成上述任务 变量的类型:

  6. VRRP配置与维护手册-1

    http://www.gpxz.com/diannao/hulianwang/80526.html 一  VRRP简介< xmlnamespace prefix ="o" n ...

  7. JS实现base64编码与解码

    var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ...

  8. Bitmap的分析与使用.md

    https://github.com/GeniusVJR/LearningNotes/blob/master/Part1/Android/Bitmap的分析与使用.md

  9. okhttp3教程(1)如何引入库

    官网: https://github.com/square/okhttp https://github.com/square/okio 1,使用okhttp3需要两个库 在build.gradle c ...

  10. Python 2.7.9 Demo - isinstance

    #coding=utf-8 #!/usr/bin/python a = 'abc'; print isinstance(a, str);