#include <iostream> #include <string.h> typedef int32_t bool32; typedef int64_t bool64; /* struct Ant { int32_t pos; bool32 dir_is_right; uint32_t count; }; */ typedef uint8_t Cell_Type; enum { Cell_None = 0, Cell_Left, Cell_Right, }; struct Cell { Cell_Type type; int32_t id; }; void repr_board(uint64_t board_size, Cell *cells) { // Repr board for (uint64_t i = 0; i < board_size; ++i) { Cell *cell = &cells[i]; char repr = '.'; if (cell->type == Cell_Left) repr = 'L'; else if (cell->type == Cell_Right) repr = 'P'; std::cout << repr; } std::cout << "\n"; } int main(void) { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); uint64_t n = 0; std::cin >> n; if (!n) return 0; uint64_t padding = (n+10); uint64_t board_size = n + (2 * padding); uint64_t *counts = new uint64_t[n]; memset(counts, 0, n * sizeof(*counts)); int8_t *done = new int8_t[n]; memset(done, 0, n * sizeof(*done)); Cell *cells = new Cell[board_size]; memset(cells, 0, board_size * sizeof(*cells)); for (uint64_t i = 0; i < n; ++i) { char dir_symbol = 0; std::cin >> dir_symbol; Cell *cell = &cells[i + padding]; cell->type = (dir_symbol == 'P' ? Cell_Right : Cell_Left); cell->id = i; } // repr_board(board_size, cells); // test1: 6 -> 3 for (int i = 0; i < n; ++i) { memset(done, 0, n * sizeof(*done)); for (uint64_t i = 0; i < board_size; ++i) { Cell *cell = &cells[i]; if (cell->type == Cell_None) continue; if (i == 0 && cell->type == Cell_Left) { cell->type = Cell_None; continue; } if (i == board_size - 1 && cell->type == Cell_Right) { cell->type = Cell_None; continue; } if (done[cell->id]) continue; uint64_t next_i = cell->type == Cell_Right ? (i + 1) : (i - 1); Cell *next = &cells[next_i]; if (next->type != Cell_None && next->type != cell->type) { // NOTE(pkruszec): swap directions Cell_Type tmp = cell->type; cell->type = next->type; next->type = tmp; counts[cell->id]++; counts[next->id]++; done[cell->id] = true; done[next->id] = true; //next_i = cell->type == Cell_Right ? i + 1 : i - 1; //next = &cells[next_i]; } else if (next->type == Cell_None) { next->type = cell->type; next->id = cell->id; cell->type = Cell_None; cell->id = 0; done[next->id] = true; } } // repr_board(board_size, cells); } // Repr counts for (uint64_t i = 0; i < n; ++i) { std::cout << counts[i] << " "; } std::cout << "\n"; /* Ant *ants = new Ant[n]; // TODO(pkruszec): does new zero out the memory? // maybe replace with malloc memset(ants, 0, n * sizeof(*ants)); for (uint32_t i = 0; i < n; ++i) { Ant *ant = &ants[i]; char dir_symbol = 0; std::cin >> dir_symbol; ant->dir_is_right = (dir_symbol == 'P'); } for (uint32_t i = 0; i < n; ++i) { Ant *ant = &ants[i]; } */ 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 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | #include <iostream> #include <string.h> typedef int32_t bool32; typedef int64_t bool64; /* struct Ant { int32_t pos; bool32 dir_is_right; uint32_t count; }; */ typedef uint8_t Cell_Type; enum { Cell_None = 0, Cell_Left, Cell_Right, }; struct Cell { Cell_Type type; int32_t id; }; void repr_board(uint64_t board_size, Cell *cells) { // Repr board for (uint64_t i = 0; i < board_size; ++i) { Cell *cell = &cells[i]; char repr = '.'; if (cell->type == Cell_Left) repr = 'L'; else if (cell->type == Cell_Right) repr = 'P'; std::cout << repr; } std::cout << "\n"; } int main(void) { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); uint64_t n = 0; std::cin >> n; if (!n) return 0; uint64_t padding = (n+10); uint64_t board_size = n + (2 * padding); uint64_t *counts = new uint64_t[n]; memset(counts, 0, n * sizeof(*counts)); int8_t *done = new int8_t[n]; memset(done, 0, n * sizeof(*done)); Cell *cells = new Cell[board_size]; memset(cells, 0, board_size * sizeof(*cells)); for (uint64_t i = 0; i < n; ++i) { char dir_symbol = 0; std::cin >> dir_symbol; Cell *cell = &cells[i + padding]; cell->type = (dir_symbol == 'P' ? Cell_Right : Cell_Left); cell->id = i; } // repr_board(board_size, cells); // test1: 6 -> 3 for (int i = 0; i < n; ++i) { memset(done, 0, n * sizeof(*done)); for (uint64_t i = 0; i < board_size; ++i) { Cell *cell = &cells[i]; if (cell->type == Cell_None) continue; if (i == 0 && cell->type == Cell_Left) { cell->type = Cell_None; continue; } if (i == board_size - 1 && cell->type == Cell_Right) { cell->type = Cell_None; continue; } if (done[cell->id]) continue; uint64_t next_i = cell->type == Cell_Right ? (i + 1) : (i - 1); Cell *next = &cells[next_i]; if (next->type != Cell_None && next->type != cell->type) { // NOTE(pkruszec): swap directions Cell_Type tmp = cell->type; cell->type = next->type; next->type = tmp; counts[cell->id]++; counts[next->id]++; done[cell->id] = true; done[next->id] = true; //next_i = cell->type == Cell_Right ? i + 1 : i - 1; //next = &cells[next_i]; } else if (next->type == Cell_None) { next->type = cell->type; next->id = cell->id; cell->type = Cell_None; cell->id = 0; done[next->id] = true; } } // repr_board(board_size, cells); } // Repr counts for (uint64_t i = 0; i < n; ++i) { std::cout << counts[i] << " "; } std::cout << "\n"; /* Ant *ants = new Ant[n]; // TODO(pkruszec): does new zero out the memory? // maybe replace with malloc memset(ants, 0, n * sizeof(*ants)); for (uint32_t i = 0; i < n; ++i) { Ant *ant = &ants[i]; char dir_symbol = 0; std::cin >> dir_symbol; ant->dir_is_right = (dir_symbol == 'P'); } for (uint32_t i = 0; i < n; ++i) { Ant *ant = &ants[i]; } */ return 0; } |