【CodeForces】713 C. Sonya and Problem Wihtout a Legend

时间:2023-01-31 07:41:22

【题目】C. Sonya and Problem Wihtout a Legend

【题意】给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增。n<=10^5。

【算法】动态规划+前缀和优化

【题解】★令b[i]=a[i]-i,则a[i]递增等价于b[i]不递减

这样做之后,显然数字加减只能到b[i]中出现的数字,而不会出现其它数字。

令f[i][j]表示前i个数,第i个数字大小为c[j](第j大的数字)的最少操作次数。

f[i][j]=abs(b[i]-c[j])+min{f[i-1][k]},k<=j

令g[i][j]表示min{f[i][k]},k<=j,则有:

g[i][j]=min{ g[i][j-1],abs(b[i]-c[j])+g[i-1][j] }

初始g[0][i]=0。

复杂度O(n^2)。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define ll long long
#define lowbit(x) x&-x
using namespace std;
int read(){
char c;int s=,t=;
while(!isdigit(c=getchar()))if(c=='-')t=-;
do{s=s*+c-'';}while(isdigit(c=getchar()));
return s*t;
}
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a<b?b:a;}
int ab(int x){return x>?x:-x;}
//int MO(int x){return x>=MOD?x-MOD:x;}
//void insert(int u,int v){tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;}
/*------------------------------------------------------------*/
const int inf=0x3f3f3f3f,maxn=; int n,a[maxn],b[maxn];
ll f[maxn][maxn];
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)a[i]=read()-i,b[i]=a[i];
sort(b+,b+n+);
int x=;
for(int i=;i<=n;i++)f[x][i]=;
for(int i=;i<=n;i++){
x=-x;f[x][]=1ll<<;
for(int j=;j<=n;j++)f[x][j]=min(f[x][j-],ab(b[j]-a[i])+f[-x][j]);
}
printf("%lld",f[x][n]);
return ;
}

【CodeForces】713 C. Sonya and Problem Wihtout a Legend的更多相关文章

  1. 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 ...

  2. Codeforces Round &num;371 &lpar;Div&period; 1&rpar; C&period; Sonya and Problem Wihtout a Legend 贪心

    C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...

  3. Codeforces Round &num;371 &lpar;Div&period; 2&rpar;E&period; Sonya and Problem Wihtout a Legend&lbrack;DP 离散化 LIS相关&rsqb;

    E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  4. codeforces 713C C&period; Sonya and Problem Wihtout a Legend&lpar;dp&rpar;

    题目链接: C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 ...

  5. Codeforces Round &num;371 &lpar;Div&period; 1&rpar; C - Sonya and Problem Wihtout a Legend

    C - Sonya and Problem Wihtout a Legend 思路:感觉没有做过这种套路题完全不会啊.. 把严格单调递增转换成非严格单调递增,所有可能出现的数字就变成了原数组出现过的数 ...

  6. 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 ...

  7. codeforces 713C C&period; Sonya and Problem Wihtout a Legend&lpar;dp&rpar;(将一个数组变成严格单增数组的最少步骤)

    E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  8. Codeforces 713C Sonya and Problem Wihtout a Legend(DP)

    题目链接   Sonya and Problem Wihtout a Legend 题意  给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...

  9. 把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend

    //把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend //dp[i][j]:把第i个数转成第j小的数,最小花费 //此题与po ...

随机推荐

  1. ActiveMQ&lowbar;Mqtt的TCP丢包

    现象 Mqtt Consumer应该收到的消息少于预期,登录ActiveMQ的管理页面里的Topics,查看Messages Enqueued发现同样少于理应接收的数量. 定位问题 怀疑是TCP丢包, ...

  2. MSBuild简单介绍

    背景 托博客园的福,上周六,有家开发医疗行业系统的初创公司联系我,说在博客园上看到我关于WPF的几篇文章,邀请我去他们那里交流WPF相关的技术知识和心得体会.作为非大拿的我自然是受宠若惊,但对方好意相 ...

  3. 如何进行服务器的批量管理以及python 的paramiko的模块

    最近对公司的通道机账号进行改造管理,全面的更加深入的理解了公司账号管理的架构.(注:基本上所有的机器上的ssh不能使用,只有部分机器能够使用.为了安全的角度考虑,安装的不是公版的ssh,而都是定制版的 ...

  4. 2016&period;05&period;03&comma;英语&comma;《Vocabulary Builder》Unit 21

    sub, means 'under', as in subway, submarine, substandard. A subject is a person who is under the aut ...

  5. &lbrack;转&rsqb; add-apt-repository

    PS: 有些项目提供的是deb 地址,那么把deb地址加到repository里,下面是一个例子: sudo apt-get update sudo add-apt-repository 'deb h ...

  6. python 从windows上传文件到linux脚本

    import paramiko import datetime import os hostname = '192.168.112.132' username = 'root' password = ...

  7. pytroch nn&period;Module源码解析&lpar;1&rpar;

    今天在写一个分类网络时,要使用nn.Sequential中的一个模块,因为nn.Sequential中模块都没有名字,我一时竟无从下笔.于是决定写这篇博客梳理pytorch的nn.Module类,看完 ...

  8. Mapnik 3&period;0&period;20编译安装

    1. 确定epel安装 yum install -y epel-release 2. 按照<CentOS7.2部署node-mapnik>一文中的步骤,手动安装 gcc-6.2.0 和 b ...

  9. 利用guava来实现本地的cache缓存

    guava是谷歌提供的工具类,功能强大,举个例子,我我想把数据存到本地,该咋办?我们想到的只有是全局的Map和session中.如果我们想实现这个容器的大小呢?时间呢?不好搞吧. guava就有这样的 ...

  10. java进行url编码和解码

    public static String getURLEncoderString(String str) { String result = ""; if (null == str ...