#include<iostream> #include<string> using namespace std; long long getpow(int n) { switch(n) { case 0: return 1; case 1: return 10; case 2: return 100; case 3: return 1000; case 4: return 10000; case 5: return 100000; default: return 1000000; } return 1000000; } string add1(string s) { s[s.length()-1]++; for(int i=s.length()-2; i>=0;i--) { if(s[i+1]>'9') { s[i+1]='0'; s[i]++; } } return s; } bool cmp(const string& a, const string& b) { if(a.length() == b.length()) { return a<b; } else return a.length()<b.length(); } int main() { ios_base::sync_with_stdio(false); int n; long long counter = 1; long long res=0; long long x=0; string cur; string prev=""; cin>>n; while(n--) { cin >>cur; if(prev == cur) { if(counter < getpow(x)) { counter++; } else { x++; counter = 1; } res+=x; } if(prev.length()+x < cur.length()) { x=0; counter = 1; } if( !cmp(prev,cur) || x != 0) { //cout << (prev >=cur)<< (x!=0)<<endl; //cout <<prev<<" "<<cur<<endl; for(int i=0;i<max(prev.length(), cur.length()) && prev != "";i++) { if(i>=prev.length()) { long long maxcou = getpow(prev.length()+x-cur.length()); for(int j=0;j+i<cur.length() ;j++) { maxcou+=(cur[i+j]-'0')*getpow(x-j-1); } //cout <<"c: "<<counter<<" "<<maxcou<<endl; if(counter < maxcou) { x -= cur.length() - prev.length(); } else { x += prev.length() - cur.length() +1; counter = 1; } res+=x; break; } if(i>=cur.length()) { if(counter < getpow(x)) { counter++; res+= x + prev.length() - cur.length(); cur = prev; break; } else { bool f=false; for(int j = i ;j < prev.length();j++) { if(prev[j]!='9') { f=true; } } if(f) { res+= x + prev.length() - cur.length(); cur = add1(prev); counter = 1; break; } else { x += prev.length() - cur.length() +1; counter = 1; res+=x; break; } } } if(prev[i] < cur[i]) { x+= prev.length() - cur.length(); counter = 1; res+=x; break; } if(prev[i] > cur[i]) { x+= prev.length() + 1 - cur.length(); res+=x; break; } } } //cout <<":"<<x<<" "<<prev<<" "<<cur<<" "<<res<<" "<<endl; prev = cur; } cout <<res<<endl; 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 | #include<iostream> #include<string> using namespace std; long long getpow(int n) { switch(n) { case 0: return 1; case 1: return 10; case 2: return 100; case 3: return 1000; case 4: return 10000; case 5: return 100000; default: return 1000000; } return 1000000; } string add1(string s) { s[s.length()-1]++; for(int i=s.length()-2; i>=0;i--) { if(s[i+1]>'9') { s[i+1]='0'; s[i]++; } } return s; } bool cmp(const string& a, const string& b) { if(a.length() == b.length()) { return a<b; } else return a.length()<b.length(); } int main() { ios_base::sync_with_stdio(false); int n; long long counter = 1; long long res=0; long long x=0; string cur; string prev=""; cin>>n; while(n--) { cin >>cur; if(prev == cur) { if(counter < getpow(x)) { counter++; } else { x++; counter = 1; } res+=x; } if(prev.length()+x < cur.length()) { x=0; counter = 1; } if( !cmp(prev,cur) || x != 0) { //cout << (prev >=cur)<< (x!=0)<<endl; //cout <<prev<<" "<<cur<<endl; for(int i=0;i<max(prev.length(), cur.length()) && prev != "";i++) { if(i>=prev.length()) { long long maxcou = getpow(prev.length()+x-cur.length()); for(int j=0;j+i<cur.length() ;j++) { maxcou+=(cur[i+j]-'0')*getpow(x-j-1); } //cout <<"c: "<<counter<<" "<<maxcou<<endl; if(counter < maxcou) { x -= cur.length() - prev.length(); } else { x += prev.length() - cur.length() +1; counter = 1; } res+=x; break; } if(i>=cur.length()) { if(counter < getpow(x)) { counter++; res+= x + prev.length() - cur.length(); cur = prev; break; } else { bool f=false; for(int j = i ;j < prev.length();j++) { if(prev[j]!='9') { f=true; } } if(f) { res+= x + prev.length() - cur.length(); cur = add1(prev); counter = 1; break; } else { x += prev.length() - cur.length() +1; counter = 1; res+=x; break; } } } if(prev[i] < cur[i]) { x+= prev.length() - cur.length(); counter = 1; res+=x; break; } if(prev[i] > cur[i]) { x+= prev.length() + 1 - cur.length(); res+=x; break; } } } //cout <<":"<<x<<" "<<prev<<" "<<cur<<" "<<res<<" "<<endl; prev = cur; } cout <<res<<endl; return 0; } |