Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
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
题意:一个序列加减1变成严格单增最少操作数
很像LIS,然后就没时间了
d[i][j]表示前i个以j结尾的最少操作数
d[i][j]=min{d[i-1][k]+a[i]-j:k<=?j}
j离散化 --> m[j] sort(m) 可以发现最后的一个一定可以是原序列中的值
严格单调递增,直接处理好难我不会(因为那样的话不一定原序列结尾了,可能+1),网上题解上都是让a[i]-=i变成不严格
可以用mn维护min(d[i-1][k]),少了一层循环
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=;
const ll INF=1e19;
int n,k,a[N],m[N];
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
ll d[N][N],ans=INF;
void dp(){
for(int i=;i<=n;i++){
ll mn=INF;
for(int j=;j<=k;j++){
mn=min(mn,d[i-][j]);
d[i][j]=mn+abs(a[i]-m[j]);
}
}
} int main(int argc, const char * argv[]) {
n=read();
for(int i=;i<=n;i++) a[i]=m[i]=read()-i;
sort(m+,m++n);
k=unique(m+,m++n)-m-;//printf("k %d\n",k);
dp();
for(int i=;i<=k;i++) ans=min(ans,d[n][i]);
cout<<ans;
return ;
}
Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]的更多相关文章
- 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 Round #371 (Div. 1) C - Sonya and Problem Wihtout a Legend
C - Sonya and Problem Wihtout a Legend 思路:感觉没有做过这种套路题完全不会啊.. 把严格单调递增转换成非严格单调递增,所有可能出现的数字就变成了原数组出现过的数 ...
- 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 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 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) C. Sonya and Queries 水题
C. Sonya and Queries 题目连接: http://codeforces.com/contest/714/problem/C Description Today Sonya learn ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries —— 二进制压缩
题目链接:http://codeforces.com/contest/714/problem/C C. Sonya and Queries time limit per test 1 second m ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries[Map|二进制]
C. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries
题目链接 分析:01trie树,很容易就看出来了,也没什么好说的.WA了一发是因为没有看见如果数字位数大于01序列的时候01序列也要补全0.我没有晚上爬起来打,白天发现过的人极多. /******** ...
随机推荐
- asp.net正则模板引擎代码
我们申明一个数组 ]; 接下来关键的正则表达式: RegexOptions options = RegexOptions.None; //嵌套模板标签(兼容) r[] = new Regex(@&qu ...
- 定制Eclipse IDE之功能篇(二)
上文回顾:定制Eclipse IDE之功能篇(一) 这一篇文章将记录一些Eclipse插件小功能,Smart but Useful. 一.设置工作空间 文本文件的编码 解决办法: 在org ...
- DIV+CSS+JS基础+正则表达式
...............HTML系列.................... DIV元素是用来为HTML文档内大块(block-level)的内容提供结构和背景的元素.DIV的起始 ...
- DropDownList
DropDownList 1,DataValueField获取或设置为各列表项提供值的数据源字段 绑定的是唯一的标识 比如是id列 使用SelectedValue获取绑定的数据使用的前端看不到的数据类 ...
- js控住DOM实现发布微博简单效果
这段代码的效果具体是输入标题和内容,点击发布把消息发布出去,并使最新的消息始终在内容的最上面,代码为: <!DOCTYPE html> <html lang="en&quo ...
- 程序新能优化-SQL优化
- react native与现有的应用程序集成
(1)通过cocopods 集成 ,以下内容 参考 http://wiki.jikexueyuan.com/project/react-native/integration-existing.html ...
- KVO的底层实现
1.KVO是基于Runtime机制实现的: 2.当某个类的对象的某个属性第一次被观察时,系统会在运行期间动态地创建该类的一个派生类,在这个派生类中重写基类的任何被观察属性的setter方法,派生类在被 ...
- Mac系统如何编辑hosts文件
Mac系统如何编辑hosts文件 Hosts 是一个没有扩展名的系统文件,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系 统会首先 ...
- iOS 边框显示不完全
设置边框为1以上就可以了,如果不觉得宽的话!