205. Quantization Problem

time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard
output: standard

When entering some analog data into a computer, this information must be quantized. Quantization transforms each measured value x to some other value l(x) selected from the predefined set L of levels. Sometimes to reduce the influence of the levels set to the information, the group of levels sets Li is used. The number of levels sets is usually chosen to be the power of 2.

When using the number of levels sets, some additional information should be used to specify which set was used for each quantization. However, providing this information may be too expensive — the better solution would be to choose more levels and use one set. To avoid the specification of the quantization set, the following technique is used. Suppose that n values \(x_1, x_2, ..., x_n\) are to be quantized and the group of \(m=2^p\) levels sets \(Li, i=0, ..., m-1\); each of size \(s=2^q\) is used to quantize it. After quantization \(x_j\) is replaced with some number \(l_j = in L_{f(j)}\). Instead of sending \(l_j\), its ordinal number in \(L_{f(j)}\) is usually sent, let \(k_j\) be the ordinal number of \(l_j\) in \(L_{f(j)}\) (levels are numbered starting with 0). Take p least significant bits of \(k_j\) and say that the number \(k_j\ \&\ (2^p-1)\) is the number of the levels set that will be used for next quantization, that is \(f(j+1) = k_j\ \&\ (2^p-1)\).

Since the least significant bits of kj are usually distributed quite randomly, the sets used for quantization change often and weakly depend on values of quantized data, thus this technique provides the good way to perform the quantization.

Usually to perform the quantization the closest to the value level of the levels set is chosen. However, using the technique described, sometimes it pays off to choose not the optimal level, but some other one, the ordinal number of which has other least significant bits, thus choosing another levels set for next measure and providing better approximation of quantized values in the future. Let us call the deviation of quantization the value of \(\sum_{j=1}^{n}|xj - lj|\). Your task is given measures and levels sets to choose quantized value for each measure in such a way, that the deviation of quantization is minimal possible.

The first value is always quantized using set L0.

Input

The first line of the input file contains n (1 ≤ n ≤ 1000). The second line contains n integer numbers xi ranging from 1 to 106. The next line contains m and s \((1 ≤ m ≤ 128, m ≤ s ≤ 128)\). Next m lines contain s integer numbers each — levels of the quantization sets given in increasing order for each set, all levels satisfy \(1 ≤ level ≤ 10^6\).

Output

First output the minimal possible deviation of the quantization. Then output n integer numbers in range from 0 to s - 1. For each input value output the number of the level in the corresponding levels set \((k_j)\) used for this number to achieve the quantization required.

Sample test(s)

Input

3
8 8 19
2 4
5 10 15 20
3 7 13 17

Output

5
1 1 3

题意

给出一列长n的数列\(\{S_i\}\)和一个\(m\times s\)的矩阵\(A\)(\(m\le s\)且\(m,s\)都是2的整数次方)。求一列数\(\{K_i\}\),使\(K_0=0\),且 \(\sum_{i=1}^{n}{|A_{K_{i-1} \mod m, K_i}-S_i|}\)最小。

引用与修改自Amber——SGU 提示


看懂题了就是sb题了。

由于在阶段i时对答案产生影响的只有\(K_i\)和\(K_{i-1}\)的取值,显然可以DP做。不妨用f[i][j]表示考虑到第i个数字\(K_i\)取j时的最小值,转移是显然的:f[i][j]=min(f[i-1][k]+A[k%m][j]-S[i]),注意记录路径,然后没了。。就是题意难懂啊。

#include <bits/stdc++.h>
#define rep(_i, _j) for(int _i = 0; _i < _j; ++_i)
const int inf = 0x3f3f3f3f;
typedef long long LL;
typedef double DB;
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 10;
const int maxm = 128 + 2; int n, m, s, A[maxm][maxm], f[maxn][maxm], pre[maxn][maxm], S[maxn]; int main() {
#ifndef ONLINE_JUDGE
freopen("205.in", "r", stdin); freopen("205.out", "w", stdout);
#endif scanf("%d", &n);
for(int i = 1; i <= n; scanf("%d", &S[i]), ++i); // 大家不要学我这样写。。。
scanf("%d%d", &m, &s);
rep(i, m) rep(j, s) scanf("%d", &A[i][j]);
memset(f, INF, sizeof f);
f[0][0] = 0;
for(int i = 1; i <= n; ++i) {
for(int j = 0; j < s; ++j) {
if(i == 1) {
f[i][j] = min(f[i][j], f[i - 1][0] + abs(A[0][j] - S[i]));
pre[i][j] = 0;
continue;
}
for(int k = 0; k < s; ++k) {
int tmp = f[i - 1][k] + abs(A[k % m][j] - S[i]);
if(tmp < f[i][j]) {
pre[i][j] = k;
f[i][j] = tmp;
}
}
}
}
int ans = INF, rec;
for(int i = 0; i < s; ++i) {
if(f[n][i] < ans) {
ans = f[n][i];
rec = i;
}
}
printf("%d\n", ans);
vector<int> res;
int i = n;
while(i != 0) {
res.push_back(rec);
rec = pre[i--][rec];
}
printf("%d", res[res.size() - 1]);
for(int i = res.size() - 2; 0 <= i; --i) {
printf(" %d", res[i]);
}
puts(""); return 0;
}

