#include <iostream> #include <stdio.h> #include <cmath> #include <cstring> using namespace std; char buf1[200100]; char buf2[200100]; char *pre = buf1; int prev_l; char *nex=buf2; int next_l; void wypisz(int i) { /* printf("%lld",a[i]); for(int j=0;j<m[i]-1;j++)printf("0"); if(m[i]>0)printf("%lld",d[i]); printf("\n"); */ } long long ilecyfr(long long a) { long long r=0; while(a>0) { r++; a/=10; } return r; } /* bool gt(int i, int j) { long long a1=a[i]; long long m1=m[i]; long long d1=d[i]; long long a2=a[j]; long long m2=m[j]; long long d2=d[j]; // printf("gt("); // wypisz(i); // wypisz(j); // printf(") = "); if(ilecyfr(a1) + m1 == ilecyfr(a2) + m2) { if (ilecyfr(a2)>ilecyfr(a1)) { m1 -= ilecyfr(a2)-ilecyfr(a1); a1 *= pow(10,ilecyfr(a2)-ilecyfr(a1)); } else { m2 -= ilecyfr(a1)-ilecyfr(a2); a2 *= pow(10,ilecyfr(a1)-ilecyfr(a2)); } if(m1==m2 && m1==0) { // printf("%d//1\n",a1+d1>a2+d2); return a1+d1>a2+d2; } if(m1==m2 && m1>0) { // printf("%d//2\n",(a1*10)+d1>(a2*10)+d2); return (a1*10)+d1>(a2*10)+d2; } // printf("ERROR\n"); } // printf("%d//3\n",(ilecyfr(a1) + m1 > ilecyfr(a2) + m2)); return (ilecyfr(a1) + m1 > ilecyfr(a2) + m2); } */ void przepisznextnaprev() { //printf("nex = %s\n",nex); //strcpy(pre,nex); if(pre == buf1) {pre=buf2;nex=buf1;} else {nex=buf2;pre=buf1;} prev_l=next_l; }; bool same9nakoncuprev(int i) { for(int j=prev_l-i;j<prev_l;j++)if(pre[j]!='9')return false; return true; }; void dodaj1doprev() { int i=prev_l-1; while(i>0 && pre[i]=='9') { pre[i]='0'; i--; } pre[i]++; }; int main() { // your code goes here int N; long long s=0; scanf("%d",&N); scanf("%s",pre); prev_l=strlen(pre); for(int i=1;i<N;i++) { scanf("%s",nex); next_l=strlen(nex); //printf("%s(%d) <-> %s(%d)\n",pre,prev_l,nex,next_l); if(next_l>prev_l) {przepisznextnaprev();continue;}; if(next_l == prev_l && strcmp(nex,pre)>0){przepisznextnaprev();continue;}; if(next_l == prev_l && strcmp(nex,pre)<=0) { s++; nex[next_l]='0'; nex[next_l+1]=0; next_l++; przepisznextnaprev(); continue; } if(strncmp(nex,pre,next_l)==0 && !same9nakoncuprev(prev_l-next_l)) { s+=prev_l-next_l; for(int j=0;j<next_l;j++)pre[j]=nex[j]; if(pre == buf1) {pre=buf2;nex=buf1;} else {nex=buf2;pre=buf1;} next_l=prev_l; dodaj1doprev(); continue; } if(strncmp(nex,pre,next_l)>0) { s+=prev_l-next_l; //for(int j=next_l;j<prev_l;j++)nex[j]='0'; memset(nex+next_l,'0',prev_l-next_l); next_l=prev_l; nex[next_l]=0; przepisznextnaprev(); continue; } s+=prev_l-next_l+1; // for(int j=next_l;j<=prev_l;j++)nex[j]='0'; memset(nex+next_l,'0',prev_l-next_l+1); next_l=prev_l+1; nex[next_l]=0; przepisznextnaprev(); } printf("%lld\n",s); 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 | #include <iostream> #include <stdio.h> #include <cmath> #include <cstring> using namespace std; char buf1[200100]; char buf2[200100]; char *pre = buf1; int prev_l; char *nex=buf2; int next_l; void wypisz(int i) { /* printf("%lld",a[i]); for(int j=0;j<m[i]-1;j++)printf("0"); if(m[i]>0)printf("%lld",d[i]); printf("\n"); */ } long long ilecyfr(long long a) { long long r=0; while(a>0) { r++; a/=10; } return r; } /* bool gt(int i, int j) { long long a1=a[i]; long long m1=m[i]; long long d1=d[i]; long long a2=a[j]; long long m2=m[j]; long long d2=d[j]; // printf("gt("); // wypisz(i); // wypisz(j); // printf(") = "); if(ilecyfr(a1) + m1 == ilecyfr(a2) + m2) { if (ilecyfr(a2)>ilecyfr(a1)) { m1 -= ilecyfr(a2)-ilecyfr(a1); a1 *= pow(10,ilecyfr(a2)-ilecyfr(a1)); } else { m2 -= ilecyfr(a1)-ilecyfr(a2); a2 *= pow(10,ilecyfr(a1)-ilecyfr(a2)); } if(m1==m2 && m1==0) { // printf("%d//1\n",a1+d1>a2+d2); return a1+d1>a2+d2; } if(m1==m2 && m1>0) { // printf("%d//2\n",(a1*10)+d1>(a2*10)+d2); return (a1*10)+d1>(a2*10)+d2; } // printf("ERROR\n"); } // printf("%d//3\n",(ilecyfr(a1) + m1 > ilecyfr(a2) + m2)); return (ilecyfr(a1) + m1 > ilecyfr(a2) + m2); } */ void przepisznextnaprev() { //printf("nex = %s\n",nex); //strcpy(pre,nex); if(pre == buf1) {pre=buf2;nex=buf1;} else {nex=buf2;pre=buf1;} prev_l=next_l; }; bool same9nakoncuprev(int i) { for(int j=prev_l-i;j<prev_l;j++)if(pre[j]!='9')return false; return true; }; void dodaj1doprev() { int i=prev_l-1; while(i>0 && pre[i]=='9') { pre[i]='0'; i--; } pre[i]++; }; int main() { // your code goes here int N; long long s=0; scanf("%d",&N); scanf("%s",pre); prev_l=strlen(pre); for(int i=1;i<N;i++) { scanf("%s",nex); next_l=strlen(nex); //printf("%s(%d) <-> %s(%d)\n",pre,prev_l,nex,next_l); if(next_l>prev_l) {przepisznextnaprev();continue;}; if(next_l == prev_l && strcmp(nex,pre)>0){przepisznextnaprev();continue;}; if(next_l == prev_l && strcmp(nex,pre)<=0) { s++; nex[next_l]='0'; nex[next_l+1]=0; next_l++; przepisznextnaprev(); continue; } if(strncmp(nex,pre,next_l)==0 && !same9nakoncuprev(prev_l-next_l)) { s+=prev_l-next_l; for(int j=0;j<next_l;j++)pre[j]=nex[j]; if(pre == buf1) {pre=buf2;nex=buf1;} else {nex=buf2;pre=buf1;} next_l=prev_l; dodaj1doprev(); continue; } if(strncmp(nex,pre,next_l)>0) { s+=prev_l-next_l; //for(int j=next_l;j<prev_l;j++)nex[j]='0'; memset(nex+next_l,'0',prev_l-next_l); next_l=prev_l; nex[next_l]=0; przepisznextnaprev(); continue; } s+=prev_l-next_l+1; // for(int j=next_l;j<=prev_l;j++)nex[j]='0'; memset(nex+next_l,'0',prev_l-next_l+1); next_l=prev_l+1; nex[next_l]=0; przepisznextnaprev(); } printf("%lld\n",s); return 0; } |