【动态规划】XMU 1583 Sequence
题目链接:
http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1583
题目大意:
T组数据,对于n(n<=6000)给定序列Xn(Xn<=20000),求一个非递减序列Y1, Y2, ..., Yn,使得绝对值差的和|X1-Y1|+|X2-Y2|+...|Xn-Yn|最小,并输出字典序最小的序列Y。
题目思路:
【动态规划】
首先序列Y的数字肯定和X中的某一个数字相等,不然可以通过平移得到相等或更优的解。证明不大会证。
用num[i]表示第i小的数的位置。f[i][j]表示前i个数,Yi 不大于第j小的X的最优值。要么当前取第j小的X,要么不取。
//
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define eps 1e-8
#define J 10
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define mod 1000000001
#define N 6004
using namespace std;
int n,m,lll,ans,cas;
int a[N],num[N];
int f[N][N],fa[N][N];
bool cmp(int aa,int bb)
{
return a[aa]<a[bb];
}
void print(int i,int j)
{
if(i)print(i-,fa[i][j]);
printf("%d ",a[num[j]]);
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k,l;
// while(~scanf("%s",s1))
// while(~scanf("%d",&n))
for(scanf("%d",&cas);cas;cas--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
num[i]=i;
}
sort(num+,num++n,cmp);
memset(f,0x7f,sizeof(f));
for(i=;i<=n;i++)f[][i]=;
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
f[i][j]=f[i][j-];
fa[i][j]=fa[i][j-];
if(f[i][j]>f[i-][j]+abs(a[i]-a[num[j]]))
{
f[i][j]=f[i-][j]+abs(a[i]-a[num[j]]);
fa[i][j]=j;
}
}
}
print(n-,fa[n][n]);
puts("");
}
return ;
} /*
// //
*/
【动态规划】XMU 1583 Sequence的更多相关文章
- 【最长下降子序列】【动态规划】【二分】XMU 1041 Sequence
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1041 题目大意: 一个二维平面,上面n(n<=1 000 000)个点.问至少选 ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- 【动态规划】POJ 1161 & ZOJ1463 & XMU 1033 Brackets sequence
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1033 http://poj.org/problem?id=1141 ZOJ目前挂了. ...
- 【动态规划】XMU 1032 装配线问题
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1032 题目大意: 一个物品在2条生产线上加工,每条线上n(n<=1000)个节点 ...
- 【动态规划】HDU 1081 & XMU 1031 To the Max
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1081 http://acm.xmu.edu.cn/JudgeOnline/problem.php?i ...
- 【动态规划】XMU 1030 苦恼的月下老人
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1030 题目大意: 给定两个字符串的长度和内容,求最长公共子序列. 题目思路: [动态规 ...
- 【动态规划】XMU 1029 矩阵链乘法
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1029 题目大意: 题同乘法难题.给n+1个数,头尾不能动,中间的数可取出,取出时代价是 ...
- 【动态规划】XMU 1028 Game Boy Advance
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1028 题目大意: 求01背包最优解的方案.物件数和物件编号. 题目思路: [动态规划] ...
随机推荐
- [Javascript] String method: endsWith() && startsWith()
With endsWith && startsWith, you can easily find out whether the string ends or starts with ...
- buffer cache chain 图
http://orabase.org/index.php/author/djeday84/page/7/
- LVS图解 ---阿里
LVS在大规模网络环境中的应用 1. SLB总体架构 LVS本身是开源的,我们对它进行了多方面的改进,并且也已开源-https://github.com/alibaba/LVS. 接下 ...
- Qt 学习之路 :Qt 绘制系统简介
Qt 的绘图系统允许使用相同的 API 在屏幕和其它打印设备上进行绘制.整个绘图系统基于QPainter,QPainterDevice和QPaintEngine三个类. QPainter用来执行绘制的 ...
- Macos Coco2d-x Android开发
1. 在装好环境 2. cocos new [-h] [-p PACKAGE_NAME] -l {cpp,lua,js} [-d DIRECTORY] [-t TEMPLATE_NAME] [--io ...
- Java 图片与byte数组互相转换
//图片到byte数组 public byte[] image2byte(String path){ byte[] data = null; FileImageInputStream input = ...
- 弹出层easydialog-v2.0
地址:http://www.lanrentuku.com/down/js/qita-862/ easydialog.css ;;; } .easyDialog_wrapper{ width:320px ...
- android handler机制简单介绍
我们需要了解4个类: handler:处理者,用于发送和接收信息 massage:消息.里面可以存储消息的许多信息 looper:循环泵,用于循环取出消息队列中的消息 MessageQueue(一般不 ...
- get the text value of a selected option.
<select id="myselect"> <option value="1">Mr</option> <optio ...
- POJ 2411.Mondriaan's Dream 解题报告
题意: 给出n*m (1≤n.m≤11)的方格棋盘,用1*2的长方形骨牌不重叠地覆盖这个棋盘,求覆盖满的方案数. Solution: 位运算+状态压缩+dp ...