SGU 205. Quantization Problem的更多相关文章

  1. 找规律 SGU 107 987654321 problem

    题目地址:http://acm.sgu.ru/problem.php?contest=0&problem=107 /* 题意:n位数的平方的后面几位为987654321的个数 尼玛,我看描述这 ...

  2. acdream 1222 Quantization Problem [dp]

    称号:acdream 1222 Quantization Problem 题意:给出一个序列 a ,然后给出一个 n * m 的矩阵,让你从这个矩阵中选出一个序列k,使得sum(abs(ki - ai ...

  3. SGU 107 987654321 problem【找规律】

    题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=107 题意: 平方后几位为987654321的n位数有多少个 分析: 虽然说是水题 ...

  4. SGU 403 Scientific Problem

    403. Scientific Problem Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: stan ...

  5. 数论 - SGU 107 987654321 problem

    987654321 problem Problem's Link Mean: 略 analyse: 这道题目是道简单题. 不过的确要好好想一下: 通过简单的搜索可以知道,在N<9时答案一定为0, ...

  6. SGU - 403 - Scientific Problem (水)

    403. Scientific Problem Time limit per test: 0.25 second(s) Memory limit: 65536 kilobytes input: sta ...

  7. sgu 107 987654321 problem

    其实挺水的,因为两个数平方,只有固定的后面几位数会影响到最后结果的后面几位数.也就是说,如果想在平方之后尾数为987654321,那么就有固定的几个尾数在平方后会是这个数,打个表,发现 10^8 内 ...

  8. 数论 - SGU 105 DIV3

    SGU 105-DIV 3 Problem's Link Mean: 定义这样一种数列:1,12,123.. 给出一个n,求这个数列中能被3整除的数的个数. analyse: 这道题可以用分析的方法解 ...

  9. SGU 乱搞日志

    SGU 100 A+B :太神不会 SGU 101 Domino: 题目大意:有N张骨牌,两张骨牌有两面有0到6的数字,能相连当且仅当前后数字相同,问能否有将N张骨牌连接的方案?思路:裸的欧拉回路,注 ...

随机推荐

  1. Codeforces Round #415 (Div. 2) A B C 暴力 sort 规律

    A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  2. web项目中解决post乱码和get乱码的方法

    前提复习编码问题产生的原因: 1.  什么是URL编码. URL编码是一种浏览器用来打包表单输入的格式,浏览器从表单中获取所有的name和其对应的value,将他们以name/value编码方式作为U ...

  3. protobuf手册

    1. c++快速上手 https://developers.google.com/protocol-buffers/docs/cpptutorial 2. c++使用手册 https://develo ...

  4. 手脱ASPack v2.12

    1.PEID查壳提示为: ASPack 2.12 -> Alexey Solodovnikov 2.载入OD,程序入口点是一个pushad,在他的下一行就可以进行ESP定律,下硬件访问断点然后s ...

  5. URL参数带加号“+”AJAX传值失败的解决方法

    URL中参数的值有加号,虽然请求的URL中含有加号,但是GET的时候却得不到加号! 解决办法,用JavaScript的encodeURIComponent函数对加号进行编码. 如str="a ...

  6. 字符串:KMP

    KMP是字符串匹配的经典算法 也是众多字符串基础的重中之重 A. 题意:给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出最先匹配完成的位置,否则输出-1. 做法:直 ...

  7. HDU 1930 CRT

    也是很模板的一道题,给出一些数,分割,模数固定是4个互质的. /** @Date : 2017-09-16 23:54:51 * @FileName: HDU 1930 CRT.cpp * @Plat ...

  8. Linux rpm yum 等安装软件

    任何程序都是先写代码,拿到源码去编译得到一个目标程序. 1  编译的过程复杂有需要准备编译的环境,和硬件有关,32位64位,内核的不同等等所以需要编译多次     Java特殊但是他需要安装jvm, ...

  9. python 文字转语音包pyttsx安装出错解决方法

    pyttsx的python的文字转语音的包,但是pyttsx的官方网站上资源只更新2012年,所以在py3中使用pip install pyttsx或者下载安装包进行安装时,虽然可以安装成功,但是im ...

  10. 脱离MVC使用Razor模板引擎

    关于Razor模板引擎 1.简介 模板引擎:Razor.Nveocity.Vtemplate.Razor有VS自动提示.使用起来会方便一点. 但是Razor大多是在MVC下使用的. 那么如何在非MVC ...