|
| 1 | +// Rozwiązanie zadania 'Trzy wieże' z II etapu XXII OI. |
| 2 | +// Autor rozwiązania: Paweł Putra |
| 3 | +// Złożoność czasowa: O(n) |
| 4 | +// Złożoność pamięciowa: O(n) |
| 5 | +// Punkty: 100 (upsolve) |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | +#include <cstdint> |
| 9 | +#include <string> |
| 10 | +#include <cassert> |
| 11 | +#define dbg(x) #x << " = " << x << " " |
| 12 | +using namespace std; |
| 13 | +int map(char c) { |
| 14 | + if (c == 'C') return 0; |
| 15 | + if (c == 'B') return 1; |
| 16 | + if (c == 'S') return 2; |
| 17 | + assert(false); |
| 18 | + return -1; |
| 19 | +} |
| 20 | +constexpr int MAXN = 1'000'005, KOLORY = 3; |
| 21 | +int pref[MAXN][KOLORY]; |
| 22 | +int suma(int l, int r, int tab) { |
| 23 | + return pref[r][tab] - pref[l-1][tab]; |
| 24 | +} |
| 25 | + |
| 26 | +bool rozne(int x, int y) { |
| 27 | + return x != y || x == 0 || y == 0; |
| 28 | +} |
| 29 | + |
| 30 | +bool dobry(int l, int r) { |
| 31 | + int a = suma(l, r, 0); |
| 32 | + int b = suma(l, r, 1); |
| 33 | + int c = suma(l, r, 2); |
| 34 | + return rozne(a, b) && rozne(a, c) && rozne(b, c); |
| 35 | +} |
| 36 | + |
| 37 | +int res; |
| 38 | +void maksuj(int i, int j) { |
| 39 | + if (j - i + 1 > res && dobry(i, j)) { |
| 40 | + res = j - i + 1; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +int32_t main() { |
| 45 | + ios_base::sync_with_stdio(0); |
| 46 | + int n; |
| 47 | + string s; |
| 48 | + |
| 49 | + cin >> n >> s; |
| 50 | + |
| 51 | + for (int i = 1; i <= n; i++) { |
| 52 | + for (int j = 0; j < KOLORY; j++) { |
| 53 | + pref[i][j] = pref[i - 1][j]; |
| 54 | + } |
| 55 | + |
| 56 | + pref[i][map(s[i-1])]++; |
| 57 | + } |
| 58 | + |
| 59 | + for (int i = 1; i <= 5; i++) { |
| 60 | + for (int j = i; j <= n; j++) { |
| 61 | + maksuj(i, j); |
| 62 | + maksuj(n-j+1, n-i+1); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + cout << res << "\n"; |
| 67 | +} |
0 commit comments