#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define pb push_back
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define dbg(x) cerr << #x << " is " << x << "\n";
#define cum cerr << "came\n";
using ll = long long;
using ii = pair<ll,ll>;
using oset = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 4010;
int n;
char s[N];
struct event {
int x, y;
int type;
bool operator<(event o) const {
return x < o.x;
}
};
vector<event> e;
oset os;
int vis[N];
vector<pair<int, int>> segs;
ll getcost() {
e.clear();
for(auto it : segs) {
e.pb({it.fi, it.se, 0});
e.pb({it.se, it.fi, 1});
} sort(all(e));
ll ret = 0;
for(auto i : e) {
if(i.type == 0) {
ret += os.order_of_key(i.y);
os.insert(i.y);
}
else {
os.erase(i.x);
}
}
os.clear();
return ret;
}
ll solve(int p, int q) {
segs.clear();
for(int i = 0; i < n + n; i++) vis[i] = 0;
segs.pb({p, q});
vis[p] = vis[q] = 1;
int matched = 1;
int i = p, j = q;
while(matched < n) {
while(vis[i] || s[i] != s[p]) {
i++;
if(i == n + n) i = 0;
}
while(s[i] == s[j] || vis[j]) {
j++;
if(j == n + n) j = 0;
}
vis[j] = vis[i] = 1;
segs.pb({min(i, j), max(i, j)});
matched++;
}
// for(auto it : segs)
// cout << it.fi + 1 << " " << it.se + 1 << endl;
// cout << "cost = " << getcost() << endl;
return getcost();
}
int main() {
ll ans = 0;
scanf("%d %s", &n, s);
for(int i = 1; i < n + n; i++) if(s[0] != s[i]) {
ans = max(ans, solve(0, i));
// cout << "/////////\n";
}
printf("%lld\n", ans);
return 0;
}
Compilation message
monochrome.cpp: In function 'int main()':
monochrome.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
91 | scanf("%d %s", &n, s);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
29 ms |
364 KB |
Output is correct |
15 |
Correct |
30 ms |
404 KB |
Output is correct |
16 |
Correct |
29 ms |
384 KB |
Output is correct |
17 |
Correct |
30 ms |
364 KB |
Output is correct |
18 |
Correct |
21 ms |
364 KB |
Output is correct |
19 |
Correct |
24 ms |
364 KB |
Output is correct |
20 |
Correct |
29 ms |
364 KB |
Output is correct |
21 |
Correct |
26 ms |
364 KB |
Output is correct |
22 |
Correct |
22 ms |
364 KB |
Output is correct |
23 |
Correct |
12 ms |
364 KB |
Output is correct |
24 |
Correct |
24 ms |
404 KB |
Output is correct |
25 |
Correct |
20 ms |
364 KB |
Output is correct |
26 |
Correct |
23 ms |
364 KB |
Output is correct |
27 |
Correct |
25 ms |
364 KB |
Output is correct |
28 |
Correct |
29 ms |
384 KB |
Output is correct |
29 |
Correct |
22 ms |
364 KB |
Output is correct |
30 |
Correct |
22 ms |
364 KB |
Output is correct |
31 |
Correct |
29 ms |
364 KB |
Output is correct |
32 |
Correct |
29 ms |
364 KB |
Output is correct |
33 |
Correct |
24 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
29 ms |
364 KB |
Output is correct |
15 |
Correct |
30 ms |
404 KB |
Output is correct |
16 |
Correct |
29 ms |
384 KB |
Output is correct |
17 |
Correct |
30 ms |
364 KB |
Output is correct |
18 |
Correct |
21 ms |
364 KB |
Output is correct |
19 |
Correct |
24 ms |
364 KB |
Output is correct |
20 |
Correct |
29 ms |
364 KB |
Output is correct |
21 |
Correct |
26 ms |
364 KB |
Output is correct |
22 |
Correct |
22 ms |
364 KB |
Output is correct |
23 |
Correct |
12 ms |
364 KB |
Output is correct |
24 |
Correct |
24 ms |
404 KB |
Output is correct |
25 |
Correct |
20 ms |
364 KB |
Output is correct |
26 |
Correct |
23 ms |
364 KB |
Output is correct |
27 |
Correct |
25 ms |
364 KB |
Output is correct |
28 |
Correct |
29 ms |
384 KB |
Output is correct |
29 |
Correct |
22 ms |
364 KB |
Output is correct |
30 |
Correct |
22 ms |
364 KB |
Output is correct |
31 |
Correct |
29 ms |
364 KB |
Output is correct |
32 |
Correct |
29 ms |
364 KB |
Output is correct |
33 |
Correct |
24 ms |
364 KB |
Output is correct |
34 |
Correct |
1631 ms |
552 KB |
Output is correct |
35 |
Correct |
1657 ms |
636 KB |
Output is correct |
36 |
Correct |
1597 ms |
764 KB |
Output is correct |
37 |
Correct |
1444 ms |
640 KB |
Output is correct |
38 |
Correct |
1353 ms |
620 KB |
Output is correct |
39 |
Correct |
1510 ms |
620 KB |
Output is correct |
40 |
Correct |
1239 ms |
748 KB |
Output is correct |
41 |
Correct |
1196 ms |
652 KB |
Output is correct |
42 |
Correct |
1582 ms |
1148 KB |
Output is correct |
43 |
Correct |
1592 ms |
492 KB |
Output is correct |
44 |
Correct |
1303 ms |
660 KB |
Output is correct |
45 |
Correct |
1641 ms |
620 KB |
Output is correct |
46 |
Correct |
1693 ms |
492 KB |
Output is correct |
47 |
Correct |
1687 ms |
620 KB |
Output is correct |
48 |
Correct |
1640 ms |
748 KB |
Output is correct |
49 |
Correct |
1310 ms |
748 KB |
Output is correct |
50 |
Correct |
1652 ms |
512 KB |
Output is correct |
51 |
Correct |
1682 ms |
552 KB |
Output is correct |
52 |
Correct |
1438 ms |
644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
29 ms |
364 KB |
Output is correct |
15 |
Correct |
30 ms |
404 KB |
Output is correct |
16 |
Correct |
29 ms |
384 KB |
Output is correct |
17 |
Correct |
30 ms |
364 KB |
Output is correct |
18 |
Correct |
21 ms |
364 KB |
Output is correct |
19 |
Correct |
24 ms |
364 KB |
Output is correct |
20 |
Correct |
29 ms |
364 KB |
Output is correct |
21 |
Correct |
26 ms |
364 KB |
Output is correct |
22 |
Correct |
22 ms |
364 KB |
Output is correct |
23 |
Correct |
12 ms |
364 KB |
Output is correct |
24 |
Correct |
24 ms |
404 KB |
Output is correct |
25 |
Correct |
20 ms |
364 KB |
Output is correct |
26 |
Correct |
23 ms |
364 KB |
Output is correct |
27 |
Correct |
25 ms |
364 KB |
Output is correct |
28 |
Correct |
29 ms |
384 KB |
Output is correct |
29 |
Correct |
22 ms |
364 KB |
Output is correct |
30 |
Correct |
22 ms |
364 KB |
Output is correct |
31 |
Correct |
29 ms |
364 KB |
Output is correct |
32 |
Correct |
29 ms |
364 KB |
Output is correct |
33 |
Correct |
24 ms |
364 KB |
Output is correct |
34 |
Correct |
1631 ms |
552 KB |
Output is correct |
35 |
Correct |
1657 ms |
636 KB |
Output is correct |
36 |
Correct |
1597 ms |
764 KB |
Output is correct |
37 |
Correct |
1444 ms |
640 KB |
Output is correct |
38 |
Correct |
1353 ms |
620 KB |
Output is correct |
39 |
Correct |
1510 ms |
620 KB |
Output is correct |
40 |
Correct |
1239 ms |
748 KB |
Output is correct |
41 |
Correct |
1196 ms |
652 KB |
Output is correct |
42 |
Correct |
1582 ms |
1148 KB |
Output is correct |
43 |
Correct |
1592 ms |
492 KB |
Output is correct |
44 |
Correct |
1303 ms |
660 KB |
Output is correct |
45 |
Correct |
1641 ms |
620 KB |
Output is correct |
46 |
Correct |
1693 ms |
492 KB |
Output is correct |
47 |
Correct |
1687 ms |
620 KB |
Output is correct |
48 |
Correct |
1640 ms |
748 KB |
Output is correct |
49 |
Correct |
1310 ms |
748 KB |
Output is correct |
50 |
Correct |
1652 ms |
512 KB |
Output is correct |
51 |
Correct |
1682 ms |
552 KB |
Output is correct |
52 |
Correct |
1438 ms |
644 KB |
Output is correct |
53 |
Runtime error |
2 ms |
748 KB |
Execution killed with signal 11 |
54 |
Halted |
0 ms |
0 KB |
- |