称号:acdream 1222 Quantization Problem

题意:给出一个序列 a ,然后给出一个 n * m 的矩阵,让你从这个矩阵中选出一个序列k,使得sum(abs(ki - ai))尽可能的小,首先第一个数仅仅能在矩阵的第一行选第 x 个。然后以后每一个在第 x%n 行选,依次选出最小就可以。

每一个点能够选多次、

分析:这个题目难度在于题意,题意读懂了就简单了。

非常明显的一个dp题目,我们定义状态:dp 【i】【j】 :选第 i 个数 在第 j 列的最小和

则转移方程:dp 【i】【j】 = dp [ i - 1 ] [ k ]  +  abs ( a [ i ] - mp  [ k % s ] [ j ]  ) ;   k是枚举的前一次在第k行选

然后用一个pre数组保存一下路径就ok

AC代码:

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1200;
const int M = 130;
int dp[N][M];
int mp[M][M];
int pre[N][M];
int a[N];
int main()
{
//freopen("Input.txt","r",stdin);
int n;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
int s,c;
scanf("%d%d",&s,&c);
for(int i=0; i<s; i++)
{
for(int j=0; j<c; j++)
scanf("%d",&mp[i][j]);
}
memset(dp,inf,sizeof(dp));
for(int i=0; i<c; i++)
{
int ff = abs(a[0]-mp[0][i]);
dp[0][i]=min(dp[0][i],ff);
//printf("%d ",dp[0][i]);
}
memset(pre,0,sizeof(pre));
for(int i=1; i<n; i++)
{
for(int j=0; j<c; j++)
{
for(int k=0; k<c; k++)
{
int ff = dp[i-1][k] + abs(a[i]-mp[k%s][j]); //枚举在全部行取。
if(ff<dp[i][j])
{
dp[i][j]=ff;
pre[i][j]=k;
}
}
//printf("%d ",dp[i][j]);
}
//printf("\n");
}
int ans = inf, rec;
for(int i = 0; i < c; i++)
{
if(dp[n-1][i] < ans)
{
ans = dp[n-1][i];
rec = i;
}
}
printf("%d\n", ans);
vector<int> res;
int i = n-1;
while(i != -1)
{
res.push_back(rec);
rec = pre[i--][rec];
}
for(int i = res.size() - 1; 0 <= i; --i)
{
printf("%d%c",res[i],i==0? '\n':' ');
}
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

acdream 1222 Quantization Problem [dp]的更多相关文章

  1. SGU 205. Quantization Problem

    205. Quantization Problem time limit per test: 0.25 sec. memory limit per test: 65536 KB input: stan ...

  2. UVA11069 - A Graph Problem(DP)

    UVA11069 - A Graph Problem(DP) 题目链接 题目大意:给你n个点.要你找出有多少子串符合要求.首先没有连续的数字,其次不能再往里面加入不论什么的数字而不违反第一条要求. 解 ...

  3. D. Easy Problem dp(有衔接关系的dp(类似于分类讨论) )

    D. Easy Problem dp(有衔接关系的dp(类似于分类讨论) ) 题意 给出一个串 给出删除每一个字符的代价问使得串里面没有hard的子序列需要付出的最小代价(子序列不连续也行) 思路 要 ...

  4. Codeforces 706 C. Hard problem (dp)

    题目链接:http://codeforces.com/problemset/problem/706/C 给你n个字符串,可以反转任意一个字符串,反转每个字符串都有其对应的花费ci. 经过操作后是否能满 ...

  5. Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度

    Problem D. Dinner ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1003 ...

  6. bzoj 1222: [HNOI2001]产品加工 dp

    1222: [HNOI2001]产品加工 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 381  Solved: 218[Submit][Status ...

  7. hdu 5464 Clarke and problem dp

    Clarke and problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...

  8. uva 10401 Injured Queen Problem(dp)

    题目链接:10401 - Injured Queen Problem 题目大意:给出一个字符串,要求在n * n(n为字符串的长度)的棋盘上摆放n个受伤的皇后,受伤的皇后只能攻击到同一列和它周围8个格 ...

  9. Codeforces 1096D - Easy Problem - [DP]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...

随机推荐

  1. SIX GOD

    SIX GOD是什么意思呢.?_百度知道 SIX GOD

  2. libevent简单介绍和使用

    <pre class="html" name="code">libevent接口的使用是简单easy的.关键还是一些其他技术须要深入了解.如epol ...

  3. 生成awr报告

    主要参考文献: http://343766868.blog.163.com/blog/static/48314056201110124513396/ 概况 Oracle内部以一定的频率把系统关键的统计 ...

  4. 平衡树 - 红黑树(JQuery+Js+Canvas版本的,帮助大家理解)

    红黑树 1.红黑树介绍 年写的一篇论文中获得的.它是复杂的,但它的操作有着良好的最坏情况运行时间,并且在实践中是高效的:它可以在O(log n)时间内做查找,插入和删除,这里的n是树中元素的数目. 2 ...

  5. springMVC框架搭建

    springMVC和struts一样为MVC框架,但是springMVC与spring做到无缝连接. 在搭建SpringMVC时可以在官网上下载最新的jar包. http://www.springso ...

  6. Windows 7中怎样找到真正的Administrator账户

    出于安全因素方面的考虑,默认情况下 Windows 7 的系统管理员 Administrator 账户处于禁用状态.需要使用时,开启它其实也很简单,并不需要复杂的操作. 方法一:从系统管理中启用Adm ...

  7. TWinControl.WMNCPaint对非客户的绘制

    混个脸熟: procedure TWinControl.WMNCPaint(var Message: TMessage); const InnerStyles: , BDR_SUNKENINNER, ...

  8. hadoop学习之ZooKeeper

    1. 什么是ZooKeeper? ZooKeeper是一组工具,用来配置和支持分布式调度. 它能处理分布式应用的“部分失败”问题. 什么是部分失败? 部分失败是分布式处理系统的固有特征,即发送者无法知 ...

  9. org.apache.jasper.JasperException: java.lang.ClassCastException

    异常信息: org.apache.jasper.JasperException: java.lang.ClassCastException:org.apache.catalina.util.Defau ...

  10. BUG: scheduling while atomic: events/0/4/总结

    对于Linux内核来说,Oops就意外着内核出了异常,此时会将产生异常时CPU的状态,出错的指令地址.数据地址及其他寄存器,函数调用的顺序甚至是栈里面的内容都打印出来,然后根据异常的严重程度来决定下一 ...