#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<int> VI; const int INF = 1000000009; const LL LINF = 1000000000000000009LL; #define FOR(i, b, e) for(int i = b; i <= e; ++i) #define FORD(i, b, e) for(int i = b; i >= e; --i) #define REP(i, n) FOR(i, 0, n-1) #define REV(i, n) FORD(i, n-1, 0) #define PB push_back #define PP pop_back #define MP make_pair #define ST first #define ND second #define SIZE(c) (int)(c).size() #define ALL(c) (c).begin(), (c).end() #define DEBUG(s) s const int N = 200005; const int L = 16; int n; int tab[N]; LL wyn; const int M = 10; LL pot[20]; void preproc() { pot[0] = 1; FOR(i, 1, 18) pot[i] = 10ll*pot[i-1]; //~ FOR(i, 0, 18) cout<<pot[i]<<" "; //~ cout<<"\n"; } int len(LL x) { int res = 0; while(x > 0) { res++; x/=10; } return res; } struct num { LL base, x, y; num() {} num(LL _base, LL _x, LL _y) : base(_base), x(_x), y(_y) {} num succ() { int l0 = len(y+1); if(l0 > x) { return num(base+1, x, 0); } else { return num(base, x, y+1); } } //zakladajac, ze k <= len() LL pref(int k) { int l0 = len(base); if(l0 >= k) { return base/pot[l0-k]; } else { int l1 = len(y); if(l0+x-l1 >= k) { return base*pot[k-l0]; } else { return base*pot[k-l0] + y/pot[l0+x-k]; } } } }; num res[N]; int ile[N]; int main() { //~ ios::sync_with_stdio(0); //~ cin.tie(0); cin>>n; FOR(i, 1, n) cin>>tab[i]; preproc(); //~ num rmc(97899, 5, 2999); //~ cout<<rmc.succ().base<<" "<<rmc.succ().x<<" "<<rmc.succ().y<<"\n"; //~ FOR(i, 1, 10) cout<<rmc.pref(i)<<" "; //~ cout<<"\n"; res[1] = num(tab[1], 0, 0); //~ cout<<res[1].base<<" "<<res[1].x<<" "<<res[1].y<<"\n"; FOR(i, 2, n) { //~ cout<<i<<"\n"; res[i] = num(tab[i], 0, 0); //~ cout<<len(res[i-1].base) + res[i-1].x<<" "<<len(res[i].base)<<"\n"; if(len(res[i-1].base) + res[i-1].x >= len(res[i].base)) { num mn = res[i-1].succ(); int l0 = len(mn.base)+mn.x; int l1 = len(res[i].base); //~ cout<<mn.base<<" "<<mn.x<<" "<<mn.y<<" "<<mn.pref(l1)<<"\n"; //~ cout<<l0<<" "<<l1<<"\n"; //~ cout<<mn.pref(l1)<<"\n"; if(res[i].base < mn.pref(l1)) { res[i].x = l0-l1+1; wyn+=l0-l1+1; ile[i] = l0-l1+1; } else if(mn.pref(l1) == res[i].base) { res[i] = mn; wyn+=l0-l1; ile[i] = l0-l1; } else { res[i].x = l0-l1; wyn+=l0-l1; ile[i] = l0-l1; } } //~ cout<<res[i].base<<" "<<res[i].x<<" "<<res[i].y<<" "<<ile[i]<<"\n"; } //~ FOR(i, 1, n) cout<<res[i]<<" "; //~ cout<<"\n"; cout<<wyn<<"\n"; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<int> VI; const int INF = 1000000009; const LL LINF = 1000000000000000009LL; #define FOR(i, b, e) for(int i = b; i <= e; ++i) #define FORD(i, b, e) for(int i = b; i >= e; --i) #define REP(i, n) FOR(i, 0, n-1) #define REV(i, n) FORD(i, n-1, 0) #define PB push_back #define PP pop_back #define MP make_pair #define ST first #define ND second #define SIZE(c) (int)(c).size() #define ALL(c) (c).begin(), (c).end() #define DEBUG(s) s const int N = 200005; const int L = 16; int n; int tab[N]; LL wyn; const int M = 10; LL pot[20]; void preproc() { pot[0] = 1; FOR(i, 1, 18) pot[i] = 10ll*pot[i-1]; //~ FOR(i, 0, 18) cout<<pot[i]<<" "; //~ cout<<"\n"; } int len(LL x) { int res = 0; while(x > 0) { res++; x/=10; } return res; } struct num { LL base, x, y; num() {} num(LL _base, LL _x, LL _y) : base(_base), x(_x), y(_y) {} num succ() { int l0 = len(y+1); if(l0 > x) { return num(base+1, x, 0); } else { return num(base, x, y+1); } } //zakladajac, ze k <= len() LL pref(int k) { int l0 = len(base); if(l0 >= k) { return base/pot[l0-k]; } else { int l1 = len(y); if(l0+x-l1 >= k) { return base*pot[k-l0]; } else { return base*pot[k-l0] + y/pot[l0+x-k]; } } } }; num res[N]; int ile[N]; int main() { //~ ios::sync_with_stdio(0); //~ cin.tie(0); cin>>n; FOR(i, 1, n) cin>>tab[i]; preproc(); //~ num rmc(97899, 5, 2999); //~ cout<<rmc.succ().base<<" "<<rmc.succ().x<<" "<<rmc.succ().y<<"\n"; //~ FOR(i, 1, 10) cout<<rmc.pref(i)<<" "; //~ cout<<"\n"; res[1] = num(tab[1], 0, 0); //~ cout<<res[1].base<<" "<<res[1].x<<" "<<res[1].y<<"\n"; FOR(i, 2, n) { //~ cout<<i<<"\n"; res[i] = num(tab[i], 0, 0); //~ cout<<len(res[i-1].base) + res[i-1].x<<" "<<len(res[i].base)<<"\n"; if(len(res[i-1].base) + res[i-1].x >= len(res[i].base)) { num mn = res[i-1].succ(); int l0 = len(mn.base)+mn.x; int l1 = len(res[i].base); //~ cout<<mn.base<<" "<<mn.x<<" "<<mn.y<<" "<<mn.pref(l1)<<"\n"; //~ cout<<l0<<" "<<l1<<"\n"; //~ cout<<mn.pref(l1)<<"\n"; if(res[i].base < mn.pref(l1)) { res[i].x = l0-l1+1; wyn+=l0-l1+1; ile[i] = l0-l1+1; } else if(mn.pref(l1) == res[i].base) { res[i] = mn; wyn+=l0-l1; ile[i] = l0-l1; } else { res[i].x = l0-l1; wyn+=l0-l1; ile[i] = l0-l1; } } //~ cout<<res[i].base<<" "<<res[i].x<<" "<<res[i].y<<" "<<ile[i]<<"\n"; } //~ FOR(i, 1, n) cout<<res[i]<<" "; //~ cout<<"\n"; cout<<wyn<<"\n"; return 0; } |