zoukankan      html  css  js  c++  java
  • ZOJ 2561 Order-Preserving Codes

    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
     
    解题:平行四边形优化动态规划
    $当ileq i'leq jleq j' 有 sum[i][j]+sum[i'][j']leq sum[i'][j]+sum[i][j']$
    故可以用平行四边形优化
    $s[i][j-1] leq s[i+1][j]$
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const LL INF = 0x3f3f3f3f3f3f3f3f;
     5 const int maxn = 2005;
     6 LL dp[maxn][maxn],sum[maxn];
     7 int s[maxn][maxn],n;
     8 void dfs(int x,int L,int R) {
     9     if(L >= R) {
    10         putchar('
    ');
    11         return;
    12     }
    13     if(x <= s[L][R]) {
    14         putchar('0');
    15         dfs(x,L,s[L][R]);
    16     } else {
    17         putchar('1');
    18         dfs(x,s[L][R] + 1,R);
    19     }
    20 }
    21 int main() {
    22     while(~scanf("%d",&n)) {
    23         for(int i = 1; i <= n; ++i) {
    24             scanf("%lld",sum + i);
    25             s[i][i] = i;
    26             sum[i] += sum[i-1];
    27         }
    28         for(int i = 2; i <= n; ++i) {
    29             for(int j = 1; j + i - 1 <= n; ++j) {
    30                 int k = j + i - 1;
    31                 dp[j][k] = INF;
    32                 for(int t = s[j][k-1]; t <= s[j+1][k]; ++t) {
    33                     LL tmp = dp[j][t] + dp[t+1][k] + sum[k] - sum[j-1];
    34                     if(dp[j][k] > tmp) {
    35                         dp[j][k] = tmp;
    36                         s[j][k] = t;
    37                     }
    38                 }
    39             }
    40         }
    41         for(int i = 1; i <= n; ++i) dfs(i,1,n);
    42     }
    43     return 0;
    44 }
    View Code
  • 相关阅读:
    中国移动全球通寻宝第四期攻略
    推荐一个奥运门票交流的网站!
    国务院发出通知明确2008年部分节假日放假安排
    [转]开发人员如何谈加薪,抛砖引玉,欢迎大家各抒己见。
    中级.NET开发人员应该知道些什么?
    linux系统管理 系统安全及应用
    Linux10 进程 定时任务 僵尸进程
    shell脚本练习
    虚拟机和liunx系统的装机
    Linux三剑客awk的应用对比案例详解
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4860369.html
Copyright © 2011-2022 走看看