题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+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. sqlite3内存不断增加的原因

    数据库是这样设计的:用内存保存数据,以提高增删查改的速度,同时把数据写入磁盘,让数据落地. 如果不删除数据库里的数据,随着数据不断地添加到数据库,数据库越来越大,RES内存也越来越大. 见测试代码a. ...

  2. yii2 批量插入or更新

    $sql1 = 'insert into business_ip (gid, name, area, belongName, belongArea, destIPv4, created, update ...

  3. java 同步锁方法

    方法一:动态同步锁 class Demo_thread implements Runnable{ public static int sum = 0; public synchronized void ...

  4. hdu4727 The Number Off of FFF

    理解错题意,wa了几次. 我一开始的理解忽略了实际背景,认为错报是绝对的,不依赖于其左边的人. 而实际上某士兵报数的对错取决且仅取决于他所报的数与其左邻所报的数. 所以假设第一个人没有报错,则其后必有 ...

  5. Cow Bowling

    Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15585 Accepted: 10363 Descrip ...

  6. JAVA基础知识之网络编程——-关于阻塞IO/非阻塞IO/同步IO/异步IO的一些参考文章

    Java NIO之多个Selector的实现Java NIO类库Selector机制解析(上) Java NIO类库Selector机制解析(下) https://www.zhihu.com/ques ...

  7. Android mvp模式、mvvm模式

    MVC和MVP的区别2007年08月08日 星期三 上午 09:23 MVC和MVP到底有什么区别呢? 从这幅图可以看到,我们可以看到在MVC里,View是可以直接访问Model的!从而,View里会 ...

  8. NSIS学习记录の----NSIS插件调用

    我们都知道NSIS可以和C或者C++混合编程,方法是NSIS调用C或C++的动态库,那么如何调用呢? 首先我们来创建动态库: // add.cpp : 定义 DLL 应用程序的导出函数. #inclu ...

  9. Objective-C之category

    http://blog.csdn.net/siemenliu/article/details/7835808

  10. Ecplise软件Devices看到两个相同设备问题

    Ecplise软件Devices看到两个相同设备问题 在使用过程中,连接一台设备,在Ecplise软件的Devices界面下突然看到2个设备,如下图: 图1 解决方案:先 kill-server, 再 ...