#include <bits/stdc++.h> using namespace std; const long long inf=1e17; /* for a,b where a was the (i-1)th number 1. If b is greater than a - nothing happens, b is the next a and a.len is the length of b 2. If b is a prefix of (a+1), a+1 is the next a and a.len is the length of (a+1) 3. If b extended to the length of a is greater than a, b is the next and a.len stays the same 4. If none of the above work, output b extended past a by one zero */ long long extend(long long a,long long pot) { while(a<pot) { a*=10; } return a; } bool ispref(long long a,long long pot,long long b,long long pot2) { pot2/=pot; if(b/pot2==a) return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n,i,j; long long last2,last_pot2,last=0,curr,last_pot=1,curr_pot,curr_len,last_len=1,last_len2,ans=0; cin>>n; for(i=0;i<n;i++) { cin>>curr; curr_pot=1; curr_len=1; if(curr>last) { while(curr_pot*10<=curr) { curr_pot*=10; curr_len++; } } else { while(curr_pot*10<=curr) { curr_pot*=10; curr_len++; } last2=last+1; last_pot2=last_pot; last_len2=last_len; if(last_pot2*10<=last2) { last_pot2*=10; last_len2++; } if(ispref(curr,curr_pot,last2,last_pot2)) { curr=last2; curr_pot=last_pot2; ans+=last_len2-curr_len; curr_len=last_len2; } else { curr=extend(curr,last_pot); curr_pot=last_pot; if(curr>last) { ans+=last_len-curr_len; curr_len=last_len; } else { ans+=last_len-curr_len+1; curr_len=last_len+1; if(curr_pot<inf) { curr*=10; curr_pot*=10; } } } } last=curr; last_pot=curr_pot; last_len=curr_len; } cout<<ans; }
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 | #include <bits/stdc++.h> using namespace std; const long long inf=1e17; /* for a,b where a was the (i-1)th number 1. If b is greater than a - nothing happens, b is the next a and a.len is the length of b 2. If b is a prefix of (a+1), a+1 is the next a and a.len is the length of (a+1) 3. If b extended to the length of a is greater than a, b is the next and a.len stays the same 4. If none of the above work, output b extended past a by one zero */ long long extend(long long a,long long pot) { while(a<pot) { a*=10; } return a; } bool ispref(long long a,long long pot,long long b,long long pot2) { pot2/=pot; if(b/pot2==a) return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n,i,j; long long last2,last_pot2,last=0,curr,last_pot=1,curr_pot,curr_len,last_len=1,last_len2,ans=0; cin>>n; for(i=0;i<n;i++) { cin>>curr; curr_pot=1; curr_len=1; if(curr>last) { while(curr_pot*10<=curr) { curr_pot*=10; curr_len++; } } else { while(curr_pot*10<=curr) { curr_pot*=10; curr_len++; } last2=last+1; last_pot2=last_pot; last_len2=last_len; if(last_pot2*10<=last2) { last_pot2*=10; last_len2++; } if(ispref(curr,curr_pot,last2,last_pot2)) { curr=last2; curr_pot=last_pot2; ans+=last_len2-curr_len; curr_len=last_len2; } else { curr=extend(curr,last_pot); curr_pot=last_pot; if(curr>last) { ans+=last_len-curr_len; curr_len=last_len; } else { ans+=last_len-curr_len+1; curr_len=last_len+1; if(curr_pot<inf) { curr*=10; curr_pot*=10; } } } } last=curr; last_pot=curr_pot; last_len=curr_len; } cout<<ans; } |