Advertisement
a53

lego

a53
Jun 8th, 2019
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. int s[25], f[15], f1[15], n1[25], n2[25], gasit, ok, rez1[25], rez2[25];
  7.  
  8. void read(FILE *fin) {
  9. char c;
  10. int i, x, cf;
  11.  
  12. fscanf(fin, "%c", &c);
  13. while(c >= '0' && c <= '9') {
  14. s[0]++;
  15. s[s[0]] = c - '0';
  16. fscanf(fin, "%c", &c);
  17. }
  18. for(i = 1; i <= s[0] / 2; i++)
  19. swap(s[i], s[s[0] - i + 1]);
  20. while(!feof(fin)) {
  21. fscanf(fin, "%d %d ", &x, &cf);
  22. f[cf] += x;
  23. n1[0] += x;
  24. }
  25. n2[0] = n1[0];
  26. }
  27.  
  28. void print_huge(int a[], FILE *fout) {
  29. for(int i = a[0]; i >= 1; i--)
  30. fprintf(fout, "%d", a[i]);
  31. }
  32.  
  33. int cmp(int a[], int b[]) {
  34. if(a[0] > b[0])
  35. return 1;
  36. if(a[0] < b[0])
  37. return -1;
  38. for(int i = a[0]; i >= 1; i--)
  39. if(a[i] > b[i])
  40. return 1;
  41. else if(a[i] < b[i])
  42. return -1;
  43. return 0;
  44. }
  45.  
  46. void solve1() {
  47. for(int k = n1[0]; k >= 1; k--) {
  48. int j = 9;
  49. while(j >= 0 + (k == n1[0]) && !f[j])
  50. j--;
  51. n1[k] = j;
  52. f[j]--;
  53. }
  54. n2[0] = 0;
  55. }
  56.  
  57. void solve2() {
  58. for(int k = n2[0]; k >= 1; k--) {
  59. int j = 0 + (k == n2[0]);
  60. while(j <= 9 && !f[j])
  61. j++;
  62. n2[k] = j;
  63. f[j]--;
  64. }
  65. n1[0] = 0;
  66. }
  67.  
  68. void set1(int k) {
  69. if(k >= 1) {
  70. if(!ok) {
  71. if(!gasit && f[s[k]]) {
  72. n1[k] = s[k];
  73. f[s[k]]--;
  74. set1(k - 1);
  75. f[s[k]]++;
  76. }
  77. if(!gasit) { /// cautam cea mai mare cf mai mica decat s[k]
  78. int j = s[k] - 1;
  79. while(j >= 0 + (k == n1[0]) && !f[j])
  80. j--;
  81. if(j >= 0 + (k == n1[0])) {
  82. n1[k] = j;
  83. f[j]--;
  84. ok = 1;
  85. set1(k - 1);
  86. f[j]++;
  87. ok = 0;
  88. }
  89. }
  90. }
  91. else {
  92. int j = 9;
  93. while(j >= 0 + (k == n1[0]) && !f[j])
  94. j--;
  95. if(j >= 0 + (k == n1[0])) {
  96. n1[k] = j;
  97. f[j]--;
  98. set1(k - 1);
  99. f[j]++;
  100. }
  101. }
  102. }
  103. else {
  104. int x = cmp(n1, s);
  105. if(x == -1)
  106. gasit = 1;
  107. }
  108. }
  109.  
  110. void set2(int k) {
  111. if(k >= 1) {
  112. if(!ok) {
  113. if(!gasit && f1[s[k]]) {
  114. n2[k] = s[k];
  115. f1[s[k]]--;
  116. set2(k - 1);
  117. f1[s[k]]++;
  118. }
  119. if(!gasit) { /// cautam cea mai mare cf mai mica decat s[k]
  120. int j = s[k] + 1;
  121. while(j <= 9 && !f1[j])
  122. j++;
  123. if(j <= 9) {
  124. n2[k] = j;
  125. f1[j]--;
  126. ok = 1;
  127. set2(k - 1);
  128. f1[j]++;
  129. ok = 0;
  130. }
  131. }
  132. }
  133. else {
  134. int j = 0 + (k == n2[0]);
  135. while(j <= 9 && !f1[j])
  136. j++;
  137. if(j <= 9) {
  138. n2[k] = j;
  139. f1[j]--;
  140. set2(k - 1);
  141. f1[j]++;
  142. }
  143. }
  144. }
  145. else {
  146. int x = cmp(n2, s);
  147. if(x == 1)
  148. gasit = 1;
  149. }
  150. }
  151.  
  152. void subtract(int a[], int b[], int c[]) {/// c = a - b
  153. for(int i = b[0] + 1; i <= a[0]; i++)
  154. b[i] = 0;
  155. b[0] = a[0];
  156. c[0] = a[0];
  157. int r = 0;
  158. for(int i = 1; i <= a[0]; i++) {
  159. c[i] = a[i] - b[i] - r;
  160. if(c[i] < 0) {
  161. r = 1;
  162. c[i] += 10;
  163. }
  164. else
  165. r = 0;
  166. }
  167. while(c[c[0]] == 0)
  168. c[0]--;
  169. }
  170.  
  171. int main() {
  172. FILE *fin = fopen("lego.in", "r");
  173. FILE *fout = fopen("lego.out", "w");
  174. int cr, i;
  175.  
  176. fscanf(fin, "%d ", &cr);
  177. read(fin);
  178. if(n1[0] < s[0])
  179. solve1();
  180. else if(n1[0] > s[0])
  181. solve2();
  182. else {
  183. gasit = ok = 0;
  184. for(i = 0; i <= 9; i++)
  185. f1[i] = f[i];
  186. set1(n1[0]);
  187. if(!gasit)
  188. n1[0] = 0;
  189. gasit = ok = 0;
  190. set2(n2[0]);
  191. if(!gasit)
  192. n2[0] = 0;
  193. }
  194. if(cr == 1) {
  195. if(n1[0] == 0)
  196. fprintf(fout, "0 ");
  197. else
  198. print_huge(n1, fout);
  199. fprintf(fout, " ");
  200. if(n2[0] == 0)
  201. fprintf(fout, "0");
  202. else
  203. print_huge(n2, fout);
  204. }
  205. else {
  206. if(n1[0] != 0)
  207. subtract(s, n1, rez1);
  208. else
  209. rez1[0] = 0;
  210. if(n2[0] != 0)
  211. subtract(n2, s, rez2);
  212. else
  213. rez2[0] = 0;
  214. if(n1[0] == 0)
  215. if(n2[0] == 0)
  216. fprintf(fout, "0");
  217. else
  218. print_huge(rez2, fout);
  219. else
  220. if(n2[0] == 0)
  221. print_huge(rez1, fout);
  222. else {
  223. int x = cmp(rez1, rez2);
  224. if(x <= 0)
  225. print_huge(rez1, fout);
  226. else
  227. print_huge(rez2, fout);
  228. }
  229. }
  230.  
  231. return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement