【CF713C】Sonya and Problem Wihtout a Legend(离散化,DP)
题意:给你一个数列,对于每个数字你都可以++或者−−
然后花费就是你修改后和原数字的差值,然后问你修改成一个严格递增的,最小花费
思路:很久以前做过一道一模一样的
严格递增很难处理,就转化为非严格递增处理
设a[i]<a[j],i<j
a[j]-a[i]>=j-i
a[j]-j>=a[i]-i
即将a[i]转化为a[i]-i处理
非严格递增情况下最终数列一定是由原先的数组成的,不可能出现某两个原数中间的值
dp[i,j]为第i位,结尾为第j大的数,转化成这样的数列的最小费用
dp[i,j]=min(dp[i-1,k]+abs(a[i]-b[j])) k<=j
第一项用前缀和优化
const oo=;
var dp,f:array[..,..]of int64;
a,b:array[..]of longint;
n,i,j:longint;
ans:int64; function min(x,y:int64):int64;
begin
if x<y then exit(x);
exit(y);
end; procedure qsort(l,r:longint);
var i,j,t,mid:longint;
begin
i:=l; j:=r; mid:=b[(l+r)>>];
repeat
while mid>b[i] do inc(i);
while mid<b[j] do dec(j);
if i<=j then
begin
t:=b[i]; b[i]:=b[j]; b[j]:=t;
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end; begin
//assign(input,'1.in'); reset(input);
// assign(output,'1.out'); rewrite(output);
readln(n);
for i:= to n do
begin
read(a[i]);
a[i]:=a[i]-i;
end;
b:=a;
qsort(,n);
for i:= to n do
begin
for j:= to n do f[i,j]:=oo;
for j:= to n do
begin
dp[i,j]:=abs(a[i]-b[j])+f[i-,j];
f[i,j]:=min(f[i,j-],dp[i,j]);
end;
end;
ans:=oo;
for i:= to n do ans:=min(ans,dp[n,i]);
writeln(ans);
//close(input);
// close(output);
end.
【CF713C】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$. 求把原序列变成 ...
- CF713C Sonya and Problem Wihtout a Legend & hihocoder1942 单调序列
这两个题是一样的,不过数据范围不同. 思路1: 在CF713C中,首先考虑使生成序列单调不下降的情况如何求解.因为单调上升的情况可以通过预处理将a[i]减去i转化成单调不下降的情况. 首先,生成的序列 ...
- CF713C Sonya and Problem Wihtout a Legend
考虑我们直接选择一个暴力\(dp\). \(f_{i,j} = min_{k<=j}\ (f_{i - 1,k}) + |a_i - j|\) 我们考虑到我们直接维护在整个数域上\(min(f_ ...
- Codeforces713C Sonya and Problem Wihtout a Legend(DP)
题目 Source http://codeforces.com/problemset/problem/713/C Description Sonya was unable to think of a ...
- 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 C. Sonya and Problem Wihtout a Legend(dp or 思维)
题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...
- 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 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 ...
- 把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend
//把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend //dp[i][j]:把第i个数转成第j小的数,最小花费 //此题与po ...
- Sonya and Problem Wihtout a Legend
Sonya and Problem Wihtout a Legend Sonya was unable to think of a story for this problem, so here co ...
随机推荐
- VIM+ctags+cscope用法
使用vim + cscope/ctags,就能够实现Source Insight的功能,可以很方便地查看分析源代码. 关键词: vim, cscope, ctags, tags 1. 查看vi ...
- 判断是否是同一人的方法——equals()?在Person类中提供一个比较的方法compare()返回boolean值?对象自己和自己比?
判断是否是同一人的方法——equals() 不能直接用per1==per2,这不是对象内容的比较而是存放对象地址的值得比较 在Person类中提供一个比较的方法compare()返回boolean值 ...
- NOIP模拟赛 魔方
[题目描述] ccy(ndsf)觉得手动复原魔方太慢了,所以他要借助计算机. ccy(ndsf)家的魔方都是3*3*3的三阶魔方,大家应该都见过. (3的“顺时针”改为“逆时针”,即3 4以图为准.) ...
- Windows平台下使用vs code来调试python代码(2)
背景:上篇文章我们介绍了怎么搭建相关的环境,文章链接:https://www.cnblogs.com/yahuian/p/10507467.html,这篇文章来介绍怎么调试我们的程序. 1.Debug ...
- 【最大权闭合子图 tarjan】bzoj1565: [NOI2009]植物大战僵尸
dinic+tarjan板子练手题 Description Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏.Plants(植物)和Zombies(僵尸)是游戏的主角,其 中P ...
- centos7.4进入单用户模式
centos7.4进入单用户模式 1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/b ...
- loc与iloc函数的使用
Pandas中loc和iloc函数用法详解(源码+实例) https://blog.csdn.net/w_weiying/article/details/81411257 Pandas中loc,il ...
- 深入理解FIFO(包含有FIFO深度的解释)——转载
深入理解FIFO(包含有FIFO深度的解释) FIFO: 一.先入先出队列(First Input First Output,FIFO)这是一种传统的按序执行方法,先进入的指令先完成并引退,跟着才执行 ...
- ACM训练联盟周赛 A. Teemo's bad day
65536K Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his homework ...
- selenium2基本控件介绍及其代码
输入框:input 表现形式: 1.在html中一般为:<input id="user" type="text"> 主要操作: ...