#include <bits/stdc++.h>
using namespace std;
#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>;
const int N = 5e5+10;
int n;
char s[N];
struct BIT {
int b[N];
void upd(int pos, int x) { while(pos < N) b[pos] += x, pos += pos&-pos; }
ll get(int pos, int r = 0) { while(pos) r += b[pos], pos -= pos&-pos; return r; }
}bit;
int vis[N];
vector<pair<int, int>> segs;
int l[N], r[N];
ll getcost() {
for(auto it : segs) {
l[it.se] = it.fi;
r[it.fi] = it.se;
l[it.fi] = r[it.se] = 0;
}
ll ret = 0;
for(int i = 1; i <= n + n; i++) {
if(l[i] == 0) {
ret += bit.get(r[i] - 1);
bit.upd(r[i], +1);
}
else bit.upd(i, -1);
}
return ret;
}
ll solve(int p, int q) {
segs.clear();
for(int i = 0; i < n + n; i++) vis[i] = 0;
segs.pb({p + 1, q + 1});
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) + 1, max(i, j) + 1});
matched++;
}
// for(auto it : segs)
// cout << it.fi + 1 << " " << it.se + 1 << endl;
// cout << "cost = " << getcost() << endl;
return getcost();
}
vector<int> pos;
int main() {
ll ans = 0;
scanf("%lld %s", &n, s);
for(int i = 1; i < n + n; i++) if(s[0] != s[i])
pos.pb(i);
int lo = 0, hi = sz(pos) - 1;
while(hi - lo >= 10) {
int mid1 = (lo + lo + hi) / 3;
int mid2 = (lo + hi + hi) / 3;
ll f1 = solve(0, pos[mid1]);
ll f2 = solve(0, pos[mid2]);
if(f1 >= f2) hi = mid2 - 1;
else lo = mid1 + 1;
}
for(int i = lo; i <= hi; i++)
ans = max(ans, solve(0, pos[i]));
printf("%lld\n", ans);
return 0;
}
Compilation message
monochrome.cpp: In function 'int main()':
monochrome.cpp:84:13: warning: format '%lld' expects argument of type 'long long int*', but argument 2 has type 'int*' [-Wformat=]
84 | scanf("%lld %s", &n, s);
| ~~~^ ~~
| | |
| | int*
| long long int*
| %d
monochrome.cpp:84:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
84 | scanf("%lld %s", &n, s);
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
324 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 |
0 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
324 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 |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
324 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 |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
2 ms |
364 KB |
Output is correct |
34 |
Correct |
8 ms |
492 KB |
Output is correct |
35 |
Correct |
8 ms |
492 KB |
Output is correct |
36 |
Correct |
8 ms |
512 KB |
Output is correct |
37 |
Correct |
7 ms |
492 KB |
Output is correct |
38 |
Correct |
6 ms |
492 KB |
Output is correct |
39 |
Correct |
6 ms |
492 KB |
Output is correct |
40 |
Correct |
7 ms |
492 KB |
Output is correct |
41 |
Correct |
6 ms |
492 KB |
Output is correct |
42 |
Correct |
7 ms |
492 KB |
Output is correct |
43 |
Correct |
6 ms |
492 KB |
Output is correct |
44 |
Correct |
6 ms |
492 KB |
Output is correct |
45 |
Correct |
6 ms |
492 KB |
Output is correct |
46 |
Correct |
6 ms |
492 KB |
Output is correct |
47 |
Correct |
6 ms |
492 KB |
Output is correct |
48 |
Correct |
6 ms |
492 KB |
Output is correct |
49 |
Correct |
6 ms |
492 KB |
Output is correct |
50 |
Correct |
6 ms |
492 KB |
Output is correct |
51 |
Correct |
6 ms |
492 KB |
Output is correct |
52 |
Correct |
6 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
324 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 |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
2 ms |
364 KB |
Output is correct |
34 |
Correct |
8 ms |
492 KB |
Output is correct |
35 |
Correct |
8 ms |
492 KB |
Output is correct |
36 |
Correct |
8 ms |
512 KB |
Output is correct |
37 |
Correct |
7 ms |
492 KB |
Output is correct |
38 |
Correct |
6 ms |
492 KB |
Output is correct |
39 |
Correct |
6 ms |
492 KB |
Output is correct |
40 |
Correct |
7 ms |
492 KB |
Output is correct |
41 |
Correct |
6 ms |
492 KB |
Output is correct |
42 |
Correct |
7 ms |
492 KB |
Output is correct |
43 |
Correct |
6 ms |
492 KB |
Output is correct |
44 |
Correct |
6 ms |
492 KB |
Output is correct |
45 |
Correct |
6 ms |
492 KB |
Output is correct |
46 |
Correct |
6 ms |
492 KB |
Output is correct |
47 |
Correct |
6 ms |
492 KB |
Output is correct |
48 |
Correct |
6 ms |
492 KB |
Output is correct |
49 |
Correct |
6 ms |
492 KB |
Output is correct |
50 |
Correct |
6 ms |
492 KB |
Output is correct |
51 |
Correct |
6 ms |
492 KB |
Output is correct |
52 |
Correct |
6 ms |
492 KB |
Output is correct |
53 |
Correct |
1016 ms |
9312 KB |
Output is correct |
54 |
Correct |
1018 ms |
9332 KB |
Output is correct |
55 |
Correct |
959 ms |
8800 KB |
Output is correct |
56 |
Correct |
977 ms |
8928 KB |
Output is correct |
57 |
Correct |
780 ms |
8928 KB |
Output is correct |
58 |
Correct |
799 ms |
9184 KB |
Output is correct |
59 |
Correct |
746 ms |
8800 KB |
Output is correct |
60 |
Correct |
785 ms |
8928 KB |
Output is correct |
61 |
Correct |
800 ms |
9056 KB |
Output is correct |
62 |
Correct |
771 ms |
9056 KB |
Output is correct |
63 |
Correct |
837 ms |
9056 KB |
Output is correct |
64 |
Correct |
759 ms |
8948 KB |
Output is correct |
65 |
Correct |
751 ms |
8544 KB |
Output is correct |
66 |
Correct |
780 ms |
8928 KB |
Output is correct |
67 |
Correct |
776 ms |
8928 KB |
Output is correct |
68 |
Correct |
728 ms |
8600 KB |
Output is correct |
69 |
Correct |
801 ms |
9184 KB |
Output is correct |
70 |
Correct |
712 ms |
8288 KB |
Output is correct |
71 |
Correct |
767 ms |
8800 KB |
Output is correct |
72 |
Correct |
779 ms |
8800 KB |
Output is correct |
73 |
Correct |
795 ms |
9312 KB |
Output is correct |