Gym - 101611D Decoding of Varints(邊界值處理)
Decoding of Varints
StatementsVarint 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 = a
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 b
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.
InputThe 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.
OutputPrint the decoded sequence of integers.
Example Input5Output
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 = 10005; ll mi[65]; ll a[MAX]; void init(){ mi[0]=1; for(ll i=1;i<=63;i++){ mi[i]=mi[i-1]*2; } } int main(void) { int t,i,j; ll n,x; init(); scanf("%I64u",&n); for(i=1;i<=n;i++){ scanf("%I64u",&a[i]); } ll ans=0;int l=0; for(i=1;i<=n;i++){ if(a[i]<128){ ans+=a[i]*mi[l*7]; if(ans&1) printf("-%I64u\n",ans/2+1); else printf("%I64u\n",ans/2); ans=0;l=0; continue; } ans+=(a[i]-128)*mi[l*7]; l++; } return 0; }
Gym - 101611D Decoding of Varints(邊界值處理)