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 1039:Zero-complexity Transposition(逆置) (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3093 解决:1255 题目描述: You are given a sequence of integer numbers. Zero-co ...

  2. Webpack探索【15】--- 基础构建原理详解(模块如何被组建&如何加载)&源码解读

    本文主要说明Webpack模块构建和加载的原理,对构建后的源码进行分析. 一 说明 本文以一个简单的示例,通过对构建好的bundle.js源码进行分析,说明Webpack的基础构建原理. 本文使用的W ...

  3. 【题解】CF989C A Mist of Florescence

    [题解]CF989C A Mist of Florescence 题目大意: 让你构造一个\(n∗m\)矩阵,这个矩阵由4种字符填充构成,给定4个整数,即矩阵中每种字符构成的四联通块个数,\(n,m\ ...

  4. 利用wxpython显示OpenCV图像

    核心代码 import wx, cv2 import numpy as np # Start with a numpy array style image I'll call "source ...

  5. Java for LeetCode 115 Distinct Subsequences【HARD】

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  6. POJ - 3278 Catch That Cow 【BFS】

    题目链接 http://poj.org/problem?id=3278 题意 给出两个数字 N K 每次 都可以用三个操作 + 1 - 1 * 2 求 最少的操作次数 使得 N 变成 K 思路 BFS ...

  7. Algorithm: bit manipulation

    1. 一个数的从右起第p1位和第p2位swap n位 unsigned int swapBits(unsigned int x, unsigned int p1, unsigned int p2, u ...

  8. react服务端渲染

    一.服务端渲染的好处 1.SEO, 让搜索引擎更容易读取页面内容: 2.首屏渲染速度更快(重点),无需等待JS文件下载执行过程: 3.更易于维护,服务端和客户端可以共享某些代码: 二.实现原理 服务端 ...

  9. HDU 4539 郑厂长系列故事——排兵布阵 —— 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Ot ...

  10. ActiveMQ之点对点使用

    package com.toov5.producer; import javax.jms.Connection; import javax.jms.JMSException; import javax ...