题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+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. master-slave(主/从)模式

    主从模式 一般来说用在数据库集群比较多,主要是实现读写分离.对于数据库应用而言基本上是读大于写,因此由 Master 服务器负责增.删.改操作,由 Slave 负责读操作(也就是 SELECT),Ma ...

  2. sqlite加密

    一直使用sqlite来管理本地的数据,但是Xcode中的SDK中集成的sqlite是免费的,不提供加密模块,但是程序中用到的很多数据,有时候是不想让别人看到,一开始虑修改sqlite的源码,自己重新编 ...

  3. xib托线出来的为什么是weak而不是strong

    因为控件他爹( view.superview )已经揪着它的小辫了( strong reference ),你( viewController )眼瞅着( weak reference )就好了. 当 ...

  4. mysql5.5.x升级到mysql5.6.x

    大概步骤是: 把配置文件添加:skip-grant-tables参数,把basedir升级成新版本,启动mysql,执行命令:mysql_upgrade升级一下字典信息,然后flush privile ...

  5. (java)==和equals()的使用小结

    1.如果两个变量说基本数据类型,且都是数值类型,eg.65f,65(不一定要求数据类型严格相同),只要两个变量的值相等,就将返回true int it=65; float fl=65.0f; char ...

  6. java 基本类型之间的转换

    基本数据类型从低级到高级是:byte  short int long float double ,char 类型比int 类型之后的都要低 下面通过一个例子说明: import javax.swing ...

  7. 第十一章 Android 内核驱动——Alarm

    11.1  基本原理 Alarm 闹钟是 android 系统中在标准 RTC 驱动上开发的一个新的驱动,提供了一个定时器 用于把设备从睡眠状态唤醒,当然因为它是依赖 RTC 驱动的,所以它同时还可以 ...

  8. 自己模拟实现spring IOC原理

    1.1.IoC是什么 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对 ...

  9. linux(centos6.5 i386)安装mysql5.6源码包

    在开始安装前,先说明一下mysql-5.6.4与较低的版本在安装上的区别,从mysql-5.5起,mysql源码安装开始使用cmake了,因此当我们配置安装目录./configure --perfix ...

  10. HDU1757 A Simple Math Problem 矩阵快速幂

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...