Sicily 1497. Wavelet Compression

1497. Wavelet Compression

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

The discrete wavelet transform is a popular tool for signal compression. In this problem, your job is to write a program to decompress a one-dimensional signal (a list of integers) that has been compressed by a simple wavelet transform.

To understand how this simple wavelet transform works, suppose that we have a list of an even number of integers. We compute the sum and difference of each pair of consecutive samples, resulting in two lists of sums and differences each having half the original length. Formally, if the original samples are

a(1),..., a(n)
the i-th sum  s(i)  and difference  d(i)  are computed as:
for i = 1,...,n/2:

s(i) = a(2*i-1) + a(2*i)
d(i) = a(2*i-1) - a(2*i)
This is then rearranged to give the transformed signal by first listing the sums and then the differences. For example, if the input signal is:
  5, 2, 3, 2, 5, 7, 9, 6
Then the sum and difference signals are:
   s(i) = 7, 5, 12, 15

d(i) = 3, 1, -2, 3
Thus, the transformed signal is:
  7, 5, 12, 15, 3, 1, -2, 3

 

The same process is applied recursively to the first half of the transformed signal, treating s(i) as the input signal, until the length of the input signal is 1. In the example above, the final transformed signal is:

   39, -15, 2, -3, 3, 1, -2, 3
It is assumed that the length of the original input is a power of 2, and the input signal consists of integers between 0 and 255 (inclusive) only.

Input

The input consists of a number of cases. Each case is specified on a line, starting with an integer N (1 <= N <= 256) indicating the number of samples. The next N integers are the transformed samples. The end of input is indicated by a case in which N = 0.

Output

For each test case, output the original samples on a single line, separated by a single space.

Sample Input

8 39 -15 2 -3 3 1 -2 3
4 10 -4 -1 -1
0

Sample Output

5 2 3 2 5 7 9 6
1 2 3 4

Problem Source

Rocky 2007

感觉像是一道倒推的模拟方程吧。。

0.2s:

// Problem#: 1497
// Submission#: 2796248
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>

int a[260];

void trans(int times) {
    int temp[2 * times + 1];
    for (int i = 1; i <= times; i++) {
        temp[2 * i - 1] = (a[i] + a[i + times]) / 2;
        temp[2 * i] = (a[i] - a[i + times]) / 2;
    }
    for (int i = 1; i <= 2 * times; i++) {
        a[i] = temp[i];
    }
}

int main() {
    int n;
    while (scanf("%d", &n) && n) {
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        int times = 1;
        while (times <= n / 2) {
            trans(times);
            times *= 2;
        }
        for (int i = 1; i <= n; i++) {
            if (i != 1)
                printf(" ");
            printf("%d", a[i]);
        }
        printf("\n");
    }
    return 0;
}                 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值