#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll maxn = 2e5 + 55, inf = 1e9; struct big{ ll poc, kon, zera, dlpoc; // kon < zera } t, p; ll n, res; ll pot[17]; bool add(int co, int dl){ co++; int licz = 0; while(co > 0){ co /= 10; licz++; } return licz <= dl; } main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; // zachlannie dobieraj kolejne liczby // uwazaj na przepelnienia dziewiatek pot[0] = 1; int pom, dl; for(int i=1; i<17; i++) pot[i] = pot[i-1]*10; for(int i=0; i<n; i++){ cin >> t.poc; t.dlpoc = 0, t.zera = 0, t.kon = 0; pom = t.poc; while(pom > 0){ pom /= 10; t.dlpoc++; } if(t.dlpoc >= p.dlpoc && t.poc > p.poc){ p = t; continue; } if(t.poc > p.poc/pot[p.dlpoc-t.dlpoc]){ while(p.dlpoc > t.dlpoc){ t.poc *= 10; t.dlpoc++; res++; } if(t.poc >= inf) t.zera = p.zera, res += p.zera; } else if(t.poc < p.poc/pot[p.dlpoc-t.dlpoc]){ while(p.dlpoc > t.dlpoc){ t.poc *= 10; t.dlpoc++; res++; } if(t.poc < inf) t.poc *= 10, res++, t.dlpoc++; else t.zera = p.zera+1, res += p.zera+1; } else{ // sa rowne, czyli musze uwazac na przepelnianie pom = t.poc, dl = t.dlpoc; t = p; if(add(t.kon, t.zera)){ t.kon++; res += t.dlpoc - dl + t.zera; } else if(add(t.poc-pom*pot[t.dlpoc - dl], t.dlpoc-dl)){ t.poc++; res += t.dlpoc - dl + t.zera; } else{ // to samo co z mniejszym t.poc = pom, t.dlpoc = dl; while(p.dlpoc > t.dlpoc){ t.poc *= 10; t.dlpoc++; res++; } if(t.poc < inf) t.poc *= 10, res++, t.dlpoc++; else t.zera = p.zera+1, res += p.zera+1; } } p = t; } cout << res; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll maxn = 2e5 + 55, inf = 1e9; struct big{ ll poc, kon, zera, dlpoc; // kon < zera } t, p; ll n, res; ll pot[17]; bool add(int co, int dl){ co++; int licz = 0; while(co > 0){ co /= 10; licz++; } return licz <= dl; } main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; // zachlannie dobieraj kolejne liczby // uwazaj na przepelnienia dziewiatek pot[0] = 1; int pom, dl; for(int i=1; i<17; i++) pot[i] = pot[i-1]*10; for(int i=0; i<n; i++){ cin >> t.poc; t.dlpoc = 0, t.zera = 0, t.kon = 0; pom = t.poc; while(pom > 0){ pom /= 10; t.dlpoc++; } if(t.dlpoc >= p.dlpoc && t.poc > p.poc){ p = t; continue; } if(t.poc > p.poc/pot[p.dlpoc-t.dlpoc]){ while(p.dlpoc > t.dlpoc){ t.poc *= 10; t.dlpoc++; res++; } if(t.poc >= inf) t.zera = p.zera, res += p.zera; } else if(t.poc < p.poc/pot[p.dlpoc-t.dlpoc]){ while(p.dlpoc > t.dlpoc){ t.poc *= 10; t.dlpoc++; res++; } if(t.poc < inf) t.poc *= 10, res++, t.dlpoc++; else t.zera = p.zera+1, res += p.zera+1; } else{ // sa rowne, czyli musze uwazac na przepelnianie pom = t.poc, dl = t.dlpoc; t = p; if(add(t.kon, t.zera)){ t.kon++; res += t.dlpoc - dl + t.zera; } else if(add(t.poc-pom*pot[t.dlpoc - dl], t.dlpoc-dl)){ t.poc++; res += t.dlpoc - dl + t.zera; } else{ // to samo co z mniejszym t.poc = pom, t.dlpoc = dl; while(p.dlpoc > t.dlpoc){ t.poc *= 10; t.dlpoc++; res++; } if(t.poc < inf) t.poc *= 10, res++, t.dlpoc++; else t.zera = p.zera+1, res += p.zera+1; } } p = t; } cout << res; } |