ZOJ 2561 Order-Preserving Codes
Order-Preserving Codes
This problem will be judged on ZJU. Original ID: 2561
64-bit integer IO format: %lld Java class name: Main
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
#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 ;
}
ZOJ 2561 Order-Preserving Codes的更多相关文章
- table2excel使用
原table2excel代码 /* * 采用jquery模板插件——jQuery Boilerplate * * Made by QuJun * 2017/01/10 */ //table2excel ...
- Cassandra 数据模型设计,根据你的查询来制定设计——反范式设计本质:空间换时间
转自:http://www.infoq.com/cn/articles/best-practice-of-cassandra-data-model-design 不要把Cassandra model想 ...
- Method and Apparatus for Providing Highly-Scalable Network Storage for Well-Gridded Objects
An apparatus comprising a plurality of storage nodes comprising a plurality of corresponding storage ...
- 细说 PEP 468: Preserving Keyword Argument Order
细说 PEP 468: Preserving Keyword Argument Order Python 3.6.0 版本对字典做了优化,新的字典速度更快,占用内存更少,非常神奇.从网上找了资料来看, ...
- M面经Prepare: Positive-Negative partitioning preserving order
Given an array which has n integers,it has both positive and negative integers.Now you need sort thi ...
- UVA-146 ID Codes
It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exerc ...
- System Error Codes
很明显,以下的文字来自微软MSDN 链接http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx M ...
- uva146 ID codes
Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...
- ZOJ 3494 BCD Code(AC自动机+数位DP)
BCD Code Time Limit: 5 Seconds Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...
随机推荐
- _bzoj2038 [2009国家集训队]小Z的袜子(hose)【莫队】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 裸的莫队,注意要先移动右端点再移动左端点. #include <cstdio&g ...
- Mybatis事务处理
知识点有事务处理的配置,还有事务处理的方法 事务处理的配置: mybatis的事务处理由两种方式控制,JDBC和MANAGED: MANAGED就是说事务处理由第三方的插件来完成,比如说spring ...
- 移动设备访问使用百度js跳转
以下为代码,可放置在网站foot底部文件,或者haead顶部文件,建议将代码放在网站顶部,这样可以实现手机访问立即跳转! <script src="http://siteapp.bai ...
- VM virtualBox网络地址设置
目的:在虚拟机LINUX中,可用通过主机访问到虚机内容. 问题描述,在虚机系统中,ip地址一直为127.0.0.1,无法在主机中建立连接 参考文章:https://cnzhx.net/blog/vir ...
- Node.js——req、res对象
requset对象类型<http.IncomingMessage>,继承stream.Readable类 requset对象: req.headers req.rawHeaders req ...
- 分析HTTP 2.0与HTTP 1.1区别
1.什么是HTTP 2.0 HTTP/2(超文本传输协议第2版,最初命名为HTTP 2.0),是HTTP协议的的第二个主要版本,使用于万维网.HTTP/2是HTTP协议自1999年HTTP 1.1发布 ...
- Ubuntu 下更新或下载输入法(搜狗)
ubuntu12.04的fcitx版本不支持,不满足依赖,需要更新fcitx 添加fcitx源添加fcitx源命令 : sudo add-apt-repository ppa:fcitx-team/n ...
- GA详解
转:http://blog.csdn.net/u010451580/article/details/51178225 本文是去年课题组周报中的一个专题讲解,详细讲了GA,由于是周报,所以十分详细.很适 ...
- (转)淘淘商城系列——KindEditor富文本编辑器的使用
http://blog.csdn.net/yerenyuan_pku/article/details/72809794 通过上文的学习,我们知道了怎样解决KindEditor富文本编辑器上传图片时的浏 ...
- cesium 原理 之 command拼接
VAO VAO(Vertext Array Object),中文是顶点数组对象.之前在<Buffer>一文中,我们介绍了Cesium如何创建VBO的过程,而VAO可以简单的认为是基于VBO ...