fork download
  1. /* VNIMANIYE! Etot fail rashiryet prototipy. Zapreshaetsa ispolzovat sovmestno
  2. s drugimi failami. */
  3.  
  4. process.stdin.resume();
  5. process.stdin.setEncoding('utf8');
  6.  
  7. Array.prototype.$ = function (f) { return f(this) }
  8.  
  9. Array.prototype.sort1 = function (f) { this.sort(f); return this }
  10.  
  11. function etoKusochekBaytika(kus) { return kus.length !== 0 && kus.replace(/0|1/g, '').length == 0 }
  12.  
  13.  
  14. function monetki (n, nominals) {
  15.  
  16. function monetki_recur(n, nominals) {
  17. return nominals.map(function (nom) {
  18. if (nom < n) {
  19. return monetki(n - nom, nominals).map(function (tail) {
  20. return [nom].concat(tail);
  21. })
  22. } else if (nom == n) {
  23. return [[nom]]
  24. } else {
  25. return [];
  26. }
  27. }).reduce(function (r, a) { return r.concat(a) }, [])
  28. }
  29.  
  30. function mprocess(monetki) {
  31. var dict = {}, res = [], i
  32. monetki.forEach(function (m) {
  33. m.sort(function (x,y) { return y-x})
  34. dict[m] = m
  35. })
  36. for(i in dict) {
  37. res.push(dict[i])
  38. }
  39. res.sort(function (x,y) { return y.length - x.length})
  40. return res
  41. }
  42.  
  43. return mprocess(monetki_recur(n, nominals))
  44. }
  45.  
  46.  
  47.  
  48. function sdelatBaytiki(kusochki) {
  49. var baitiki = []
  50. , kluchiki = function (slovar) {
  51. var kl=[], i; for (i in slovar) { kl.push(i) }; return kl;
  52. }
  53. , stopochki = kusochki.reduce(function (stopki, kus) {
  54. stopki[kus.length] = (stopki[kus.length] || []).concat([kus])
  55. return stopki
  56. }, {})
  57. , ostalos = function (stopki) {
  58. return kluchiki(stopki).reduce(function (x,y) { return x+stopki[y].length }, 0)
  59. }
  60. , try_match = function (stopki, schemka) {
  61. var rollback = function (baitik) {
  62. baitik.forEach(function (kus) {
  63. stopki[kus.length].push(kus)
  64. })
  65. }
  66. return schemka.reduce(function (baitik, ks) {
  67. if (baitik === null)
  68. return null
  69. if (stopki[ks] && stopki[ks].length > 0) {
  70. return [stopki[ks].pop()].concat(baitik)
  71. } else {
  72. rollback(baitik);
  73. return null;
  74. }
  75. }, [])
  76.  
  77. }
  78. , schemki = monetki(8, kluchiki(stopochki).map(Number))
  79. , schemka = schemki.pop()
  80. , baitik = null;
  81.  
  82. while (schemka && ostalos(stopochki) > 0) {
  83. while(schemka && !(baitik = try_match(stopochki, schemka))) {
  84. schemka = schemki.pop()
  85. }
  86. if (baitik !== null)
  87. baitiki.push(baitik)
  88. }
  89.  
  90. // PROVERKA
  91. if (ostalos(stopochki) == 0) {
  92. console.log('VSE HOROSHO')
  93. } else {
  94. console.log('VNIMANIYE, OSHIBKA')
  95. console.log(stopochki)
  96. console.log(monetki(8, kluchiki(stopochki).map(Number)))
  97. }
  98.  
  99. return baitiki
  100. }
  101.  
  102. function pechatatBaytik(bayt) { process.stdout.write(bayt.join(' + ') + '\n') }
  103.  
  104. function mult50(arr) {
  105. var res = []
  106. for (var i = 0; i < 25; ++i) res = res.concat(arr);
  107. return res;
  108. }
  109.  
  110. function count01(arr) {
  111. var zs = 0, os = 0;
  112. for (var i = 0; i < arr.length; ++i) {
  113. var s = arr[i];
  114. for (var j = 0; j < s.length; ++j) {
  115. if (s[j] == '0') ++zs;
  116. else ++os;
  117. }
  118. }
  119. return [zs, os];
  120. }
  121.  
  122. function print01(arr) {
  123. var res = count01(arr);
  124. console.log('0s: ' + res[0] + ' 1s: ' + res[1]);
  125. return arr;
  126. }
  127.  
  128. function print01arr(arr) {
  129. var res = [0, 0];
  130. for (var i = 0; i < arr.length; ++i) {
  131. var tmp = count01(arr[i]);
  132. res[0] += tmp[0];
  133. res[1] += tmp[1];
  134. }
  135. console.log('0s: ' + res[0] + ' 1s: ' + res[1]);
  136. return arr;
  137. }
  138.  
  139. function sobratBaytiki(zemlya) {
  140. zemlya
  141. .split(/\s+/)
  142. .filter(etoKusochekBaytika)
  143. .$(mult50)
  144. .$(print01)
  145. .sort1(function (x, y) { return y.length - x.length })
  146. .$(sdelatBaytiki)
  147. //.forEach(pechatatBaytik)
  148. .$(print01arr)
  149. }
  150.  
  151. var buffer = ""
  152. process.stdin.on('data', function (chunk) { return buffer+=chunk})
  153. process.stdin.on('end', function () { sobratBaytiki(buffer) })
  154.  
Success #stdin #stdout 0.08s 50068KB
stdin
10010110 00110110 11110101 00001110 10010100 11101001 01011110 11000011 10011100 11001110 11001101 01111011 11111100 10111010 10100100 10101111 00001000 10010101 10101010 11101011 00000 10010 11000 11011 01111 01010 00011 10100 10100 11101 00101 01111 01100 00111 00111 00110 01110 11001 10101 11101 11111 11001 01110 10101 00100 10101 11100 00100 01001 01011 01010 10111 01011 01100 10011 00100 01110 01100 01010 00011 00011 00000 00010 00011 00000 10010 11000 11011 01111 01010 00011 10100 10100 11101 00101 01111 01100 00111 00111 00110 01110 11001 10101 11101 11111 11001 01110 10101 00100 10101 11100 00100 01001 01011 01010 10111 01011 00000 00000 00000 00000 00000 00000
stdout
0s: 7200 1s: 7175
VNIMANIYE, OSHIBKA
{
  '5': [
    '00000', '10010', '11000', '11011', '01111', '01010', '00011',
    '10100', '10100', '11101', '00101', '01111', '01100', '00111',
    '00111', '00110', '01110', '11001', '10101', '11101', '11111',
    '11001', '01110', '10101', '00100', '10101', '11100', '00100',
    '01001', '01011', '01010', '10111', '01011', '01100', '10011',
    '00100', '01110', '01100', '01010', '00011', '00011', '00000',
    '00010', '00011', '00000', '10010', '11000', '11011', '01111',
    '01010', '00011', '10100', '10100', '11101', '00101', '01111',
    '01100', '00111', '00111', '00110', '01110', '11001', '10101',
    '11101', '11111', '11001', '01110', '10101', '00100', '10101',
    '11100', '00100', '01001', '01011', '01010', '10111', '01011',
    '00000', '00000', '00000', '00000', '00000', '00000', '00000',
    '10010', '11000', '11011', '01111', '01010', '00011', '10100',
    '10100', '11101', '00101', '01111', '01100', '00111', '00111',
    '00110', '01110',
    ... 1975 more items
  ],
  '8': []
}
[ [ 8 ] ]
0s: 1775 1s: 2225