#include<bits/stdc++.h>
#define getchar getchar_unlocked
using namespace std;
typedef long long ll;
const int N=200000, C=17;
int n;
int dl, zera, pref;
char l[C], buf[C];
int readUI(int *x)
{
*x = 0;
int ile=0;
char c = getchar();
while(c > '9' || c < '0')
c = getchar();
while(c >= '0' && c <= '9')
{
buf[ile] = c;
++ile;
*x = (*x)*10 + (c-'0');
c = getchar();
}
return ile;
}
int comp(int dl)//czy buf > l
{
for(int i=0; i < dl; ++i)
{
if(buf[i] < l[i])return -1;
if(buf[i] > l[i])return 1;
}
return 0;
}
void ustaw(int dl)
{
pref = dl;
for(int i=0; i < C; ++i)
l[i] = '0';
for(int i=0; i < dl; ++i)
l[i] = buf[i];
}
bool dodaj()
{
int u = min(dl, C);
--u;
int r=1;
for(int i=u; i >= 0; --i)
{
r += (l[i] - '0');
l[i] = char('0' + r % 10);
r /= 10;
}
if(r)return 1;
return 0;
}
void wypisz()
{
for(int i=0; i < pref; ++i)
{
putchar(l[i]);
}
//int li = C - pref;
for(int i=1; i <= dl-C; ++i)
putchar('0');
for(int i=pref; i < min(C, dl); ++i)
putchar(l[i]);
putchar('\n');
}
int main()
{
ll wynik=0;
readUI(&n);
//cout << n << endl;
for(int i=0; i < n; ++i)
{
int x;
int y = readUI(&x);
//cout << x << " " << y << endl;
if(y > dl)
{
dl = y;
pref = y;
ustaw(y);
continue;
}
if(y < dl)
{
wynik += dl - y;
}
int z = comp(y);
if(z == 1){
ustaw(y);
}//ustaw liczbe i wymien na 0
else if(z == 0){
bool u = dodaj();
if(u || comp(y) != 0){
++wynik;
++dl;
ustaw(y);
}
}//dodawanie pisemne
else{
++wynik;
++dl;
ustaw(y);
}//wydluz liczbe, ustaw prefiks i 0
//wypisz();
}
cout << wynik;
}
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 | #include<bits/stdc++.h> #define getchar getchar_unlocked using namespace std; typedef long long ll; const int N=200000, C=17; int n; int dl, zera, pref; char l[C], buf[C]; int readUI(int *x) { *x = 0; int ile=0; char c = getchar(); while(c > '9' || c < '0') c = getchar(); while(c >= '0' && c <= '9') { buf[ile] = c; ++ile; *x = (*x)*10 + (c-'0'); c = getchar(); } return ile; } int comp(int dl)//czy buf > l { for(int i=0; i < dl; ++i) { if(buf[i] < l[i])return -1; if(buf[i] > l[i])return 1; } return 0; } void ustaw(int dl) { pref = dl; for(int i=0; i < C; ++i) l[i] = '0'; for(int i=0; i < dl; ++i) l[i] = buf[i]; } bool dodaj() { int u = min(dl, C); --u; int r=1; for(int i=u; i >= 0; --i) { r += (l[i] - '0'); l[i] = char('0' + r % 10); r /= 10; } if(r)return 1; return 0; } void wypisz() { for(int i=0; i < pref; ++i) { putchar(l[i]); } //int li = C - pref; for(int i=1; i <= dl-C; ++i) putchar('0'); for(int i=pref; i < min(C, dl); ++i) putchar(l[i]); putchar('\n'); } int main() { ll wynik=0; readUI(&n); //cout << n << endl; for(int i=0; i < n; ++i) { int x; int y = readUI(&x); //cout << x << " " << y << endl; if(y > dl) { dl = y; pref = y; ustaw(y); continue; } if(y < dl) { wynik += dl - y; } int z = comp(y); if(z == 1){ ustaw(y); }//ustaw liczbe i wymien na 0 else if(z == 0){ bool u = dodaj(); if(u || comp(y) != 0){ ++wynik; ++dl; ustaw(y); } }//dodawanie pisemne else{ ++wynik; ++dl; ustaw(y); }//wydluz liczbe, ustaw prefiks i 0 //wypisz(); } cout << wynik; } |
English