【CodeForces】713 C. Sonya and Problem Wihtout a Legend
【题目】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的更多相关文章
- 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 ...
- 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. 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 ...
- Codeforces Round #371 (Div. 1) C - Sonya and Problem Wihtout a Legend
C - Sonya and Problem Wihtout a Legend 思路:感觉没有做过这种套路题完全不会啊.. 把严格单调递增转换成非严格单调递增,所有可能出现的数字就变成了原数组出现过的数 ...
- 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 713C Sonya and Problem Wihtout a Legend(DP)
题目链接 Sonya and Problem Wihtout a Legend 题意 给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...
- 把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend
//把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend //dp[i][j]:把第i个数转成第j小的数,最小花费 //此题与po ...
随机推荐
- 第二章 script元素
<script>元素 async:可选.表示应该立即下载脚本,但不应妨碍页面中的其他操作,比如下载其他资源或等待加载其他脚本.只对外部脚本文件有效. charset:可选.表示通过 ...
- 把a文件删除b文件中的相同的行
grep -vxFf b.txt a.txt > newa.txt 更好的方法是 comm - - b.txt a.txt > newa.txt 来自Tool in unix to sub ...
- Spark Shuffle之Sort Shuffle
源文件放在github,随着理解的深入,不断更新,如有谬误之处,欢迎指正.原文链接https://github.com/jacksu/utils4s/blob/master/spark-knowled ...
- NFS 它的目的就是想让不同的机器、不同的作业系统可以彼此分享个别的档案啦
NFS即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件, ...
- Java第一次笔记
- 31. Ubuntu15.04系统中如何启用、禁用客人会话
https://jingyan.baidu.com/article/046a7b3edf9639f9c27fa995.html 31. Ubuntu15.04系统中如何启用.禁用客人会话 听语音 | ...
- arp_filter的验证,使用net namespace
使用网络命名空间:net namespace 在namespace ns1中增加了两个网卡 sudo ip netns add ns1 sudo ip link add veth0 type veth ...
- poj3107 Godfather 求树的重心
Description Last years Chicago was full of gangster fights and strange murders. The chief of the pol ...
- [十三]SpringBoot 之 过滤器(Filter)和监听器(Listener)
过滤器(Filter)和 监听器(Listener)的注册方法和 Servlet 一样,不清楚的可以查看下上一篇文章 代码示例 package me.shijunjie.filter; import ...
- 【纪念】NOIP2018前夕——一些想说的话
刚刚复习了一下相关的内容,决定一会儿就洗洗睡了.在睡觉之前,决定写点东西. 有的时候真的很迷茫,选择了一条超过自己能力范围的路,每天挣扎在各种各样难题的面前,文化成绩一落千丈……在从前觉得这一切都是有 ...