【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 ...
随机推荐
- NFS 它的目的就是想让不同的机器、不同的作业系统可以彼此分享个别的档案啦
NFS即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件, ...
- 基于图形学混色问题OpenGl的收获
void myDisplay(void) {glClearColor(0.0f,0.0f,0.0f,1.0f); glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_B ...
- bl bl bl bl bl
package com.dh.activiti; import org.springframework.web.servlet.HandlerInterceptor; import org.sprin ...
- (转)ActiveMQ的重连机制
花了一天的时间,终于搞明白了我的疑问. failover://(tcp://localhost:6168)?randomize=false&initialReconnectDelay=100& ...
- TP中if标签
if标签 If标签如果php中if语句的作用,if是用于流程控制的. 在ThinkPHP中if标签也是用于流程控制的. If标签的语法格式: <if condition=’条件表达式’> ...
- Android Studio- 把项目提交到SVN中操作方法
第一步 下载SVN,下载完成之后,需要吧command line client tools点击修改安装 然后Crash Reporter点击选择取消安装 如果不进行该操作,则可能在C:\Program ...
- .net下使用NPOI读取Excel表数据
这里只写MVC下的情况 public ActionResult Index() { var path = Server.MapPath(@"/content/user.xlsx") ...
- BZOJ 2004 公交线路(状压DP+矩阵快速幂)
注意到每个路线相邻车站的距离不超过K,也就是说我们可以对连续K个车站的状态进行状压. 然后状压DP一下,用矩阵快速幂加速运算即可. #include <stdio.h> #include ...
- bzoj5090[lydsy11月赛]组题
裸的01分数规划,二分答案,没了. #include<cstdio> #include<algorithm> using namespace std; const int ma ...
- Race to 1 UVA - 11762 (记忆dp概率)
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...