/* VNIMANIYE! Etot fail rashiryet prototipy. Zapreshaetsa ispolzovat sovmestno s drugimi failami. */ process.stdin.resume(); process.stdin.setEncoding('utf8'); Array.prototype.$ = function (f) { return f(this) } Array.prototype.sort1 = function (f) { this.sort(f); return this } function etoKusochekBaytika(kus) { return kus.length !== 0 && kus.replace(/0|1/g, '').length == 0 } function monetki (n, nominals) { function monetki_recur(n, nominals) { return nominals.map(function (nom) { if (nom < n) { return monetki(n - nom, nominals).map(function (tail) { return [nom].concat(tail); }) } else if (nom == n) { return [[nom]] } else { return []; } }).reduce(function (r, a) { return r.concat(a) }, []) } function mprocess(monetki) { var dict = {}, res = [], i monetki.forEach(function (m) { m.sort(function (x,y) { return y-x}) dict[m] = m }) for(i in dict) { res.push(dict[i]) } res.sort(function (x,y) { return y.length - x.length}) return res } return mprocess(monetki_recur(n, nominals)) } function sdelatBaytiki(kusochki) { var baitiki = [] , kluchiki = function (slovar) { var kl=[], i; for (i in slovar) { kl.push(i) }; return kl; } , stopochki = kusochki.reduce(function (stopki, kus) { stopki[kus.length] = (stopki[kus.length] || []).concat([kus]) return stopki }, {}) , ostalos = function (stopki) { return kluchiki(stopki).reduce(function (x,y) { return x+stopki[y].length }, 0) } , try_match = function (stopki, schemka) { var rollback = function (baitik) { baitik.forEach(function (kus) { stopki[kus.length].push(kus) }) } return schemka.reduce(function (baitik, ks) { if (baitik === null) return null if (stopki[ks] && stopki[ks].length > 0) { return [stopki[ks].pop()].concat(baitik) } else { rollback(baitik); return null; } }, []) } , schemki = monetki(8, kluchiki(stopochki).map(Number)) , schemka = schemki.pop() , baitik = null; while (schemka && ostalos(stopochki) > 0) { while(schemka && !(baitik = try_match(stopochki, schemka))) { schemka = schemki.pop() } if (baitik !== null) baitiki.push(baitik) } // PROVERKA if (ostalos(stopochki) == 0) { console.log('VSE HOROSHO') } else { console.log('VNIMANIYE, OSHIBKA') console.log(stopochki) console.log(monetki(8, kluchiki(stopochki).map(Number))) } return baitiki } function pechatatBaytik(bayt) { process.stdout.write(bayt.join(' + ') + '\n') } function mult50(arr) { var res = [] for (var i = 0; i < 25; ++i) res = res.concat(arr); return res; } function count01(arr) { var zs = 0, os = 0; for (var i = 0; i < arr.length; ++i) { var s = arr[i]; for (var j = 0; j < s.length; ++j) { if (s[j] == '0') ++zs; else ++os; } } return [zs, os]; } function print01(arr) { var res = count01(arr); console.log('0s: ' + res[0] + ' 1s: ' + res[1]); return arr; } function print01arr(arr) { var res = [0, 0]; for (var i = 0; i < arr.length; ++i) { var tmp = count01(arr[i]); res[0] += tmp[0]; res[1] += tmp[1]; } console.log('0s: ' + res[0] + ' 1s: ' + res[1]); return arr; } function sobratBaytiki(zemlya) { zemlya .split(/\s+/) .filter(etoKusochekBaytika) .$(mult50) .$(print01) .sort1(function (x, y) { return y.length - x.length }) .$(sdelatBaytiki) //.forEach(pechatatBaytik) .$(print01arr) } var buffer = "" process.stdin.on('data', function (chunk) { return buffer+=chunk}) process.stdin.on('end', function () { sobratBaytiki(buffer) })
11 01 1 1 0000 00 01 000 X 1010 111 010 0001 00 10 001 000 X 0 1100 0 010 X 10 00100 110 1011 000 1100 01101 1100 X 0000 01 1 0001 01 10 10 00100 110 1011
0s: 1625 1s: 1100 VNIMANIYE, OSHIBKA { '1': [ '1', '1', '0', '0', '1' ], '2': [], '3': [], '4': [], '5': [] } [ [ 1, 1, 1, 1, 1, 1, 1, 1 ], [ 2, 1, 1, 1, 1, 1, 1 ], [ 3, 1, 1, 1, 1, 1 ], [ 2, 2, 1, 1, 1, 1 ], [ 4, 1, 1, 1, 1 ], [ 3, 2, 1, 1, 1 ], [ 2, 2, 2, 1, 1 ], [ 5, 1, 1, 1 ], [ 4, 2, 1, 1 ], [ 3, 3, 1, 1 ], [ 3, 2, 2, 1 ], [ 2, 2, 2, 2 ], [ 5, 2, 1 ], [ 4, 3, 1 ], [ 4, 2, 2 ], [ 3, 3, 2 ], [ 5, 3 ], [ 4, 4 ] ] 0s: 1623 1s: 1097