#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int N, as, bs, aplus = 0, bplus = 0;
unsigned long long suma = 0;
string a,b;
cin>>N;
cin>>a; as = a.size();
N--;
while(N-->0) {
cin>>b;
bs = b.size();
if(b.size() <= as) {
bs = as;
bplus = aplus;
suma += bs-b.size();
if(a.size() <= b.size()) {
string added;
if(aplus) added = to_string(aplus);
a+=string(min(as-a.size()-added.size(), 12-a.size()-added.size()), '0');
a+=added;
}
if(!lexicographical_compare(a.begin(), a.end(), b.begin(), b.end())) {
// a >= b;
if(b.size() < a.size() && string(a.begin(), a.begin()+b.size()) == b) {
if(!bplus) {
bplus = a[b.size()]-0x30;
} else if(bplus < 9) bplus++;
else if(bs - b.size() > 1) {
bplus++;
} else {
bplus = 0;
suma++;
bs++;
}
} else {
bs++;
bplus = 0;
suma++;
}
}
}
a = b;
as = bs;
aplus = bplus;
}
cout << suma << 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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int N, as, bs, aplus = 0, bplus = 0; unsigned long long suma = 0; string a,b; cin>>N; cin>>a; as = a.size(); N--; while(N-->0) { cin>>b; bs = b.size(); if(b.size() <= as) { bs = as; bplus = aplus; suma += bs-b.size(); if(a.size() <= b.size()) { string added; if(aplus) added = to_string(aplus); a+=string(min(as-a.size()-added.size(), 12-a.size()-added.size()), '0'); a+=added; } if(!lexicographical_compare(a.begin(), a.end(), b.begin(), b.end())) { // a >= b; if(b.size() < a.size() && string(a.begin(), a.begin()+b.size()) == b) { if(!bplus) { bplus = a[b.size()]-0x30; } else if(bplus < 9) bplus++; else if(bs - b.size() > 1) { bplus++; } else { bplus = 0; suma++; bs++; } } else { bs++; bplus = 0; suma++; } } } a = b; as = bs; aplus = bplus; } cout << suma << endl; return 0; } |
English