Codeforces Beta Round #13 C. Sequence (DP)
题目大意
给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降
数列中每个数为 -109~109 中的一个数
做法分析
先这样考虑:如果操作的次数最少,那么最终得到的不降的数列,必然是由原始数列中的数组成的,具体的证明可以使用反证法
知道了上面讲述的性质,这题就好搞了
先将原始数列(设为 A,共 n 个数)中所有的数去重并从小到达排序,保存在另一个数列中(设为 B,共 m 个数)
定义状态:f[i][j] 表示将原始数列中的前 i 个数变成单调不降,第 i 个数最多为 B[j] 的最少操作次数
初始化 f[0][0]=abs(A[0]-B[0]), f[0][i]=min{ f[0][i-1], abs(A[0]-B[i]) }
初始化 f[i][0]=f[i-1][0]+abs(A[i]-A[0]),那么有:f[i][j]=min( f[i][j-1], f[i-1][j]+abs(A[i]-B[j]) )
目标状态:f[n-1][m-1]
由于内存的关系,可以使用滚动数组
参考代码
1 #include <iostream>
2 #include <cstring>
3 #include <cstdio>
4 #include <algorithm>
5 #include <vector>
6
7 using namespace std;
8
9 typedef long long LL;
10 const int N=5003;
11 const LL INF=(1LL)<<60;
12
13 LL f[2][N], A[N], B[N];
14 vector <int> tub;
15 int n, m;
16
17 int main() {
18 while(scanf("%d", &n)!=EOF) {
19 tub.clear(), m=0;
20 for(int i=0; i<n; i++) {
21 scanf("%I64d", &A[i]);
22 tub.push_back(A[i]);
23 }
24 sort(tub.begin(), tub.end());
25 for(int i=0; i<n; i++) {
26 while(i+1<n && tub[i]==tub[i+1]) i++;
27 B[m++]=tub[i];
28 }
29 memset(f, 0, sizeof f);
30 f[0][0]=abs(A[0]-B[0]);
31 for(int i=1; i<m; i++) f[0][i]=min(f[0][i-1], abs(A[0]-B[i]));
32 for(int i=1; i<n; i++) {
33 int cur=i&1, lst=cur^1;
34 f[cur][0]=f[lst][0]+abs(A[i]-B[0]);
35 for(int j=1; j<m; j++) f[cur][j]=min(f[cur][j-1], f[lst][j]+abs(A[i]-B[j]));
36 }
37 printf("%I64d\n", f[(n-1)&1][m-1]);
38 }
39 return 0;
40 }
C. Sequence
题目链接 & AC 通道
Codeforces Beta Round #13 C. Sequence
Codeforces Beta Round #13 C. Sequence (DP)的更多相关文章
- Codeforces Beta Round #13 E. Holes (分块)
E. Holes time limit per test 1 second memory limit per test 64 megabytes input standard input output ...
- Codeforces Beta Round #10 D. LCIS(DP&LCIS)
D. LCIS time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- codeforces 486 E. LIS of Sequence(dp)
题目链接:http://codeforces.com/contest/486/problem/E 题意:给出n个数,如果一个数满足不属于最长递增序列,那么输出1,如果属于最长递增序列但是不属于所有最长 ...
- Codeforces Beta Round #96 (Div. 2) (A-E)
写份DIV2的完整题解 A 判断下HQ9有没有出现过 #include <iostream> #include<cstdio> #include<cstring> ...
- Codeforces Beta Round #7--D. Palindrome Degree(Manacer)
题目:http://blog.csdn.net/winddreams/article/details/44218961 求出每一个点为中心的最长字符串,推断该串是不是从开头的回文串. #include ...
- Codeforces Beta Round #13 E. Holes 分块暴力
E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Beta Round #17 C. Balance (字符串计数 dp)
C. Balance time limit per test 3 seconds memory limit per test 128 megabytes input standard input ou ...
- Educational Codeforces Round 51 D. Bicolorings(dp)
https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...
随机推荐
- [J2ME] 基本框架框架
import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import jav ...
- jQuery的简单入门练习
<html> <head> <meta charset="utf-8"> <title>jQuery的练习</title> ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- Python面试题(二)
打印九九乘法表 思路:利用字符串的连接,梯形输出结果 >>> def st(num): ... l = [] ... for x in xrange(1, num + 1): ... ...
- python实现curl功能
之前写过一篇文章关于python CURL模块的,在这里我们从urllib来实现同样的功能.具体代码如下: import urllib import urllib2 import json #发起请求 ...
- nload 实时网速查看
nload eth0 -u K Device eth0 [192.168.0.33] (1/1):=================================================== ...
- 招聘:web前端开发(中级、高级均可)
web前端开发(中级.高级均可) 工作地点:广东-深圳 工作年限:2年 学历要求:本科 招聘分类:前端开发工程师 工资范围:面议 招聘人数:3 发布日期:2014/07/29 截止日期:2014/08 ...
- 解决连接MySql速度慢的方法
最近在Linux服务器上安装MySql5后,本地使用客户端连MySql速度超慢,本地程序连接也超慢.解决方法:在配置文件my.cnf的[mysqld]下加入skip-name-resolve. 原因是 ...
- Socket编程基本流程实践
通讯基本流程图如下所示: Server端代码(ServerDemo.cpp): #include <WinSock2.h> #include <Windows.h> #incl ...
- 【Vegas原创】RHEL6.2安装vmtools
1,mount虚拟cdrom # mount /dev/cdrom /mnt/cdrom 2, ls一下,看有没有 # cd /mnt/cdrom # ls –l ...