#include <bits/stdc++.h>
using namespace std;
int ans=0;
int n;
const int N=2*1e5+5;
string a[N];
bool is_greater(string x, string y)
{
if(x.size()>y.size())
return 1;
if(x.size()<y.size())
return 0;
for(int i=0; i<x.size(); ++i)
{
if(x[i]>y[i]) return 1;
if(x[i]<y[i]) return 0;
}
return 0;
}
bool is_greater(string x, string y, int xd)
{
for(int i=0; i<xd; ++i)
{
if(x[i]>y[i]) return 1;
if(x[i]<y[i]) return 0;
}
return 0;
}
bool is_equal(string x, string y, int xd)
{
for(int i=0; i<xd; ++i)
{
if(x[i]!=y[i]) return 0;
}
return 1;
}
bool is_equal(string x, int l, int p)
{
for(int i=l; i<p; ++i)
{
if(x[i]=='9') continue;
return 0;
}
return 1;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
cin>>n;
for(int i=0; i<n; ++i)
cin>>a[i];
for(int i=1; i<n; ++i)
{
if(is_greater(a[i], a[i-1])==0)
{
if(a[i].size()==a[i-1].size())
{
a[i].push_back('0');
ans++;
continue;
}
if(is_greater(a[i], a[i-1], a[i].size()))
{
while(a[i].size()<a[i-1].size())
{
a[i].push_back('0');
ans++;
}
continue;
}
if(is_equal(a[i], a[i-1], a[i].size()))
{
if(is_equal(a[i-1], a[i].size(), a[i-1].size()))
{
for(int j=a[i].size(); j<=a[i-1].size(); ++j)
{
a[i].push_back('0');
ans++;
}
continue;
}
for(int j=a[i].size(); j<a[i-1].size(); ++j)
{
a[i].push_back(a[i-1][j]);
ans++;
}
a[i][a[i].size()-1]++;
continue;
}
for(int j=a[i].size(); j<=a[i-1].size(); ++j)
{
a[i].push_back('0');
ans++;
}
continue;
}
}
cout<<ans;
/* for(int i=0; i<n; ++i)
{
cout<<a[i]<<"\n";
}*/
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 | #include <bits/stdc++.h> using namespace std; int ans=0; int n; const int N=2*1e5+5; string a[N]; bool is_greater(string x, string y) { if(x.size()>y.size()) return 1; if(x.size()<y.size()) return 0; for(int i=0; i<x.size(); ++i) { if(x[i]>y[i]) return 1; if(x[i]<y[i]) return 0; } return 0; } bool is_greater(string x, string y, int xd) { for(int i=0; i<xd; ++i) { if(x[i]>y[i]) return 1; if(x[i]<y[i]) return 0; } return 0; } bool is_equal(string x, string y, int xd) { for(int i=0; i<xd; ++i) { if(x[i]!=y[i]) return 0; } return 1; } bool is_equal(string x, int l, int p) { for(int i=l; i<p; ++i) { if(x[i]=='9') continue; return 0; } return 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin>>n; for(int i=0; i<n; ++i) cin>>a[i]; for(int i=1; i<n; ++i) { if(is_greater(a[i], a[i-1])==0) { if(a[i].size()==a[i-1].size()) { a[i].push_back('0'); ans++; continue; } if(is_greater(a[i], a[i-1], a[i].size())) { while(a[i].size()<a[i-1].size()) { a[i].push_back('0'); ans++; } continue; } if(is_equal(a[i], a[i-1], a[i].size())) { if(is_equal(a[i-1], a[i].size(), a[i-1].size())) { for(int j=a[i].size(); j<=a[i-1].size(); ++j) { a[i].push_back('0'); ans++; } continue; } for(int j=a[i].size(); j<a[i-1].size(); ++j) { a[i].push_back(a[i-1][j]); ans++; } a[i][a[i].size()-1]++; continue; } for(int j=a[i].size(); j<=a[i-1].size(); ++j) { a[i].push_back('0'); ans++; } continue; } } cout<<ans; /* for(int i=0; i<n; ++i) { cout<<a[i]<<"\n"; }*/ return 0; } |
English