#include <stdio.h> #include <string> int main() { int n,a; scanf("%d",&n); long long res=0; scanf("%d",&a); char buf[200004]; std::string prev; sprintf(buf,"%d",a); prev =buf; int zera =0; for (int i=1;i<n;i++) { //printf("%s %d\n",prev.c_str(), zera); scanf("%d",&a); std::string curr; sprintf(buf, "%d",a); curr = buf; if(curr.size() > prev.size()+zera) { prev = std::move(curr); zera =0; continue; } long long prevVal = 0, mn =1; for (int i =curr.size()-1;i>=0;i--) { prevVal += mn * ((i<=prev.size()-1)? (prev[i]-'0'):0); mn *= 10; } if(prevVal < a || prevVal > a) { long long diff = prev.size() +zera - curr.size() + (prevVal > a); res += diff; prev= std:: move(curr); zera = diff; continue; } if(zera != 0) { long long diff = prev.size() +zera - curr.size(); prev = prev + std::string(zera -1,'0'); prev += '1'; zera = 0; res +=diff; continue; } int index = -1; for (int i =prev.size()-1;i>=curr.size();i--) if(prev[i] != '9') { index = i; break; } if(index == -1) { long long diff = prev.size() - curr.size() + 1; res += diff; prev= std:: move(curr); zera = diff; } else { long long diff = prev.size() - curr.size(); res += diff; prev[index] ++; for (int j=index+1;j<prev.size();j++) prev[j]='0'; } } printf("%lld\n",res); 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 | #include <stdio.h> #include <string> int main() { int n,a; scanf("%d",&n); long long res=0; scanf("%d",&a); char buf[200004]; std::string prev; sprintf(buf,"%d",a); prev =buf; int zera =0; for (int i=1;i<n;i++) { //printf("%s %d\n",prev.c_str(), zera); scanf("%d",&a); std::string curr; sprintf(buf, "%d",a); curr = buf; if(curr.size() > prev.size()+zera) { prev = std::move(curr); zera =0; continue; } long long prevVal = 0, mn =1; for (int i =curr.size()-1;i>=0;i--) { prevVal += mn * ((i<=prev.size()-1)? (prev[i]-'0'):0); mn *= 10; } if(prevVal < a || prevVal > a) { long long diff = prev.size() +zera - curr.size() + (prevVal > a); res += diff; prev= std:: move(curr); zera = diff; continue; } if(zera != 0) { long long diff = prev.size() +zera - curr.size(); prev = prev + std::string(zera -1,'0'); prev += '1'; zera = 0; res +=diff; continue; } int index = -1; for (int i =prev.size()-1;i>=curr.size();i--) if(prev[i] != '9') { index = i; break; } if(index == -1) { long long diff = prev.size() - curr.size() + 1; res += diff; prev= std:: move(curr); zera = diff; } else { long long diff = prev.size() - curr.size(); res += diff; prev[index] ++; for (int j=index+1;j<prev.size();j++) prev[j]='0'; } } printf("%lld\n",res); return 0; } |