codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)
题目链接:
C. Sonya and Problem Wihtout a Legend
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 题意: 把这个序列变成严格的单调递增序列,每+1或者-1都花费1,最小花费是多少; 思路: 严格单调递增的变成非严格的可以a[i]-i;然后就是转移了;
dp[i][j]表示第i个数变成j可以满足题目要求的最小花费,现在dp[i][j]=min(dp[i-1][k])+abs(a[i]-j);
j的范围太大,可以排序离散化,用j表示第j个数,那么dp[i][j]=min(dp[i-1][k])+abs(a[i]-b[j]);
这样转移的时间复杂度是O(n*n*n)的,所以还要优化,还可以发现dp[i][j]的转移方程中min(d[i-1][k])是上一层的前缀最小值;
所以可以用一个数组保存下来,复杂度就降为O(n*n)了; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const int mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=(1<<20)+10;
const int maxn=3e3+110;
const double eps=1e-12; int n,a[maxn],b[maxn];
LL dp[maxn][maxn],f[maxn];
int main()
{
read(n);
For(i,1,n)
{
read(a[i]);
a[i]=a[i]-i;
b[i]=a[i];
}
sort(b+1,b+n+1);
For(i,0,n)f[i]=inf;
For(i,1,n)
{
For(j,1,n)
{
if(i==1)dp[i][j]=abs(a[i]-b[j]);
else dp[i][j]=f[j]+abs(a[i]-b[j]);
f[j]=min(f[j-1],dp[i][j]);
}
}
cout<<f[n]<<endl;
return 0;
}
codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)的更多相关文章
- 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 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 ...
- 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 713 C Sonya and Problem Wihtout a Legend
Description Sonya was unable to think of a story for this problem, so here comes the formal descript ...
- 把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend
//把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend //dp[i][j]:把第i个数转成第j小的数,最小花费 //此题与po ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(DP)
题目链接 Sonya and Problem Wihtout a Legend 题意 给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...
- 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 ...
- 【CodeForces】713 C. Sonya and Problem Wihtout a Legend
[题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...
- Codeforces Round #371 (Div. 1) C - Sonya and Problem Wihtout a Legend
C - Sonya and Problem Wihtout a Legend 思路:感觉没有做过这种套路题完全不会啊.. 把严格单调递增转换成非严格单调递增,所有可能出现的数字就变成了原数组出现过的数 ...
随机推荐
- FL2440驱动添加(3)LCD驱动添加学习笔记
FL2440 LCD内置控制器,320*240 TFT型LCD. 自我理解总结的两种添加驱动模式: 非platform方式添加驱动: 加载驱动: 1,硬件初始化,申请内存,并作地址映射 2,分配设备号 ...
- C++ 面向对象的三个特点--继承与封装(一)
面试的时候经常会有很多概念性的东西,许久不用都会很生疏,特意整理一下方便自己以后不记得了可以查看一下,也顺便帮助自己复习一下. 概念 继承是面向对象程序设计的一个重要特性,它允许在既有类的基础上创建新 ...
- A除以B问题
描述:本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入:输入在1行中依次给出A和B,中间以1空格分隔. 输出: ...
- Mysql之执行计划
1.explain分析sql语句 例如: EXPLAIN ) ORDER BY bi.`publish_time` 返回结果: 而今天检查的不是这条sql,远比这条复杂,不过也能反映情况了. (1 ...
- javascript作用域链学习笔记
作用域链 "JavaScript中的函数运行在它们被定义的作用域里,而不是它们被执行的作用域里." --权威指南 在JavaScript中,一切皆对象,包括函数.函数对象和其它对象 ...
- SharePoint 2013 列表多表联合查询
在SharePoint的企业应用中,遇到复杂的逻辑的时候,我们会需要多表查询:SharePoint和Sql数据表一样,也支持多表联合查询,但是不像Sql语句那样简单,需要使用SPQuery的Joins ...
- 内存管理2(主讲MRR)
内存管理2 我们讨论过properties 后,所有的内存管理系统都是通过控制所有对象的生命周期来减少内存的占用.iOS和OS X应用程序完成这些是通过对象拥有者来实现的,它保证了只要对象使用就会存在 ...
- Swift 中的函数
学习来自<极客学院:Swift中的函数> 工具:Xcode6.4 直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会 练习一: import Foundation //函数 ...
- SAM4E单片机之旅——20、DMAC之使用Multi-buffer进行内存拷贝
这次使用这个DMAC的Multi-buffer传输功能,将两个缓冲区的内容拷贝至一个连续的缓冲区中. 一. DMAC 在M4中,DMA控制器(DMAC)比外设DMA控制器(PDC)要复杂,但是功能更加 ...
- Asp.Net实现WebApi跨域 (非MVC)
目前WebApi在使用上大部分都是跟MVC组合的,而且使用起来也确实十分便利. 但有时候我们也需要在WebForm中使用WebApi,二者还是有一定区别的. 首先看下结构 ①ApiControlle ...