#include "cstdio" #include "cstring" // #define D #define MAX 12 #define MAXL 1000000000 #define lli long long int int n; lli r, tr; lli is, t, im; int ls, lm, e, f, i; int dl(lli v) { if(v > 999999999) return 10; if(v > 99999999) return 8; if(v > 9999999) return 8; if(v > 999999) return 7; if(v > 99999) return 6; if(v > 9999) return 5; if(v > 999) return 4; if(v > 99) return 3; if(v > 9) return 2; return 1; } int rdl(int l) { if(l == 5) return 100000; if(l == 4) return 10000; if(l == 3) return 1000; if(l == 2) return 100; if(l == 1) return 10; if(l == 0) return 1; return 1000000; } void step(lli &i, int &l) { ++l; lli t = i * 10; if(t <= MAXL) { i = t; } } int main() { r = 0; scanf("%d", &n); scanf("%lld", &im); lm = dl(im); e = 0; f = rdl(lm); #ifdef D printf("\t# m(%6d) %-10lld\n", lm, im); #endif while(--n) { scanf("%lld", &is); ls = dl(is); tr = 0; #ifdef D printf("#%d", n); printf("\tR m(%6d) %-10lld\n\t: s(%6d) %-10lld\n", lm, im, ls, is); #endif if(lm < 11) { #ifdef D puts("\t -> lm < 11"); #endif while(ls < lm) { step(is, ls); ++tr; } #ifdef D puts("\t while step"); printf("\t# m(%6d) %-10lld\n\t: s(%6d) %-10lld\n", lm, im, ls, is); #endif if(is > im) { im = is; lm = ls; } else { //check #ifdef D printf("\t# tr = %lld (%d == %d)\n", tr, dl(im+1), lm); #endif t = rdl(tr); if(tr && (im + 1 < im - im % t + t)) { is = ++im; } else { step(is, ls); ++tr; lm = ls; im = is; } } } else { if(ls > lm) { lm = ls; im = is; e = 0; f = rdl(lm); } else { while(ls < lm) { step(is, ls); ++tr; } if(is > im) { im = is; } else { if(tr && ((e+1) < f)) { ++e; } else { step(is, ls); ++tr; lm = ls; im = is; } } } } #ifdef D printf("\tE m(%6d) %-10lld\n\t: s(%6d) %-10lld\n", lm, im, ls, is); printf("\t# r = %lld + %lld\n", r, tr); #endif r += tr; } printf("%lld\n", r); 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 164 165 166 167 168 169 | #include "cstdio" #include "cstring" // #define D #define MAX 12 #define MAXL 1000000000 #define lli long long int int n; lli r, tr; lli is, t, im; int ls, lm, e, f, i; int dl(lli v) { if(v > 999999999) return 10; if(v > 99999999) return 8; if(v > 9999999) return 8; if(v > 999999) return 7; if(v > 99999) return 6; if(v > 9999) return 5; if(v > 999) return 4; if(v > 99) return 3; if(v > 9) return 2; return 1; } int rdl(int l) { if(l == 5) return 100000; if(l == 4) return 10000; if(l == 3) return 1000; if(l == 2) return 100; if(l == 1) return 10; if(l == 0) return 1; return 1000000; } void step(lli &i, int &l) { ++l; lli t = i * 10; if(t <= MAXL) { i = t; } } int main() { r = 0; scanf("%d", &n); scanf("%lld", &im); lm = dl(im); e = 0; f = rdl(lm); #ifdef D printf("\t# m(%6d) %-10lld\n", lm, im); #endif while(--n) { scanf("%lld", &is); ls = dl(is); tr = 0; #ifdef D printf("#%d", n); printf("\tR m(%6d) %-10lld\n\t: s(%6d) %-10lld\n", lm, im, ls, is); #endif if(lm < 11) { #ifdef D puts("\t -> lm < 11"); #endif while(ls < lm) { step(is, ls); ++tr; } #ifdef D puts("\t while step"); printf("\t# m(%6d) %-10lld\n\t: s(%6d) %-10lld\n", lm, im, ls, is); #endif if(is > im) { im = is; lm = ls; } else { //check #ifdef D printf("\t# tr = %lld (%d == %d)\n", tr, dl(im+1), lm); #endif t = rdl(tr); if(tr && (im + 1 < im - im % t + t)) { is = ++im; } else { step(is, ls); ++tr; lm = ls; im = is; } } } else { if(ls > lm) { lm = ls; im = is; e = 0; f = rdl(lm); } else { while(ls < lm) { step(is, ls); ++tr; } if(is > im) { im = is; } else { if(tr && ((e+1) < f)) { ++e; } else { step(is, ls); ++tr; lm = ls; im = is; } } } } #ifdef D printf("\tE m(%6d) %-10lld\n\t: s(%6d) %-10lld\n", lm, im, ls, is); printf("\t# r = %lld + %lld\n", r, tr); #endif r += tr; } printf("%lld\n", r); return 0; } |