#include <cstdio> #include <vector> #include <algorithm> using namespace std; typedef long long LL; vector<char> get_digits(int a) { vector<char> v; while(a) { char d = a%10; v.push_back(d); a /= 10; } reverse(v.begin(), v.end()); return v; } const int ADD_ONE = 0, TOO_BIG = 1, UPPER_LOWER = 2, UPPER_GREATER = 3; void upper_lower(vector<char>& cn, vector<char>& av) { fill(cn.begin(), cn.begin()+min((int)cn.size(), 9), 0); fill(cn.end()-min((int)cn.size(), 9), cn.end(), 0); copy(av.begin(), av.end(), cn.begin()); } void upper_greater(vector<char>& cn, vector<char>& av) { upper_lower(cn, av); cn.push_back(0); } void add_one(vector<char>& v, vector<char>& a) { for(int i = v.size()-1; i >= a.size(); --i) { if(v[i] == 9) v[i] = 0; else { ++v[i]; return; } } upper_greater(v, a); } int main() { vector<char> cn; int n, a; scanf("%d", &n); LL result = 0; for(int i = 0; i < n; ++i) { scanf("%d", &a); vector<char> av = get_digits(a); int cs = ADD_ONE; for(int j = 0; j < av.size(); ++j) { if(j >= cn.size()) { cs = TOO_BIG; break; } if(cn[j] < av[j]) { cs = UPPER_LOWER; break; } if(cn[j] > av[j]) { cs = UPPER_GREATER; break; } } if(cn.size() < av.size()) cs = TOO_BIG; if(cs == ADD_ONE) add_one(cn, av); else if(cs == TOO_BIG) cn = av; else if(cs == UPPER_LOWER) upper_lower(cn, av); else upper_greater(cn, av); result += cn.size() - av.size(); } printf("%lld\n", result); }
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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; typedef long long LL; vector<char> get_digits(int a) { vector<char> v; while(a) { char d = a%10; v.push_back(d); a /= 10; } reverse(v.begin(), v.end()); return v; } const int ADD_ONE = 0, TOO_BIG = 1, UPPER_LOWER = 2, UPPER_GREATER = 3; void upper_lower(vector<char>& cn, vector<char>& av) { fill(cn.begin(), cn.begin()+min((int)cn.size(), 9), 0); fill(cn.end()-min((int)cn.size(), 9), cn.end(), 0); copy(av.begin(), av.end(), cn.begin()); } void upper_greater(vector<char>& cn, vector<char>& av) { upper_lower(cn, av); cn.push_back(0); } void add_one(vector<char>& v, vector<char>& a) { for(int i = v.size()-1; i >= a.size(); --i) { if(v[i] == 9) v[i] = 0; else { ++v[i]; return; } } upper_greater(v, a); } int main() { vector<char> cn; int n, a; scanf("%d", &n); LL result = 0; for(int i = 0; i < n; ++i) { scanf("%d", &a); vector<char> av = get_digits(a); int cs = ADD_ONE; for(int j = 0; j < av.size(); ++j) { if(j >= cn.size()) { cs = TOO_BIG; break; } if(cn[j] < av[j]) { cs = UPPER_LOWER; break; } if(cn[j] > av[j]) { cs = UPPER_GREATER; break; } } if(cn.size() < av.size()) cs = TOO_BIG; if(cs == ADD_ONE) add_one(cn, av); else if(cs == TOO_BIG) cn = av; else if(cs == UPPER_LOWER) upper_lower(cn, av); else upper_greater(cn, av); result += cn.size() - av.size(); } printf("%lld\n", result); } |