Order-Preserving Codes

Time Limit: 5000ms
Memory Limit: 65536KB

This problem will be judged on ZJU. Original ID: 2561
64-bit integer IO format: %lld      Java class name: Main

Special Judge

Binary code is a mapping of characters of some alphabet to the set of finite length bit sequences. For example, standard ASCII code is a fixed length code, where each character is encoded using 8 bits.

Variable length codes are often used to compress texts taking into account the frequencies of occurence of different characters. Characters that occur more often get shorter codes, while characters occuring less often -- longer ones.

To ensure unique decoding of variable length codes so called prefix codes are usually used. In a prefix code no code sequence is a proper prefix of another sequence. Prefix code can be easily decoded scanning the encoded sequence from left to right, since no code is the prefix of another, one always knows where the code for the current character ends and the new character starts.

Among prefix codes, the optimal code is known, so called Huffman code. It provides the shortest possible length of the text among all prefix codes that separatly encode each character with an integer number of bits.

However, as many other codes, Huffman code does not preserve character order. That is, Huffman codes for lexicographically ordered characters are not necessarily lexicographicaly ordered.

In this problem you are asked to develop a prefix code that would be optimal for the given text among all order-preserving prefix codes. Code is called order-preserving if for any two characters the code sequence for the character that goes earlier in the alphabet is lexicographically smaller.

Since text itself is not essential for finding the code, only the number of occurences of each character is important, only this data is given.

Input:

The input consists of several test cases

For each test case, the first line contains n -- the number of characters in the alphabet (2 <= n <= 2000). The next line contains n integer numbers -- the number of occurences of the characters in the text for which the code must be developed (numbers are positive and do not exceed 109). Characters are described in the alphabetical order.

Output:

For each test case, Output n bit sequences, one on a line -- the optimal order-preserving prefix code for the described text.

Sample Input:

5
1 8 2 3 1

Sample Output:

00
01
10
110
111
 

Source

Author

Andrew Stankevich
 
解题:平行四边形优化动态规划
$当i\leq i'\leq j\leq j' 有 sum[i][j]+sum[i'][j']\leq sum[i'][j]+sum[i][j']$
故可以用平行四边形优化
$s[i][j-1] \leq s[i+1][j]$
 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = ;
LL dp[maxn][maxn],sum[maxn];
int s[maxn][maxn],n;
void dfs(int x,int L,int R) {
if(L >= R) {
putchar('\n');
return;
}
if(x <= s[L][R]) {
putchar('');
dfs(x,L,s[L][R]);
} else {
putchar('');
dfs(x,s[L][R] + ,R);
}
}
int main() {
while(~scanf("%d",&n)) {
for(int i = ; i <= n; ++i) {
scanf("%lld",sum + i);
s[i][i] = i;
sum[i] += sum[i-];
}
for(int i = ; i <= n; ++i) {
for(int j = ; j + i - <= n; ++j) {
int k = j + i - ;
dp[j][k] = INF;
for(int t = s[j][k-]; t <= s[j+][k]; ++t) {
LL tmp = dp[j][t] + dp[t+][k] + sum[k] - sum[j-];
if(dp[j][k] > tmp) {
dp[j][k] = tmp;
s[j][k] = t;
}
}
}
}
for(int i = ; i <= n; ++i) dfs(i,,n);
}
return ;
}

最新文章

  1. Anliven - 基础知识梳理汇总 - 软件测试
  2. Cassandra中的数据一致性
  3. 如何学习Python
  4. Apache配置默认首页
  5. Text selection in div(contenteditable) when double click
  6. Git CMD - checkout: Switch branches or restore working tree files
  7. thinkphp 比对过去时间距离现在时间多少的问题
  8. 凡客副总裁崔晓琦离职 曾负责旗下V+商城项目_科技_腾讯网
  9. SWT的TreeVierer的使用
  10. 微软最牛MS08-067漏洞各系统补丁下载地址
  11. 【转】JQuery.Ajax之错误调试帮助信息
  12. 文字编码转换器 V1.0 免费绿色版
  13. php 数据连接 基础
  14. jemter 新增sha256函数
  15. Java toBinaryString()函数探究及Math.abs(-2147483648)=-2147483648原理探究
  16. 【Codeforces 115D】Unambiguous Arithmetic Expression
  17. C/C++中static,const,inline三种关键字详细总结
  18. 安装python-devel开发包
  19. pyhon类继承
  20. torchvision 批量可视化图片

热门文章

  1. 01背包 Codeforces Round #267 (Div. 2) C. George and Job
  2. break跳出嵌套循环体
  3. (五)Mybatis总结之一对多、一对一
  4. Java多线程——线程的优先级和生命周期
  5. provider模式
  6. ubuntu服务器建立apache虚拟主机
  7. codeforces_C. Sequence Transformation
  8. Android(java)学习笔记197:ContentProvider使用之内容观察者02
  9. JVM中常见的垃圾收集器
  10. JS日期,金钱处理