Codeforces Beta Round #13 C. Sequence (DP)

时间:2022-09-05 11:59:48

题目大意

给一个数列,长度不超过 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)的更多相关文章

  1. Codeforces Beta Round &num;13 E&period; Holes (分块)

    E. Holes time limit per test 1 second memory limit per test 64 megabytes input standard input output ...

  2. Codeforces Beta Round &num;10 D&period; LCIS(DP&amp&semi;amp&semi;LCIS)

    D. LCIS time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  3. codeforces 486 E&period; LIS of Sequence(dp)

    题目链接:http://codeforces.com/contest/486/problem/E 题意:给出n个数,如果一个数满足不属于最长递增序列,那么输出1,如果属于最长递增序列但是不属于所有最长 ...

  4. Codeforces Beta Round &num;96 &lpar;Div&period; 2&rpar; (A-E)

    写份DIV2的完整题解 A 判断下HQ9有没有出现过 #include <iostream> #include<cstdio> #include<cstring> ...

  5. Codeforces Beta Round &num;7--D&period; Palindrome Degree(Manacer)

    题目:http://blog.csdn.net/winddreams/article/details/44218961 求出每一个点为中心的最长字符串,推断该串是不是从开头的回文串. #include ...

  6. Codeforces Beta Round &num;13 E&period; Holes 分块暴力

    E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...

  7. Codeforces Global Round 1 - D&period; Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  8. Codeforces Beta Round &num;17 C&period; Balance (字符串计数 dp)

    C. Balance time limit per test 3 seconds memory limit per test 128 megabytes input standard input ou ...

  9. Educational Codeforces Round 51 D&period; Bicolorings(dp)

    https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...

随机推荐

  1. 如何用 SQL Tuning Advisor &lpar;STA&rpar; 优化SQL语句

    在Oracle10g之前,优化SQL是个比较费力的技术活,不停的分析执行计划,加hint,分析统计信息等等.在10g中,Oracle推出了自己的SQL优化辅助工具: SQL优化器(SQL Tuning ...

  2. 自顶而下设计FPGA

    对IC设计而言,FPGA设计层级大致包括:系统级和行为级,RTL级,门级和晶体管级.然而更普遍的情况,FPGA只是用作实时数据采集控制.某些快速处理算法.PCIe\DDR3等高速数据通道,甚至某些简单 ...

  3. WinRAR安装、破解与去除弹窗广告

    本教程教大家怎么破解WinRAR压缩软件和去除广告(教程属于总结类,总结网上给出的方法,并且亲测有效,非本人原创)WinRAR5.40 2016.10.06 首先下载WinRAR软件,去它的官方英文网 ...

  4. webrtc学习&lpar;二&rpar;&colon; audio&lowbar;device之opensles

    audio_device是webrtc的音频设备模块.  封装了各个平台的音频设备相关的代码 audio device 在android下封装了两套音频代码. 1. 通过jni调用java的media ...

  5. asp&period;net 实现对xml文件的 读取&comma;添加&comma;删除&comma;修改

    用于修改站内xml文件 已知有一个XML文件(bookstore.xml)如下:<?xml version="1.0" encoding="gb2312" ...

  6. thinkphp增删改查

    添加数据: (添加单行数据) // 添加成功返回1,失败返回0 (添加多行数据) // 返回添加数据的条数 删除数据: 修改数据: (修改单个字段) (修改多个字段) // 修改成功返回1,失败返回0 ...

  7. 机器学习——kNN(1)基本原理

    =================================版权声明================================= 版权声明:原创文章 禁止转载  请通过右侧公告中的“联系邮 ...

  8. jenkins:一键回滚站点集群

    最近在学习jenkins过程中整理了大量资料,都收录在<jenkins自动化工具使用教程>,但依然缺少一些具体实现细节. 这篇文章,介绍jenkins做集群回滚时的两个设计方案,让一键回滚 ...

  9. 使用Spring表达式语言进行装备--SpEL

    本文主要想记录最近的两个使用spring框架实现通过配置文件装备Bean,以及使用SpEL装备Bean. 1.使用配置文件装备Bean: 当我们写某些Bean的时候是希望这个Bean当中的属性是可以通 ...

  10. 一个关于finally和return的面试题

    public class Test{ public int add(int a,int b){ try { return a+b; } catch (Exception e) { System.out ...