Decoding of Varints

Statements

Varint is a type used to serializing integers using one or more bytes. The key idea is to have smaller values being encoded with a smaller number of bytes.

First, we would like to encode some unsigned integer x. Consider its binary representation x = a0a1a2... ak - 1, where ai-th stands for the i-th significant bit, i.e. x = a0·20 + a1·21 + ... + ak - 1·2k - 1, while k - 1 stands for the index of the most significant bit set to 1 or k = 1 if x = 0.

To encode x we will use bytes b0, b1, ..., bm - 1. That means one byte for integers from 0 to 127, two bytes for integers from 128 to 214 - 1 = 16383 and so on, up to ten bytes for 264 - 1. For bytes b0, b1, ..., bm - 2 the most significant bit is set to 1, while for byte bm - 1 it is set to 0. Then, for each i from 0 to k - 1, i mod 7 bit of byte is set to ai. Thus,

x = (b0 - 128)·20 + (b1 - 128)·27 + (b2 - 128)·214 + ... + (bm - 2 - 128)·27·(m - 2) + bm - 1·27·(m - 1)

In the formula above we subtract 128 from b0, b1, ..., bm - 2 because their most significant bit was set to 1.

For example, integer 7 will be represented as a single byte b0 = 7, while integer 260 is represented as two bytes b0 = 132 and b1 = 2.

To represent signed integers we introduce ZigZag encoding. As we want integers of small magnitude to have short representation we map signed integers to unsigned integers as follows. Integer 0 is mapped to 0,  - 1 to 1, 1 to 2,  - 2 to 3, 2 to 4,  - 3 to 5, 3 to 6 and so on, hence the name of the encoding. Formally, if x ≥ 0, it is mapped to 2x, while if x < 0, it is mapped to  - 2x - 1.

For example, integer 75 is mapped to 150 and is encoded as b0 = 150, b1 = 1, while  - 75 will be mapped to 149 and will be encoded as b0 = 149, b1 = 1. In this problem we only consider such encoding for integers from  - 263 to 263 - 1 inclusive.

You are given a sequence of bytes that corresponds to a sequence of signed integers encoded as varints. Your goal is to decode and print the original sequence.

Input

The first line of the input contains one integer n (1 ≤ n ≤ 10 000) — the length of the encoded sequence. The next line contains n integers from 0 to 255. You may assume that the input is correct, i.e. there exists a sequence of integers from  - 263 to 263 - 1 that is encoded as a sequence of bytes given in the input.

Output

Print the decoded sequence of integers.

Example

Input
5
0 194 31 195 31
Output
0
2017
-2018 首先需要用unsigned long long,除2前值为long long的两倍。
再一个就是先除2再加1,避免先加后值越界。
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int MAX = ; ll mi[];
ll a[MAX]; void init(){
mi[]=;
for(ll i=;i<=;i++){
mi[i]=mi[i-]*;
}
}
int main(void)
{
int t,i,j;
ll n,x;
init();
scanf("%I64u",&n);
for(i=;i<=n;i++){
scanf("%I64u",&a[i]);
}
ll ans=;int l=;
for(i=;i<=n;i++){
if(a[i]<){
ans+=a[i]*mi[l*];
if(ans&) printf("-%I64u\n",ans/+);
else printf("%I64u\n",ans/); ans=;l=;
continue;
}
ans+=(a[i]-)*mi[l*];
l++;
}
return ;
}

Gym - 101611D Decoding of Varints(边界值处理)的更多相关文章

  1. Gym - 101611D Decoding of Varints(阅读理解题 )

    Decoding of Varints ​ 题意&思路: 首先根据红色边框部分的公式算出x,再有绿色部分得知,如果x是偶数则直接除以2,x是奇数则(x+1)/-2. PS:这题有数据会爆掉un ...

  2. 2017-2018 ACM-ICPC, NEERC, Moscow Subregional Contest

    A. Advertising Strategy 最优策略一定是第一天用$y$元,最后一天再用$x-y$元补满. 枚举所有可能的$y$,然后模拟即可,天数为$O(\log n)$级别. 时间复杂度$O( ...

  3. go语言标准库 时刻更新

    Packages   Standard library Other packages Sub-repositories Community Standard library ▾ Name Synops ...

  4. 08 Packages 包

    Packages   Standard library Other packages Sub-repositories Community Standard library Name Synopsis ...

  5. Codeforces Gym 100002 D"Decoding Task" 数学

    Problem D"Decoding Task" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  6. varints

    Protocol Buffer技术详解(数据编码) - Stephen_Liu - 博客园 https://www.cnblogs.com/stephen-liu74/archive/2013/01/ ...

  7. Block Markov Coding & Decoding

    Block Markov coding在一系列block上进行.在除了第一个和最后一个block上,都发送一个新消息.但是,每个block上发送的码字不仅取决于新的信息,也跟之前的一个或多个block ...

  8. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  9. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

随机推荐

  1. 九度OJ 1149:子串计算 (计数、排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1026 解决:571 题目描述: 给出一个01字符串(长度不超过100),求其每一个子串出现的次数. 输入: 输入包含多行,每行一个字符串. ...

  2. ios Symbol(s) not found for architecture arm64总结 含隐藏错误cocoapods

    一.通用 报错:Desktop/project/ASDF/WEIXIN/libWeChatSDK.a (3 slices) Undefinedsymbols for architecture arm6 ...

  3. 我的Java开发学习之旅------>计算从1到N中1的出现次数的效率优化问题

    有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数.比如f(1)=1:f(13)=6,问一个最大的能满足f(n)=n中的n是什么? 例如:f(13)=6, 因为1, ...

  4. VMware和Centos系统安装

    1.Linux发行版的选择 2.vmware创建一个虚拟机(centos) 3.安装配置centos7 4.xshell配置连接虚拟机(centos) 选择性 pc可以选择 -纯系统 Linux/wi ...

  5. 关于RabbitMQ简介

    RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息通讯的世界里有很多公开标准 ...

  6. python类初探

    class human: is_alive=True is_man=True def __init__(self,age): print('this is __init__() method, whi ...

  7. JavaScript--Object类

    Object类是所有JavaScript类的基类,提供了一种创建自定义对象的简单方式,不需要程序员再定义构造函数. 主要属性: constructor-对象的构造函数 prototype-获得类的pr ...

  8. 淘宝双十一页面(Flexible)

    以下demo点我下载 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  9. HBASE---shangxueT

  10. [CTSC 2012] Cheat

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2806 [算法] 首先建立广义后缀自动机 注意到问题具有单调性 , 不妨对于每组询问二 ...