题目:http://poj.org/problem?id=3666

dp方程可以是 d [ i ] [ j ] = min ( d [ i - 1 ] [ k ] ) + abs ( a [ i ] - j ),表示a数组前 i 个与结尾为 j 的 b 数组匹配的最优解。0<=k<=j。

可以证明 b 数组中的数都在 a 数组中出现过。详见《算法竞赛进阶指南》。

所以 j 可以表示a [ j ]。当然为了满足不降或不升,需要复制下a数组再排个序来用。

n^2是 LICS 的套路。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const long long INF=0x7fffffff;
int n;
long long a[],b[],d[][],ans=INF;
bool cmp(long long x,long long y)
{
return x>y;
}
long long as(long long k)
{
return k<?-k:k;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memcpy(b,a,sizeof a);
sort(b+,b+n+);
for(int i=;i<=n;i++)
{
int k=;
// printf("i=%d\n",i);
for(int j=;j<=n;j++)
{
// printf(" j=%lld ",b[j]);
k=d[i-][j]<d[i-][k]?j:k;
// printf("k=%lld d[i-1][k]=%lld ",b[k],d[i-1][k]);
d[i][j]=d[i-][k]+as(a[i]-b[j]);
// printf("d=%lld\n",d[i][j]);
}
}
for(int i=;i<=n;i++)
ans=min(ans,d[n][i]);
sort(b+,b+n+,cmp);
memset(d,,sizeof d);
for(int i=;i<=n;i++)
{
int k=;
// printf("i=%d\n",i);
for(int j=;j<=n;j++)
{
// printf(" j=%lld ",b[j]);
k=d[i-][j]<d[i-][k]?j:k;
// printf("k=%lld d[i-1][k]=%lld ",b[k],d[i-1][k]);
d[i][j]=d[i-][k]+as(a[i]-b[j]);
// printf("d=%lld\n",d[i][j]);
}
}
for(int i=;i<=n;i++)
ans=min(ans,d[n][i]);
printf("%lld",ans);
return ;
}

POJ3666序列最小差值的更多相关文章

  1. 【编程题目】有两个序列 a,b,大小都为 n,序列元素的值任意整数,无序;(需要回头仔细研究)

    32.(数组.规划)有两个序列 a,b,大小都为 n,序列元素的值任意整数,无序:要求:通过交换 a,b 中的元素,使[序列 a 元素的和]与[序列 b 元素的和]之间的差最小.例如: var a=[ ...

  2. LuoguP4234_最小差值生成树_LCT

    LuoguP4234_最小差值生成树_LCT 题意: 给出一个无向图,求最大的边权减最小的边权最小的一棵生成树. 分析: 可以把边权从大到小排序,然后类似魔法森林那样插入. 如果两点不连通,直接连上, ...

  3. [Swift]LeetCode908. 最小差值 I | Smallest Range I

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  4. [Swift]LeetCode910. 最小差值 II | Smallest Range II

    Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and ad ...

  5. CCF CSP 201712-1 最小差值

    题目链接:http://118.190.20.162/view.page?gpid=T68 问题描述 试题编号: 201712-1 试题名称: 最小差值 时间限制: 1.0s 内存限制: 256.0M ...

  6. 2018-计算机系机试(第二批)-D-最小差值

    单点时限: 2.0 sec 内存限制: 256 MB 输入 n 个整数,输出最小差值.最小差值指所有数之间差的绝对值的最小数. 例如:3 个整数 1,2 和 6 的最小差值是 1. 输入格式 第一个数 ...

  7. leetcode-908-最小差值 I

    题目描述: 给定一个整数数组 A,对于每个整数 A[i],我们可以选择任意 x 满足 -K <= x <= K,并将 x 加到 A[i] 中. 在此过程之后,我们得到一些数组 B. 返回  ...

  8. [luogu4234]最小差值生成树

    [luogu4234]最小差值生成树 luogu 从小到大枚举边,并连接,如果已连通就删掉路径上最小边 lct维护 \(ans=min(E_{max}-E_{min})\) #include<b ...

  9. CCF201712-1 最小差值

    试题编号: 201712-1 试题名称: 最小差值 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定n个数,请找出其中相差(差的绝对值)最小的两个数,输出它们的差值的绝对值 ...

随机推荐

  1. 42. Trapping Rain Water *HARD*

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  2. simulate mdns message

    use dns-sd command simulate mdns message: dns-sd -R test _test._tcp local 1234 a=1111111111111111111 ...

  3. learning docker steps(3) ----- docker services 初次体验

    参考:https://docs.docker.com/get-started/part3/#docker-composeyml docker 的 service样例, 我们可以理解成是一个本地负载均衡 ...

  4. CMS垃圾回收

    最近在学习JVM和GC调优,今天总结下CMS的一些特点和要点,让我们先简单的看下整个堆年轻代和年老代的垃圾收集器组合(以下配合java8完美支持,其他版本可能稍有不同),其中标红线的则是我们今天要着重 ...

  5. struts2读取request,session,application中的值

    request.----> <s:property value="#request.mySessionPropKey"/> or <s:property v ...

  6. vim configures for normal work

    " " 主要用于保留一些常用的vim配置,省得每次都另外写,效率太低. " " 2015-09-18 深圳 南山平山村 曾剑锋 " set nocom ...

  7. matlab读写视频VideoReader/VideoWriter

    前言 视频处理分析的过程中,需要用到将视频一帧帧地读取.写入,本文就涉及此问题. 系统环境 1.系统:win7_64 2.matlab版本:matlab2015a 测试代码 代码一(读视频): %To ...

  8. Executor 框架

    Java的线程既是工作单元,也是执行机制.从JDK5开始,把工作单元与执行机制分离开来.工作单元包括Runnable和Callable,而执行机制由Executor框架提供. Executor 框架简 ...

  9. c4 L3-001 找零钱 (简单01背包-输出最小字典序解(用vector保存当前最优解))

    #include <iostream> #include <algorithm> #include <vector> #include <cstdio> ...

  10. pylot压力测试工具

    博客搬家了,欢迎大家关注,https://bobjin.com 由于版本的兼容性问题,现在在windows下搭建pylot+matplotlib压力测试环境真的会令人吃尽苦头.这是因为,pylot从2 ...