EOJ 1918 Wavelet Compression

2 篇文章 0 订阅

Time limit per test: 5.0 seconds
Memory limit: 256 megabytes

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
a1,…,an
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.
Examples

Input

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

Output

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


其实用C超级简单啊,fwq大佬代码20行不到,但是思路是一样的
就当作菜鸡Cpp练手了


#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int x,v_size,cnt,k,tmp,i;
    cin>>v_size;
    cnt=v_size;
    while(v_size!=0)
    {
        k=1;
        cnt=v_size;
        vector<int> v;
/***********************************************************/
        while(cnt--)
        {
            cin>>x;
            v.push_back(x);
        }
        vector<int>::iterator it=v.begin();//got an iterator!
/***********************************************************/
        while(k!=v_size)
        {
            tmp=k;
            while(tmp)
            {
                v.push_back((*(it+k-tmp)+*(it+k+k-tmp))/2);
                it=v.begin();
                v.push_back((*(it+k-tmp)-*(it+k+k-tmp))/2);
                it=v.begin();
                tmp--;
            }
            //then erase !!//forgot to insert
            tmp=k*2;
            if(k!=v_size/2)
            {
                for(tmp;tmp<v_size;tmp++)
                {
                    v.push_back(*(it+tmp));
                    it=v.begin();
                }
            }
            v.erase(it,it+v_size);
            it=v.begin();
            k=k*2;
        }
        for(i=0;i<v_size;i++) cout<<v[i]<<" ";
        cout<<endl;

    cin>>v_size;
    }
    return 0;
}

刚学会一点vector操作的依葫芦画瓢。
因为担心野指针所以不停初始化,还不知道哪些是没有必要的。
v.end()返回的是指向最后元素的后一个元素的迭代器,而v.back()才是返回指向最后一个元素的迭代器。
insert和erase都是左闭右开的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值