Codeforces 713C Sonya and Problem Wihtout a Legend DP
5 seconds
256 megabytes
standard input
standard output
Sonya was unable to think of a story for this problem, so here comes the formal description.
You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.
The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array.
Next line contains n integer ai (1 ≤ ai ≤ 109).
Print the minimum number of operation required to make the array strictly increasing.
7
2 1 5 11 5 9 11
9
5
5 4 3 2 1
12
In the first sample, the array is going to look as follows:
2 3 5 6 7 9 11
|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9
And for the second sample:
1 2 3 4 5
|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12
题意:求一个序列变为严格递增序列的最小花费。
思路:这题和POJ3666本质相似,POJ3666求的是非严格递增,在这题的条件下,我们只要将a[i]-i预处理,再按照相同的做法就可以保证严格递增了。QD某校赛比赛时候看到这题就觉得好像,结果一直没有想出来怎么改,POJ那题还是最近做过的呐。/.\另外这题还可以用优先队列做...
/** @Date : 2016-11-26-17.16
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/ #include<bits/stdc++.h>
#define LL long long
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+2000; int a[3030];
int b[3030];
LL dp[3030][3030];
int main()
{
int n;
while(~scanf("%d", &n))
{
MMF(dp);
for(int i = 1; i <= n; i++)
{
scanf("%d", a + i);
a[i] -= i;
b[i] = a[i];
}
sort(b + 1, b + 1 + n);
for(int i = 1; i <= n; i++)
{
dp[i][1] = dp[i - 1][1] + abs(a[i] - b[1]);
for(int j = 2; j <= n; j++)
{
dp[i][j] = min(dp[i - 1][j] + abs(a[i] - b[j]), dp[i][j - 1]);
}
}
printf("%lld\n", dp[n][n]);
}
return 0;
}
Codeforces 713C Sonya and Problem Wihtout a Legend DP的更多相关文章
- Codeforces 713C Sonya and Problem Wihtout a Legend(DP)
题目链接 Sonya and Problem Wihtout a Legend 题意 给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)
[题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...
- Codeforces 713C Sonya and Problem Wihtout a Legend
题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列. 题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数.那 ...
- 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 ...
- 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 ...
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)
题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...
- 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 ...
- CodeForces 714E Sonya and Problem Wihtout a Legend(单调数列和DP的小研究)
题意:给你n个数字,每个数字可以加减任何数字,付出变化差值的代价,求最后整个序列是严格单调递增的最小的代价. 首先我们要将这个题目进行转化,因为严格单调下是无法用下面这个dp的方法的,因此我们转化成非 ...
随机推荐
- [leetcode-753-Open the Lock]
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...
- nodejs笔记--模块篇(三)
文件模块访问方式通过require('/文件名.后缀') require('./文件名.后缀') requrie('../文件名.后缀') 去访问,文件后缀可以省略:以"/&qu ...
- iostat lsof
转至:http://www.51testing.com/html/48/202848-242043.html 命令总结: 1. top/vmstat 发现 wa%过高,vmstat b >1: ...
- HTML5 Geolocation位置信息定位总结
现在定位功能很常用,所以抽出一些时间将这个功能的知识总结一下作为知识梳理的依据.HTML5 Geolocation的定位用法很简单,首先请求位置信息,用户同意,则返回位置信息.HTML5 Geoloc ...
- block知识总结
一.block在内存中存在的形式 1.当把block句法写在函数或者方法外面时,系统会在静态数据区分配一块内存区域给block对象.这片区域在程序执行期会一直存在. 2.当block句法写在函数或者方 ...
- android入门 — AlertDialog对话框
常见的对话框主要分为消息提示对话框.确认对话框.列表对话框.单选对话框.多选对话框和自定义对话框. 对话框可以阻碍当前的UI线程,常用于退出确认等方面. 在这里主要的步骤可以总结为: 1.创建Aler ...
- C++ 普通函数和虚函数调用的区别
引出:写个类A,声明类A指针指向NULL,调用类A的方法会有什么后果,编译通过吗,运行会通过吗? #include<stdio.h> #include<iostream> us ...
- Tiny4412 LED 程序
package cn.hyc.led; import android.os.Bundle; import android.app.Activity; import android.view.Menu; ...
- SpringBoot事件监听
代码演示: package com.boot.event.eventdemo; import org.springframework.boot.SpringApplication; import or ...
- bzoj2676 Contra
题意: 给定N,R,Q,S 有N个关卡,初始有Q条命,且任意时刻最多只能有Q条命 每通过一个关卡,会得到u分和1条命,其中u=min(最近一次连续通过的关数,R) 若没有通过这个关卡,将失去一条命,并 ...