#include <bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define fst first #define snd second #define int4 int32_t #define LL long long #define int LL #define FOR(i,a,b) for (int (i) = (int)(a) - (int)(a > b); (i) != (int)(b) - (int)(a > b); (i) += ((a) < (b)) ? 1 : -1) #define REP(b) FOR(i, 0, b) #define REP1(b) FOR(i, 1, (b)+1) #define REV(b) FOR(i, b, 0) #define REV1(b) FOR(i, (b)+1, 1) #define ITER(i, b) for (auto (i) : (b)) #define ALL(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class C> void minu(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxu(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h) { while(*sdbg==' ') sdbg++; if(*sdbg!='\"' && *sdbg!='\'')cerr<<" "<<sdbg<<"="<<h<<"\n"; else cerr<<" "<<h<<"\n"; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg==' ') sdbg++; if(*sdbg!='\"' && *sdbg!='\'') { cerr<<" "; while(*sdbg!=',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } else { while(*sdbg!=',') sdbg++; cerr<<" "<<h<<","; _dbg(sdbg+1, a...); } } template<class T> ostream &operator<<(ostream &os, vector<T> V) { os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.fst << "," << P.snd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const int maxn=200005; string cen[maxn]; int dl[maxn]; bool pref_big(int pier_nr, int drug_nr) { string &pier = cen[pier_nr]; string &drug = cen[drug_nr]; REP(min(sz(pier), sz(drug))) { if(pier[i] < drug[i]) return 1; if(pier[i] > drug[i]) return 0; } return 0; } bool pref_big_eq(int pier_nr, int drug_nr) { debug(pier_nr, drug_nr); string &pier = cen[pier_nr]; string &drug = cen[drug_nr]; REP(min(sz(pier), sz(drug))) { debug(pier[i], drug[i], i); if(pier[i] < drug[i]) return 1; if(pier[i] > drug[i]) return 0; } return 1; } bool bigger(int pier, int drug) { if(dl[pier]<dl[drug]) return 1; if(dl[pier]>dl[drug]) return 0; return pref_big(pier, drug); } void add0(string &a, int il) { REP(il) a+='0'; } string plus1(string a) { a.back()++; FOR(i, sz(a), 0) { if(a[i]>'9') { a[i]-=10; if(i>0) a[i-1]++; else { a='1'+a; } } } return a; } void kopi(int pier_nr, int drug_nr) { string &pier = cen[pier_nr]; string &drug = cen[drug_nr]; REP(10) { if(i+1>sz(drug)) drug+=pier[i]; } } int32_t main() { ios_base::sync_with_stdio(0); int n; cin>>n; REP(n) cin>>cen[i+1]; REP(n) dl[i+1]=sz(cen[i+1]); int sum=0; REP(n) sum+=dl[i+1]; int wyn=0; cen[0]=""; for(int i=0; i<=n; i++) cerr<<i<<" "<<cen[i]<<" "<<dl[i]<<endl; FOR(i, 1, n+1) { wyn-=dl[i]; debug(dl[i], wyn); if(!bigger(i-1, i)) { if(dl[i-1]<=18) { debug(i, cen[i], cen[i-1]); string &tmp = cen[maxn-1]; tmp = cen[i]; dl[maxn-1]=dl[i]; int il=0; while(!bigger(i-1, maxn-1)) { tmp+='9'; il++; dl[maxn-1]++; } debug(il); if(dl[i]+il>dl[i-1]) { add0(cen[i], il); debug("pier", cen[i]); } else if(pref_big(i-1, i)) { add0(cen[i], il); debug("drug", cen[i]); } else { cen[i]=plus1(cen[i-1]); debug("trze", cen[i]); } dl[i]=sz(cen[i]); } else { debug("else"); if(pref_big(i-1, i)) { dl[i]=dl[i-1]; add0(cen[i], 10); } else if(pref_big_eq(i-1, i)) { dl[i]=dl[i-1]; kopi(i-1, i); } else { dl[i]=dl[i-1]+1; add0(cen[i], 10); } } } wyn+=dl[i]; debug(dl[i], wyn); cerr<<endl; } for(int i=0; i<=n; i++) cerr<<i<<" "<<cen[i]<<" "<<dl[i]<<endl; printf("%lld\n", wyn); }
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | #include <bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define fst first #define snd second #define int4 int32_t #define LL long long #define int LL #define FOR(i,a,b) for (int (i) = (int)(a) - (int)(a > b); (i) != (int)(b) - (int)(a > b); (i) += ((a) < (b)) ? 1 : -1) #define REP(b) FOR(i, 0, b) #define REP1(b) FOR(i, 1, (b)+1) #define REV(b) FOR(i, b, 0) #define REV1(b) FOR(i, (b)+1, 1) #define ITER(i, b) for (auto (i) : (b)) #define ALL(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class C> void minu(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxu(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h) { while(*sdbg==' ') sdbg++; if(*sdbg!='\"' && *sdbg!='\'')cerr<<" "<<sdbg<<"="<<h<<"\n"; else cerr<<" "<<h<<"\n"; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg==' ') sdbg++; if(*sdbg!='\"' && *sdbg!='\'') { cerr<<" "; while(*sdbg!=',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } else { while(*sdbg!=',') sdbg++; cerr<<" "<<h<<","; _dbg(sdbg+1, a...); } } template<class T> ostream &operator<<(ostream &os, vector<T> V) { os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.fst << "," << P.snd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const int maxn=200005; string cen[maxn]; int dl[maxn]; bool pref_big(int pier_nr, int drug_nr) { string &pier = cen[pier_nr]; string &drug = cen[drug_nr]; REP(min(sz(pier), sz(drug))) { if(pier[i] < drug[i]) return 1; if(pier[i] > drug[i]) return 0; } return 0; } bool pref_big_eq(int pier_nr, int drug_nr) { debug(pier_nr, drug_nr); string &pier = cen[pier_nr]; string &drug = cen[drug_nr]; REP(min(sz(pier), sz(drug))) { debug(pier[i], drug[i], i); if(pier[i] < drug[i]) return 1; if(pier[i] > drug[i]) return 0; } return 1; } bool bigger(int pier, int drug) { if(dl[pier]<dl[drug]) return 1; if(dl[pier]>dl[drug]) return 0; return pref_big(pier, drug); } void add0(string &a, int il) { REP(il) a+='0'; } string plus1(string a) { a.back()++; FOR(i, sz(a), 0) { if(a[i]>'9') { a[i]-=10; if(i>0) a[i-1]++; else { a='1'+a; } } } return a; } void kopi(int pier_nr, int drug_nr) { string &pier = cen[pier_nr]; string &drug = cen[drug_nr]; REP(10) { if(i+1>sz(drug)) drug+=pier[i]; } } int32_t main() { ios_base::sync_with_stdio(0); int n; cin>>n; REP(n) cin>>cen[i+1]; REP(n) dl[i+1]=sz(cen[i+1]); int sum=0; REP(n) sum+=dl[i+1]; int wyn=0; cen[0]=""; for(int i=0; i<=n; i++) cerr<<i<<" "<<cen[i]<<" "<<dl[i]<<endl; FOR(i, 1, n+1) { wyn-=dl[i]; debug(dl[i], wyn); if(!bigger(i-1, i)) { if(dl[i-1]<=18) { debug(i, cen[i], cen[i-1]); string &tmp = cen[maxn-1]; tmp = cen[i]; dl[maxn-1]=dl[i]; int il=0; while(!bigger(i-1, maxn-1)) { tmp+='9'; il++; dl[maxn-1]++; } debug(il); if(dl[i]+il>dl[i-1]) { add0(cen[i], il); debug("pier", cen[i]); } else if(pref_big(i-1, i)) { add0(cen[i], il); debug("drug", cen[i]); } else { cen[i]=plus1(cen[i-1]); debug("trze", cen[i]); } dl[i]=sz(cen[i]); } else { debug("else"); if(pref_big(i-1, i)) { dl[i]=dl[i-1]; add0(cen[i], 10); } else if(pref_big_eq(i-1, i)) { dl[i]=dl[i-1]; kopi(i-1, i); } else { dl[i]=dl[i-1]+1; add0(cen[i], 10); } } } wyn+=dl[i]; debug(dl[i], wyn); cerr<<endl; } for(int i=0; i<=n; i++) cerr<<i<<" "<<cen[i]<<" "<<dl[i]<<endl; printf("%lld\n", wyn); } |