Codeforces 713C Sonya and Problem Wihtout a Legend
题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列。
题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数。那么O(n^2)复杂度dp一下即可。
dp[i][j]表示第i个数变成第j小的数,dp[i][j] = min (dp[i-1][1 ... j])+abs(a[i]-b[j]).
那么对于严格递增序列,将a[i]变成a[i]-i后,再照非严格递增序列跑一遍dp即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[][];
int a[], b[];
int main(){
int n;
cin >> n;
for(int i = ; i <= n; i++) {
cin >> a[i];
a[i] -= i;
b[i] = a[i];
}
sort(b+, b+n+);
int tot = unique(b+, b+n+)-b;
memset(dp, 0x3f, sizeof(dp));
for(int j = ; j < tot; j++) dp[][j] = ;
for(int i = ; i <= n; i++)
for(int j = ; j < tot; j++){
dp[i][j] = abs(a[i]-b[j])+dp[i-][j];
dp[i][j] = min(dp[i][j], dp[i][j-]);
}
printf("%lld\n", dp[n][tot-]);
return ;
}
更优秀的复杂度是O(nlogn)的解法。同样需要转为非严格递增。
int n;
priority_queue<long long> q; int main() {
scanf("%d",&n);
long long ans = ;
for(int i = ;i < n;i++) {
long long x;
scanf("%lld",&x);
x -= i;
q.push(x);
if(q.top() > x) {
ans += q.top()-x;
q.pop();
q.push(x);
}
}
printf("%lld\n",ans);
return ;
}
Codeforces 713C 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 Sonya and Problem Wihtout a Legend(DP)
题目链接 Sonya and Problem Wihtout a Legend 题意 给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)
[题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...
- codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)
题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...
- 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 714E Sonya and Problem Wihtout a Legend(单调数列和DP的小研究)
题意:给你n个数字,每个数字可以加减任何数字,付出变化差值的代价,求最后整个序列是严格单调递增的最小的代价. 首先我们要将这个题目进行转化,因为严格单调下是无法用下面这个dp的方法的,因此我们转化成非 ...
- 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 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. 1) C. Sonya and Problem Wihtout a Legend 贪心
C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...
随机推荐
- Zend Debugger 配置
到官网 http://www.zend.com/en/products/studio/downloads 下载 windows 版 Studio Web Debugger 打开下载得到的压缩包,里面有 ...
- 搭建LAMP
RPM包和源码包存放位置 /usr/local/src 源码包编译安装位置 /usr/local/apache /usr/local/mysql /usr/local/php 默认MySQL 数据库位 ...
- Git and GitHub
1.GitHub 创建一个仓库 2.进入本地要管理的某个文件夹下,感觉目录的操作命令和linux里面差不多, $git init 此时该文件下就会多出一个.git的文件 3.进入要上传的仓库,右键gi ...
- gets()和getchar()还有getch()的区别
getch()和getchar()区别:1.getch(): 所在头文件:conio.h 函数用途:从控制台读取一个字符,但不显示在屏幕上例如: char ch;或int ch: getch();或c ...
- easyui datagrid 可过滤行的数据表格 导出
//过滤栏表格导出数据 /* xukf * id datagrid id * url Action 路 ...
- JQuery知识快览之三—JQuery对象集
本文讲述JQuery对象集的各相关知识 获取JQuery对象集 JQuery对象,和DOM对象是两个不同的东西,JQuery对象可以是DOM对象的封装,但是JQuery对象不只是DOM对象的封装,它还 ...
- 20150625_Andriod_02_ListView2_多条目显示_选中
android listview 参考地址: http://www.cnblogs.com/zhengbeibei/archive/2013/05/14/3078805.html http://xy ...
- JAVA基础知识之网络编程——-基于AIO的异步Socket通信
异步IO 下面摘子李刚的<疯狂JAVA讲义> 按照POSIX标准来划分IO,分为同步IO和异步IO.对于IO操作分为两步,1)程序发出IO请求. 2)完成实际的IO操作. 阻塞IO和非阻塞 ...
- HashTable的实现原理
转载:http://wiki.jikexueyuan.com/project/java-collection/hashtable.html 概述 和 HashMap 一样,Hashtable 也是一个 ...
- zoj Abs Problem
Abs Problem Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Alice and Bob is pl ...