Gym - 101611D Decoding of Varints(边界值处理)
Decoding of Varints
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
5
0 194 31 195 31
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(边界值处理)的更多相关文章
- Gym - 101611D Decoding of Varints(阅读理解题 )
Decoding of Varints 题意&思路: 首先根据红色边框部分的公式算出x,再有绿色部分得知,如果x是偶数则直接除以2,x是奇数则(x+1)/-2. PS:这题有数据会爆掉un ...
- 2017-2018 ACM-ICPC, NEERC, Moscow Subregional Contest
A. Advertising Strategy 最优策略一定是第一天用$y$元,最后一天再用$x-y$元补满. 枚举所有可能的$y$,然后模拟即可,天数为$O(\log n)$级别. 时间复杂度$O( ...
- go语言标准库 时刻更新
Packages Standard library Other packages Sub-repositories Community Standard library ▾ Name Synops ...
- 08 Packages 包
Packages Standard library Other packages Sub-repositories Community Standard library Name Synopsis ...
- Codeforces Gym 100002 D"Decoding Task" 数学
Problem D"Decoding Task" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- varints
Protocol Buffer技术详解(数据编码) - Stephen_Liu - 博客园 https://www.cnblogs.com/stephen-liu74/archive/2013/01/ ...
- Block Markov Coding & Decoding
Block Markov coding在一系列block上进行.在除了第一个和最后一个block上,都发送一个新消息.但是,每个block上发送的码字不仅取决于新的信息,也跟之前的一个或多个block ...
- 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 ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
随机推荐
- 一起来学linux:进程
简单来说,每当执行一个程序或者命令,启动事件的时候都会得到一个PID,也就是进程ID.比如用户登陆的时候就会得到一个PID.如下所示.两个用户zhf和root在登陆后分别获得PID 3212和3214 ...
- Linux入门基础(一)——ls、mkdir命令
- 关于left join遇到where就不管用的问题
今天做了个存储过程,需要的功能是查询所有人的得分,有人没分就给零分,显而易见这里用左外连接是没有问题的, 但是在连接完之后有个根据时间筛选功能,于是我加了where条件判断,这时候没有想到的事情发生了 ...
- 如何使用ipv6
需要系统至少是Vista以上还有就是要问你们学校是否已经支持IPV6 从Windows Vista开始,IPv6在默认状态下已经安装并启用,无需额外配置.检测步骤开启浏览器窗口,输入以下域名访问本站首 ...
- java.util.ResourceBundle国际化用法详解
java.util.ResourceBundle国际化用法详解 初识国际化和ResourceBundle 这个类主要用来解决国际化和本地化问题.国际化和本地化可不是两个概念,两者都是一起出现的.可以说 ...
- 简单学习github代码托管
之前尝试使用阿里云code做代码托管 egret+git+阿里云code搭建团队开发 ,现在来学习一下使用 Github做代码托管服务. 总体上看使用的步骤差不多,都需要使用GIT客户端来进行相关的操 ...
- python输出shell命令执行结果
import os,subprocess p = subprocess.Popen("df -h", shell=True, stdout=subprocess.PIPE) out ...
- 关于for 循环里 线程执行顺序问题
最近在做项目时遇到了 这样的需求 要在一个for循环里执行下载的操作, 而且要等 下载完每个 再去接着走循环.上网查了一些 觉得说的不是很明确.现在把我用到的代码 贴上 希望可以帮到有此需求的开发者 ...
- RQNOJ 671 纯洁的买卖:无限背包
题目链接:https://www.rqnoj.cn/problem/671 题意: ALEJ要通过倒卖东西来赚钱. 现在他有m元经费. 有n种物品供他选择,每种物品数量无限. 第i件物品的买入价为c[ ...
- 搭建LoadRunner中的场景(三)场景的执行计划
所谓场景操作,包括初始化用户组.启动用户组各用户以及停止虚拟用户的全过程.依据设置不同,执行过程中可以最多有5类操作,分别是启动用户组(start group).初始化(Initialize).启动虚 ...