#include <bits/stdc++.h> using namespace std; #define pi pair<int,int> #define BOOST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define X first #define Y second const int N = 8e5 + 7; pi dp[N*8 + 69]; map<int,char> v = {{3, 'a'}, {4, 'c'}, {5, 's'}, {6, 'w'}}; //czy da sie wydać x k nominałami bool ok(int x, int k){ if (x == 0){ return k == 0; } if (x < 0) return false; return dp[x].X <= k and k <= dp[x].Y; } int main(){ BOOST; int n; string s; cin >> n >> s; int x = count(s.begin(), s.end(), '1'); // int su = n + x; // debug(x); const int INF = 2e9; fill(dp, dp + N, make_pair(INF, -1)); dp[0] = {0, 0}; for (int i = 0; i <= x; i++){ if (dp[i].X == INF) continue; for (int j : {3,4,5,6}){ dp[i + j].X = min(dp[i + j].X, dp[i].X + 1); dp[i + j].Y = max(dp[i + j].Y, dp[i].Y + 1); } } int cur = x; string ans = ""; for (int len = n; len; len--){ bool hailed = false; for (int c : {3,4,5,6}){ if (ok(cur - c, len - 1)){ hailed = true; cur -= c; ans += v[c]; break; } } if (!hailed){ cout << "NIE\n"; return 0; } } cout << ans << "\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 | #include <bits/stdc++.h> using namespace std; #define pi pair<int,int> #define BOOST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define X first #define Y second const int N = 8e5 + 7; pi dp[N*8 + 69]; map<int,char> v = {{3, 'a'}, {4, 'c'}, {5, 's'}, {6, 'w'}}; //czy da sie wydać x k nominałami bool ok(int x, int k){ if (x == 0){ return k == 0; } if (x < 0) return false; return dp[x].X <= k and k <= dp[x].Y; } int main(){ BOOST; int n; string s; cin >> n >> s; int x = count(s.begin(), s.end(), '1'); // int su = n + x; // debug(x); const int INF = 2e9; fill(dp, dp + N, make_pair(INF, -1)); dp[0] = {0, 0}; for (int i = 0; i <= x; i++){ if (dp[i].X == INF) continue; for (int j : {3,4,5,6}){ dp[i + j].X = min(dp[i + j].X, dp[i].X + 1); dp[i + j].Y = max(dp[i + j].Y, dp[i].Y + 1); } } int cur = x; string ans = ""; for (int len = n; len; len--){ bool hailed = false; for (int c : {3,4,5,6}){ if (ok(cur - c, len - 1)){ hailed = true; cur -= c; ans += v[c]; break; } } if (!hailed){ cout << "NIE\n"; return 0; } } cout << ans << "\n"; } |