-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path509C.cpp
115 lines (108 loc) · 2.96 KB
/
509C.cpp
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
// Rodrigo Farias de Macêdo
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define SZ(S) ((int) ((S).size()))
#define DEBUG(x) { cout << #x << " = " << x << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { REP(_,n) cout << a[_] << ' '; cout << endl; }
int n;
list<int> nb;
void trocaZero(bool x){
list<int>::iterator it2 = nb.begin();
if(x) it2++;
int i = 0;
for(list<int>::reverse_iterator it = nb.rbegin(); it != nb.rend(); ++it){
if(i > SZ(nb)/2) break;
if(*it==0 && *it2 != *it){
*it = *it2;
*it2 = 0;
}
i++;
}
}
void makenum(int sum){
int a = sum/9, b = sum%9;
if(b!=0) nb.push_back(b);
REP(i, a) nb.push_back(9);
EACH(it,nb)cout << *it ;
cout << endl;
}
void update(int dif){
if(dif > 0){
for (list<int>::reverse_iterator it = nb.rbegin(); it != nb.rend(); ++it){
if(9-*it > 0 && dif > 9-*it){
dif -= (9-*it);
*it = 9;
}else if(9-*it > 0 && dif <= 9-*it){
*it += dif;
dif = 0;
return;
}
}
while(dif != 0){
int a = dif/9, b = dif%9;
REP(j, a){ nb.push_front(9); dif-= 9;}
if(b!=0) nb.push_front(b); dif-= b;
return;
}
}
if(dif == 0){
int aux2 = -1;
for(list<int>::reverse_iterator it = nb.rbegin(); it != nb.rend(); ++it){
if(9 - *it > 0 && aux2 != 0 && it!= nb.rbegin()){
*it = *it+1;
--it;
*it = *it -1;
++it;
if(++it==nb.rend()) trocaZero(true);
return;
}
aux2= *it;
}
trocaZero(false);
EACH(it, nb){
if(*it > 0){
*it = *it-1;
break;
}
}
nb.push_front(1);
return;
}
if(dif < 0){
int difa = 0;
list<int>::reverse_iterator it;
for(it = nb.rbegin(); it != nb.rend(); ++it){
if(9 - *it > 0 && difa > abs(dif) ){
*it = *it +1;
if (difa+dif-1!=0) update(difa + dif-1);
return;
}else{ difa+=*it; *it = 0;}
}
if(it == nb.rend()){
EACH(it2,nb) *it2 = 0 ;
nb.push_front(1);
if(difa+dif-1 != 0) update(difa+dif-1);
}
}
}
int main() {
scanf("%d", &n);
int aux, last = 0;
scanf("%d", &aux);
makenum(aux);
n--;
while(n--){
last = aux;
scanf("%d", &aux);
update(aux-last);
EACH(it,nb)cout << *it ;
cout << endl;
}
return 0;
}