#include <bits/stdc++.h> #ifndef ONLINE_JUDGE # pragma GCC diagnostic warning "-Wall" # pragma GCC diagnostic warning "-Wextra" # pragma GCC diagnostic warning "-Wformat=2" # pragma GCC diagnostic warning "-Wnull-dereference" # pragma GCC diagnostic warning "-Wduplicated-branches" # pragma GCC diagnostic warning "-Wduplicated-cond" # pragma GCC diagnostic warning "-Wfloat-equal" # pragma GCC diagnostic warning "-Wshadow" # pragma GCC diagnostic warning "-Wconversion" # pragma GCC diagnostic warning "-Wlogical-op" # pragma GCC diagnostic warning "-Wvector-operation-performance" # pragma GCC diagnostic warning "-Wdisabled-optimization" # pragma GCC diagnostic warning "-Wunsafe-loop-optimizations" # pragma GCC diagnostic warning "-Wdouble-promotion" #endif #pragma GCC target "arch=ivybridge", "tune=ivybridge" #if defined(ONLINE_JUDGE) || !defined(__OPTIMIZE__) # pragma GCC optimize "Ofast", "inline", "unroll-loops", "ipa-pta", "no-rtti", \ "no-exceptions", "nothrow-opt", "strict-enums", "stdarg-opt", "tracer" # pragma GCC target "inline-all-stringops" #endif #define rangeof(c) (c).begin(), (c).end() using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string s; cin >> s; int zeroc = 0, onec = 0; for(char c: s) { if(c == '0') { zeroc++; } else if(c == '1') { onec++; } } zeroc -= n; onec -= 2 * n; if(zeroc < 0 || onec < 0) { cout << "NIE\n"; return 0; } /*set<pair<int, int>> bases; for(char i = 'a'; i <= 'z'; i++) { int zeroc = 0; int onec = 0; for(int j = 0; j < 5; j++) { if(i & (1u << j)) { onec++; } else { zeroc++; } } bases.insert({zeroc, onec}); cout << i << " " << zeroc << " " << onec << endl; } for(auto [a, b]: bases) { cout << a << " " << b << "\n"; } int k = 35; vector<vector<bool>> is(k, vector<bool>(k, false)); is[0][0] = true; for(int y = 0; y < k; y++) { for(int x = 0; x < k; x++) { for(auto [a, b]: bases) { if(x - a < 0 || y - b < 0) continue; if(is[x - a][y - b]) { is[x][y] = true; break; } } cout << is[x][y]; } cout << "\n"; }*/ auto is_ok = [&](int zeroc, int onec) { return zeroc >= onec / 4 && onec >= zeroc / 4; }; if(!is_ok(zeroc, onec)) { cout << "NIE\n"; return 0; } int const alphac = 'z' - 'a' + 1; vector<pair<int, int>> letters_bits(alphac, {0, 0}); for(char c = 'a'; c <= 'z'; c++) { for(int j = 0; j < 5; j++) { if(c & (1u << j)) { letters_bits[c - 'a'].second++; } else { letters_bits[c - 'a'].first++; } } } int seed = 69; for(int i = 0; i < n; i++) { for(char c_before = 'a'; c_before <= 'z'; c_before++) { int c = (2137 * seed + 421 + c_before - 'a') % alphac; if(is_ok(zeroc - letters_bits[c].first, onec - letters_bits[c].second)) { cout << (char) (c + 'a'); zeroc -= letters_bits[c].first; onec -= letters_bits[c].second; seed = c; break; } } } assert(zeroc == 0 && onec == 0); cout << "\n"; }
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 128 129 130 | #include <bits/stdc++.h> #ifndef ONLINE_JUDGE # pragma GCC diagnostic warning "-Wall" # pragma GCC diagnostic warning "-Wextra" # pragma GCC diagnostic warning "-Wformat=2" # pragma GCC diagnostic warning "-Wnull-dereference" # pragma GCC diagnostic warning "-Wduplicated-branches" # pragma GCC diagnostic warning "-Wduplicated-cond" # pragma GCC diagnostic warning "-Wfloat-equal" # pragma GCC diagnostic warning "-Wshadow" # pragma GCC diagnostic warning "-Wconversion" # pragma GCC diagnostic warning "-Wlogical-op" # pragma GCC diagnostic warning "-Wvector-operation-performance" # pragma GCC diagnostic warning "-Wdisabled-optimization" # pragma GCC diagnostic warning "-Wunsafe-loop-optimizations" # pragma GCC diagnostic warning "-Wdouble-promotion" #endif #pragma GCC target "arch=ivybridge", "tune=ivybridge" #if defined(ONLINE_JUDGE) || !defined(__OPTIMIZE__) # pragma GCC optimize "Ofast", "inline", "unroll-loops", "ipa-pta", "no-rtti", \ "no-exceptions", "nothrow-opt", "strict-enums", "stdarg-opt", "tracer" # pragma GCC target "inline-all-stringops" #endif #define rangeof(c) (c).begin(), (c).end() using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string s; cin >> s; int zeroc = 0, onec = 0; for(char c: s) { if(c == '0') { zeroc++; } else if(c == '1') { onec++; } } zeroc -= n; onec -= 2 * n; if(zeroc < 0 || onec < 0) { cout << "NIE\n"; return 0; } /*set<pair<int, int>> bases; for(char i = 'a'; i <= 'z'; i++) { int zeroc = 0; int onec = 0; for(int j = 0; j < 5; j++) { if(i & (1u << j)) { onec++; } else { zeroc++; } } bases.insert({zeroc, onec}); cout << i << " " << zeroc << " " << onec << endl; } for(auto [a, b]: bases) { cout << a << " " << b << "\n"; } int k = 35; vector<vector<bool>> is(k, vector<bool>(k, false)); is[0][0] = true; for(int y = 0; y < k; y++) { for(int x = 0; x < k; x++) { for(auto [a, b]: bases) { if(x - a < 0 || y - b < 0) continue; if(is[x - a][y - b]) { is[x][y] = true; break; } } cout << is[x][y]; } cout << "\n"; }*/ auto is_ok = [&](int zeroc, int onec) { return zeroc >= onec / 4 && onec >= zeroc / 4; }; if(!is_ok(zeroc, onec)) { cout << "NIE\n"; return 0; } int const alphac = 'z' - 'a' + 1; vector<pair<int, int>> letters_bits(alphac, {0, 0}); for(char c = 'a'; c <= 'z'; c++) { for(int j = 0; j < 5; j++) { if(c & (1u << j)) { letters_bits[c - 'a'].second++; } else { letters_bits[c - 'a'].first++; } } } int seed = 69; for(int i = 0; i < n; i++) { for(char c_before = 'a'; c_before <= 'z'; c_before++) { int c = (2137 * seed + 421 + c_before - 'a') % alphac; if(is_ok(zeroc - letters_bits[c].first, onec - letters_bits[c].second)) { cout << (char) (c + 'a'); zeroc -= letters_bits[c].first; onec -= letters_bits[c].second; seed = c; break; } } } assert(zeroc == 0 && onec == 0); cout << "\n"; } |