#include<cstdio> #include<algorithm> #include<string> using namespace std; int n; int liczby[200005]; int cyfry[200005][12]; string answer; char ctc(int x){ return '0' + x; } int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%d", &liczby[i]); int multi = 1000000000; int log = 10; bool go = false; int wsk = 1; while(multi != 0){ if(go == false and liczby[i]/multi != 0){ go = true; cyfry[i][0] = log; } if(go == false){ multi /= 10; log--; continue; } cyfry[i][wsk] = (liczby[i]/multi)%10; multi /= 10; log--; wsk++; } } for(int i = 1; i <= cyfry[1][0]; i++){ answer = answer + ctc(cyfry[1][i]); } long long wynik = 0; for(int i = 2; i <= n; i++){ if(cyfry[i][0] > answer.size()){ answer.clear(); for(int j = 1; j <= cyfry[i][0]; j++){ answer = answer + ctc(cyfry[i][j]); } } else{ bool done_ear = false; for(int j = 1; j <= cyfry[i][0]; j++){ if(answer[j-1] > ctc(cyfry[i][j])){ int x = answer.size(); answer.clear(); for(int k = 1; k <= cyfry[i][0]; k++){ answer = answer + ctc(cyfry[i][k]); } while(answer.size() != x + 1) answer = answer + '0'; done_ear = true; break; } else if(answer[j-1] < ctc(cyfry[i][j])){ int x = answer.size(); answer.clear(); for(int k = 1; k <= cyfry[i][0]; k++){ answer = answer + ctc(cyfry[i][k]); } while(answer.size() != x) answer = answer + '0'; done_ear = true; break; } } if(!done_ear){ answer.back()++; for(int j = answer.size() - 1; j >= 0; j--){ if(answer[j] > '9'){ answer[j] = '0'; answer[j-1]++; } } for(int j = 1; j <= cyfry[i][0]; j++){ if(answer[j-1] != ctc(cyfry[i][j])){ int x = answer.size(); answer.clear(); for(int k = 1; k <= cyfry[i][0]; k++){ answer = answer + ctc(cyfry[i][k]); } while(answer.size() != x + 1) answer = answer + '0'; break; } } } wynik += answer.size() - cyfry[i][0]; } } printf("%lld", wynik); 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 | #include<cstdio> #include<algorithm> #include<string> using namespace std; int n; int liczby[200005]; int cyfry[200005][12]; string answer; char ctc(int x){ return '0' + x; } int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%d", &liczby[i]); int multi = 1000000000; int log = 10; bool go = false; int wsk = 1; while(multi != 0){ if(go == false and liczby[i]/multi != 0){ go = true; cyfry[i][0] = log; } if(go == false){ multi /= 10; log--; continue; } cyfry[i][wsk] = (liczby[i]/multi)%10; multi /= 10; log--; wsk++; } } for(int i = 1; i <= cyfry[1][0]; i++){ answer = answer + ctc(cyfry[1][i]); } long long wynik = 0; for(int i = 2; i <= n; i++){ if(cyfry[i][0] > answer.size()){ answer.clear(); for(int j = 1; j <= cyfry[i][0]; j++){ answer = answer + ctc(cyfry[i][j]); } } else{ bool done_ear = false; for(int j = 1; j <= cyfry[i][0]; j++){ if(answer[j-1] > ctc(cyfry[i][j])){ int x = answer.size(); answer.clear(); for(int k = 1; k <= cyfry[i][0]; k++){ answer = answer + ctc(cyfry[i][k]); } while(answer.size() != x + 1) answer = answer + '0'; done_ear = true; break; } else if(answer[j-1] < ctc(cyfry[i][j])){ int x = answer.size(); answer.clear(); for(int k = 1; k <= cyfry[i][0]; k++){ answer = answer + ctc(cyfry[i][k]); } while(answer.size() != x) answer = answer + '0'; done_ear = true; break; } } if(!done_ear){ answer.back()++; for(int j = answer.size() - 1; j >= 0; j--){ if(answer[j] > '9'){ answer[j] = '0'; answer[j-1]++; } } for(int j = 1; j <= cyfry[i][0]; j++){ if(answer[j-1] != ctc(cyfry[i][j])){ int x = answer.size(); answer.clear(); for(int k = 1; k <= cyfry[i][0]; k++){ answer = answer + ctc(cyfry[i][k]); } while(answer.size() != x + 1) answer = answer + '0'; break; } } } wynik += answer.size() - cyfry[i][0]; } } printf("%lld", wynik); return 0; } |