#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define MottoHayaku ios::sync_with_stdio(false);cin.tie(0);
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N=4e5+5;
struct BIT
{
int a[N];
void init()
{
rep1(i,N-1) a[i]=0;
}
void upd(int x,int val)
{
for(;x<N;x+=x&-x) a[x]+=val;
}
int qry(int x)
{
int ret=0;
for(;x;x-=x&-x) ret+=a[x];
return ret;
}
} bit;
int n,to[N];
vector<int> a,b;
string s;
ll getans(int i)
{
i=(i%n+n)%n;
rep(j,n)
{
to[a[j]]=b[(j+i)%n];
to[b[(j+i)%n]]=a[j];
}
bit.init();
ll tot=0;
rep1(j,n*2)
{
if(to[j]<j) bit.upd(j,-1);
else
{
tot+=bit.qry(to[j]);
bit.upd(to[j],1);
}
}
return tot;
}
signed main()
{
MottoHayaku
cin>>n>>s;
//rep(i,n) s+="BW";
//shuffle(s.begin(),s.end(),rng);
//cout<<n<<"\n"<<s<<"\n";
rep(i,n*2)
{
if(s[i]=='B') a.pb(i+1);
else b.pb(i+1);
}
if(n<=2000)
{
ll ans=0;
rep(i,n) ans=max(ans,getans(i));
cout<<ans<<"\n";
return 0;
}
int l=rng()%n;
int r=l+n-1;
while(l<r)
{
int mid=l+r+1>>1;
ll x=getans(mid)-getans(mid-1);
if(x>0) l=mid;
else r=mid-1;
}
cout<<getans(l)<<"\n";
}
Compilation message
monochrome.cpp: In function 'int main()':
monochrome.cpp:83:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
83 | int mid=l+r+1>>1;
| ~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1776 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1776 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
21 ms |
1876 KB |
Output is correct |
15 |
Correct |
19 ms |
1872 KB |
Output is correct |
16 |
Correct |
21 ms |
1904 KB |
Output is correct |
17 |
Correct |
18 ms |
1904 KB |
Output is correct |
18 |
Correct |
15 ms |
1876 KB |
Output is correct |
19 |
Correct |
16 ms |
1900 KB |
Output is correct |
20 |
Correct |
16 ms |
1912 KB |
Output is correct |
21 |
Correct |
16 ms |
1876 KB |
Output is correct |
22 |
Correct |
18 ms |
1868 KB |
Output is correct |
23 |
Correct |
11 ms |
1904 KB |
Output is correct |
24 |
Correct |
20 ms |
1876 KB |
Output is correct |
25 |
Correct |
15 ms |
2004 KB |
Output is correct |
26 |
Correct |
15 ms |
1876 KB |
Output is correct |
27 |
Correct |
18 ms |
1872 KB |
Output is correct |
28 |
Correct |
17 ms |
1876 KB |
Output is correct |
29 |
Correct |
14 ms |
1908 KB |
Output is correct |
30 |
Correct |
15 ms |
1908 KB |
Output is correct |
31 |
Correct |
22 ms |
1908 KB |
Output is correct |
32 |
Correct |
18 ms |
1908 KB |
Output is correct |
33 |
Correct |
18 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1776 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
21 ms |
1876 KB |
Output is correct |
15 |
Correct |
19 ms |
1872 KB |
Output is correct |
16 |
Correct |
21 ms |
1904 KB |
Output is correct |
17 |
Correct |
18 ms |
1904 KB |
Output is correct |
18 |
Correct |
15 ms |
1876 KB |
Output is correct |
19 |
Correct |
16 ms |
1900 KB |
Output is correct |
20 |
Correct |
16 ms |
1912 KB |
Output is correct |
21 |
Correct |
16 ms |
1876 KB |
Output is correct |
22 |
Correct |
18 ms |
1868 KB |
Output is correct |
23 |
Correct |
11 ms |
1904 KB |
Output is correct |
24 |
Correct |
20 ms |
1876 KB |
Output is correct |
25 |
Correct |
15 ms |
2004 KB |
Output is correct |
26 |
Correct |
15 ms |
1876 KB |
Output is correct |
27 |
Correct |
18 ms |
1872 KB |
Output is correct |
28 |
Correct |
17 ms |
1876 KB |
Output is correct |
29 |
Correct |
14 ms |
1908 KB |
Output is correct |
30 |
Correct |
15 ms |
1908 KB |
Output is correct |
31 |
Correct |
22 ms |
1908 KB |
Output is correct |
32 |
Correct |
18 ms |
1908 KB |
Output is correct |
33 |
Correct |
18 ms |
1876 KB |
Output is correct |
34 |
Correct |
357 ms |
1928 KB |
Output is correct |
35 |
Correct |
314 ms |
1896 KB |
Output is correct |
36 |
Correct |
290 ms |
1928 KB |
Output is correct |
37 |
Correct |
303 ms |
2000 KB |
Output is correct |
38 |
Correct |
243 ms |
1928 KB |
Output is correct |
39 |
Correct |
276 ms |
1932 KB |
Output is correct |
40 |
Correct |
262 ms |
1920 KB |
Output is correct |
41 |
Correct |
226 ms |
1932 KB |
Output is correct |
42 |
Correct |
298 ms |
1928 KB |
Output is correct |
43 |
Correct |
284 ms |
1936 KB |
Output is correct |
44 |
Correct |
252 ms |
1920 KB |
Output is correct |
45 |
Correct |
309 ms |
1924 KB |
Output is correct |
46 |
Correct |
279 ms |
1928 KB |
Output is correct |
47 |
Correct |
281 ms |
1924 KB |
Output is correct |
48 |
Correct |
307 ms |
1932 KB |
Output is correct |
49 |
Correct |
239 ms |
1916 KB |
Output is correct |
50 |
Correct |
292 ms |
1924 KB |
Output is correct |
51 |
Correct |
306 ms |
1924 KB |
Output is correct |
52 |
Correct |
290 ms |
1932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1776 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
21 ms |
1876 KB |
Output is correct |
15 |
Correct |
19 ms |
1872 KB |
Output is correct |
16 |
Correct |
21 ms |
1904 KB |
Output is correct |
17 |
Correct |
18 ms |
1904 KB |
Output is correct |
18 |
Correct |
15 ms |
1876 KB |
Output is correct |
19 |
Correct |
16 ms |
1900 KB |
Output is correct |
20 |
Correct |
16 ms |
1912 KB |
Output is correct |
21 |
Correct |
16 ms |
1876 KB |
Output is correct |
22 |
Correct |
18 ms |
1868 KB |
Output is correct |
23 |
Correct |
11 ms |
1904 KB |
Output is correct |
24 |
Correct |
20 ms |
1876 KB |
Output is correct |
25 |
Correct |
15 ms |
2004 KB |
Output is correct |
26 |
Correct |
15 ms |
1876 KB |
Output is correct |
27 |
Correct |
18 ms |
1872 KB |
Output is correct |
28 |
Correct |
17 ms |
1876 KB |
Output is correct |
29 |
Correct |
14 ms |
1908 KB |
Output is correct |
30 |
Correct |
15 ms |
1908 KB |
Output is correct |
31 |
Correct |
22 ms |
1908 KB |
Output is correct |
32 |
Correct |
18 ms |
1908 KB |
Output is correct |
33 |
Correct |
18 ms |
1876 KB |
Output is correct |
34 |
Correct |
357 ms |
1928 KB |
Output is correct |
35 |
Correct |
314 ms |
1896 KB |
Output is correct |
36 |
Correct |
290 ms |
1928 KB |
Output is correct |
37 |
Correct |
303 ms |
2000 KB |
Output is correct |
38 |
Correct |
243 ms |
1928 KB |
Output is correct |
39 |
Correct |
276 ms |
1932 KB |
Output is correct |
40 |
Correct |
262 ms |
1920 KB |
Output is correct |
41 |
Correct |
226 ms |
1932 KB |
Output is correct |
42 |
Correct |
298 ms |
1928 KB |
Output is correct |
43 |
Correct |
284 ms |
1936 KB |
Output is correct |
44 |
Correct |
252 ms |
1920 KB |
Output is correct |
45 |
Correct |
309 ms |
1924 KB |
Output is correct |
46 |
Correct |
279 ms |
1928 KB |
Output is correct |
47 |
Correct |
281 ms |
1924 KB |
Output is correct |
48 |
Correct |
307 ms |
1932 KB |
Output is correct |
49 |
Correct |
239 ms |
1916 KB |
Output is correct |
50 |
Correct |
292 ms |
1924 KB |
Output is correct |
51 |
Correct |
306 ms |
1924 KB |
Output is correct |
52 |
Correct |
290 ms |
1932 KB |
Output is correct |
53 |
Correct |
396 ms |
5824 KB |
Output is correct |
54 |
Correct |
388 ms |
5920 KB |
Output is correct |
55 |
Correct |
403 ms |
5936 KB |
Output is correct |
56 |
Correct |
384 ms |
5976 KB |
Output is correct |
57 |
Correct |
348 ms |
5848 KB |
Output is correct |
58 |
Correct |
359 ms |
5940 KB |
Output is correct |
59 |
Correct |
321 ms |
5680 KB |
Output is correct |
60 |
Correct |
333 ms |
6044 KB |
Output is correct |
61 |
Correct |
348 ms |
6148 KB |
Output is correct |
62 |
Correct |
342 ms |
6112 KB |
Output is correct |
63 |
Correct |
339 ms |
5984 KB |
Output is correct |
64 |
Correct |
351 ms |
5732 KB |
Output is correct |
65 |
Correct |
333 ms |
5852 KB |
Output is correct |
66 |
Correct |
323 ms |
5732 KB |
Output is correct |
67 |
Correct |
333 ms |
5732 KB |
Output is correct |
68 |
Correct |
325 ms |
5504 KB |
Output is correct |
69 |
Correct |
345 ms |
5976 KB |
Output is correct |
70 |
Correct |
311 ms |
5592 KB |
Output is correct |
71 |
Correct |
369 ms |
5856 KB |
Output is correct |
72 |
Correct |
347 ms |
5860 KB |
Output is correct |
73 |
Correct |
371 ms |
6192 KB |
Output is correct |