Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. //Daniel Grzegorzewski
  2. #include <bits/stdc++.h>
  3. #pragma GCC optimize("O3")
  4.  
  5. #define MP make_pair
  6. #define PB push_back
  7. #define ST first
  8. #define ND second
  9. #define int long long
  10.  
  11. using namespace std;
  12.  
  13. typedef pair<int, int> PII;
  14. typedef vector<int> VI;
  15. typedef vector<PII> VII;
  16. typedef long long LL;
  17.  
  18. void init_ios() {
  19.      ios_base::sync_with_stdio(0);
  20.      cin.tie(0);
  21. }
  22.  
  23. const int N = 2*(int)1e5 + 10;
  24. const int LOL = 18;
  25.  
  26. int n, len, res;
  27. string a[N];
  28.  
  29. bool check(const string& x) {
  30.   if (x.size() >= LOL)
  31.     len = x.size();
  32. }
  33.  
  34. string dodaj(string co) {
  35.   int nxt = 1;
  36.   for (int i = (int)co.size()-1; nxt && i >= 0; --i) {
  37.     if (co[i] == '9')
  38.       co[i] = '0';
  39.     else {
  40.       co[i]++;
  41.       nxt = 0;
  42.     }
  43.   }
  44.   if (nxt)
  45.     co = "1" + co;
  46.   return co;
  47. }
  48.  
  49. void modify(int i) {
  50.   // a[i-1] krotszy od LOL
  51.   if (len == 0) {
  52.     if (a[i].size() > a[i-1].size())
  53.       return;
  54.     else if (a[i].size() == a[i-1].size()) {
  55.       if (a[i] > a[i-1])
  56.         return;
  57.       ++res;
  58.       a[i] += "0";
  59.       check(a[i]);
  60.       return;
  61.     }
  62.     else {
  63.       string pref = a[i-1].substr(0, a[i].size());
  64.       if (a[i] > pref) {
  65.         while (a[i].size() < a[i-1].size()) {
  66.           ++res;
  67.           a[i] += "0";
  68.         }
  69.         check(a[i]);
  70.       }
  71.       else if (a[i] < pref) {
  72.         while (a[i].size() <= a[i-1].size()) {
  73.           ++res;
  74.           a[i] += "0";
  75.         }
  76.         check(a[i]);
  77.       }
  78.       else {
  79.         string suf = a[i-1].substr(a[i].size());
  80.         string suf2 = dodaj(suf);
  81.         if (suf.size() == suf2.size()) {
  82.           res += suf.size();
  83.           a[i] += suf2;
  84.         }
  85.         else {
  86.           while (a[i].size() <= a[i-1].size()) {
  87.             ++res;
  88.             a[i] += "0";
  89.           }
  90.         }
  91.         check(a[i]);
  92.       }
  93.     }
  94.   }
  95.   // a[i-1] co najmniej dlugosci LOL
  96.   else {
  97.     string pref = a[i-1].substr(0, a[i].size());
  98.     if (pref > a[i]) {
  99.       ++len;
  100.       res += len-a[i].size();
  101.       while (a[i].size() <= LOL)
  102.         a[i] += "0";
  103.     }
  104.     else if (a[i] == pref) {
  105.       res += len-a[i].size();
  106.       while (a[i].size() <= LOL)
  107.         a[i] += a[i-1][a[i].size()];
  108.     }
  109.     else {
  110.       res += len-a[i].size();
  111.       while (a[i].size() <= LOL)
  112.         a[i] += "0";
  113.     }
  114.   }
  115. }
  116.  
  117. signed main() {
  118.   init_ios();
  119.   cin >> n;
  120.   for (int i = 1; i <= n; ++i)
  121.     cin >> a[i];
  122.   for (int i = 2; i <= n; ++i)
  123.     modify(i);
  124.   cout<<res<<"\n";
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement