#include <cstdio> int bits(int v) { int r = 0; for (; v > 0; v >>= 1) { r += (v & 1); } return r; } int abs(int x) { if (x < 0) { return -x; } else { return x; } } // 3 -> a // 4 -> c // 5 -> g // 6 -> w char letters[] = "###acgw"; char str[1000000]; int choose(int ones, int N) { int min_diff = N << 3; int min = 0; for (int i = 3; i <= 6; ++i) { int diff = abs(i * N - ones); if (diff < min_diff) { min_diff = diff; min = i; } } return min; } int main() { int N, ones = 0; scanf("%d%s", &N, str); for (char *x = str; *x; ++x) { if (*x == '1') { ++ones; } } if (ones < 3 * N || ones > 6 * N) { printf("NIE\n"); } else { for (; N > 0; --N) { int chosen = choose(ones, N); ones -= chosen; printf("%c", letters[chosen], N); } printf("\n"); } return 0; }
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 | #include <cstdio> int bits(int v) { int r = 0; for (; v > 0; v >>= 1) { r += (v & 1); } return r; } int abs(int x) { if (x < 0) { return -x; } else { return x; } } // 3 -> a // 4 -> c // 5 -> g // 6 -> w char letters[] = "###acgw"; char str[1000000]; int choose(int ones, int N) { int min_diff = N << 3; int min = 0; for (int i = 3; i <= 6; ++i) { int diff = abs(i * N - ones); if (diff < min_diff) { min_diff = diff; min = i; } } return min; } int main() { int N, ones = 0; scanf("%d%s", &N, str); for (char *x = str; *x; ++x) { if (*x == '1') { ++ones; } } if (ones < 3 * N || ones > 6 * N) { printf("NIE\n"); } else { for (; N > 0; --N) { int chosen = choose(ones, N); ones -= chosen; printf("%c", letters[chosen], N); } printf("\n"); } return 0; } |