Skip to content

Commit a2d8b31

Browse files
committed
Dodano rozwiązanie zadania 'Trzy wieże' z XXII OI
1 parent dc29a43 commit a2d8b31

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
compile() {
4+
g++ -O3 -static -g $1.cpp -o $1
5+
}
6+
7+
MODEL=$1
8+
BRUTE=$2
9+
GEN=$3
10+
11+
trap 'echo STOP; exit 0' INT
12+
13+
run() {
14+
/usr/bin/time -f "seconds=%e\nkbytes=%M\nexitcode=%x" -o $1.time ./$1 < test.in > $1.out 2> $1.err
15+
}
16+
17+
info() {
18+
source $1.time
19+
echo -ne "(${1}: ${seconds}s, $((kbytes / 1024)) MB) "
20+
}
21+
22+
set -x
23+
compile $MODEL && compile $BRUTE && compile $GEN
24+
set +x
25+
26+
for i in $(seq 1 100000); do
27+
echo -n $i
28+
29+
./$GEN > test.in
30+
run $1
31+
run $2
32+
33+
cmp $1.out $2.out
34+
if [[ $? -ne 0 ]]; then
35+
echo -ne " [FAIL] "
36+
exit 1
37+
fi
38+
39+
echo -n " [OK] "
40+
info $1
41+
info $2
42+
echo ""
43+
44+
done
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Rozwiązanie powolne zadania 'Trzy wieże' z II etapu XXII OI.
2+
// Autor rozwiązania: Paweł Putra
3+
// Złożoność czasowa: O(n^2)
4+
// Złożoność pamięciowa: O(n)
5+
// Punkty: 60 (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+
int32_t main() {
38+
ios_base::sync_with_stdio(0);
39+
int n;
40+
string s;
41+
42+
cin >> n >> s;
43+
44+
for (int i = 1; i <= n; i++) {
45+
for (int j = 0; j < KOLORY; j++) {
46+
pref[i][j] = pref[i - 1][j];
47+
}
48+
49+
pref[i][map(s[i-1])]++;
50+
}
51+
52+
int res = 0;
53+
for (int i = 1; i <= n; i++) {
54+
for (int j = i; j <= n; j++) {
55+
if (j - i + 1 > res && dobry(i, j)) {
56+
res = j - i + 1;
57+
}
58+
}
59+
}
60+
61+
cout << res << "\n";
62+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Generatorka do zadania 'Trzy wieże' z II etapu XXII OI.
2+
3+
#include <random>
4+
#include <iostream>
5+
#include <chrono>
6+
using namespace std;
7+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
8+
int R(int l, int r) {
9+
return uniform_int_distribution<>(l, r)(rng);
10+
}
11+
12+
string alpha = "BSC";
13+
int main() {
14+
int n = R(1, 16);
15+
cout << n << endl;
16+
17+
for (int i = 1; i <= n; i++) {
18+
cout << alpha[R(0, 2)];
19+
}
20+
21+
cout << "\n";
22+
}

0 commit comments

Comments
 (0)