#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6;
const ll inf = 1e18;
char v[N*2];
int v2[N],v3[N];
ll ps[N + 1];
pair<ll,int> dp[N + 1];
int n,k;
int cnt = 0,cnt2 = 0;
struct line{
ll k,b;
int id;
};
bool check(line a,line b,line c){
return (a.b - b.b)*(b.k - c.k) <= (b.b - c.b)*(a.k - b.k);
}
struct CHT{
vector <line> cht;
int sz = 0, pt = 0;
void init(){
cht.clear();
sz = 0;
pt = 0;
}
void add(ll k,ll b,int id){
while(sz >= 2 && check(cht[sz - 2],cht[sz - 1],{k,b,id})){
cht.pop_back();
sz--;
}
cht.push_back({k,b,id});
pt = min(pt,sz - 1);
sz++;
}
pair<ll,int> get(int x){
assert(sz != 0);
pt = max(pt,0);
while(pt < sz - 1 && x > -(cht[pt].b - cht[pt + 1].b)/(cht[pt].k - cht[pt + 1].k)){
pt++;
}
return {cht[pt].k*x + cht[pt].b,cht[pt].id};
}
};
CHT cht;
pair <ll,int> check(ll x){
dp[0] = {0,0};
cht.init();
int pt = 0;
for(int i = 1;i <= n;i++){
dp[i] = {inf,0};
while(pt < i && max(v2[pt],pt) < i){
cht.add(-pt,dp[pt].first + x - 1ll*max(v2[pt],pt)*(n - pt) + ps[max(v2[pt],pt)],dp[pt].second);
pt++;
}
if(pt != i){
dp[i] = min(dp[i],{dp[pt].first + x,dp[pt].second + 1});
}
if(cht.sz != 0){
auto id = cht.get(i);
dp[i] = min(dp[i],{id.first + 1ll*i*n - ps[i],id.second + 1});
}
}
return dp[n];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>k;
for(int i = 0;i < 2*n;i++){
cin>>v[i];
if(v[i] == 'B'){
v2[cnt] = i - cnt;
cnt++;
}
if(v[i] == 'A'){
v3[cnt2] = i - cnt2;
cnt2++;
}
}
for(int i = 1;i <= n;i++){
ps[i] = ps[i - 1] + (n - v3[i - 1]);
}
ll l = 0,r = inf;
ll ans = 0;
while(l != r){
ll mij = (l + r)/2;
auto x = check(mij);
if(x.second >= k){
l = mij + 1;
}else{
r = mij;
}
}
check(l);
ans = dp[n].first - k*l;
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6596 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6596 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6488 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6636 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6596 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6488 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6636 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6636 KB |
Output is correct |
29 |
Correct |
6 ms |
7004 KB |
Output is correct |
30 |
Correct |
8 ms |
6744 KB |
Output is correct |
31 |
Correct |
8 ms |
6748 KB |
Output is correct |
32 |
Correct |
6 ms |
6748 KB |
Output is correct |
33 |
Correct |
6 ms |
6748 KB |
Output is correct |
34 |
Correct |
7 ms |
7004 KB |
Output is correct |
35 |
Correct |
6 ms |
7004 KB |
Output is correct |
36 |
Correct |
8 ms |
6744 KB |
Output is correct |
37 |
Correct |
8 ms |
6748 KB |
Output is correct |
38 |
Correct |
6 ms |
7004 KB |
Output is correct |
39 |
Correct |
7 ms |
6900 KB |
Output is correct |
40 |
Correct |
6 ms |
6748 KB |
Output is correct |
41 |
Correct |
6 ms |
6744 KB |
Output is correct |
42 |
Correct |
4 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6596 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6488 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6636 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6636 KB |
Output is correct |
29 |
Correct |
6 ms |
7004 KB |
Output is correct |
30 |
Correct |
8 ms |
6744 KB |
Output is correct |
31 |
Correct |
8 ms |
6748 KB |
Output is correct |
32 |
Correct |
6 ms |
6748 KB |
Output is correct |
33 |
Correct |
6 ms |
6748 KB |
Output is correct |
34 |
Correct |
7 ms |
7004 KB |
Output is correct |
35 |
Correct |
6 ms |
7004 KB |
Output is correct |
36 |
Correct |
8 ms |
6744 KB |
Output is correct |
37 |
Correct |
8 ms |
6748 KB |
Output is correct |
38 |
Correct |
6 ms |
7004 KB |
Output is correct |
39 |
Correct |
7 ms |
6900 KB |
Output is correct |
40 |
Correct |
6 ms |
6748 KB |
Output is correct |
41 |
Correct |
6 ms |
6744 KB |
Output is correct |
42 |
Correct |
4 ms |
6748 KB |
Output is correct |
43 |
Correct |
94 ms |
10080 KB |
Output is correct |
44 |
Correct |
159 ms |
10608 KB |
Output is correct |
45 |
Correct |
167 ms |
11212 KB |
Output is correct |
46 |
Correct |
114 ms |
8796 KB |
Output is correct |
47 |
Correct |
103 ms |
11280 KB |
Output is correct |
48 |
Correct |
101 ms |
12744 KB |
Output is correct |
49 |
Correct |
99 ms |
12740 KB |
Output is correct |
50 |
Correct |
159 ms |
11212 KB |
Output is correct |
51 |
Correct |
163 ms |
11080 KB |
Output is correct |
52 |
Correct |
103 ms |
12160 KB |
Output is correct |
53 |
Correct |
99 ms |
12744 KB |
Output is correct |
54 |
Correct |
167 ms |
11208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6596 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6488 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6636 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6636 KB |
Output is correct |
29 |
Correct |
6 ms |
7004 KB |
Output is correct |
30 |
Correct |
8 ms |
6744 KB |
Output is correct |
31 |
Correct |
8 ms |
6748 KB |
Output is correct |
32 |
Correct |
6 ms |
6748 KB |
Output is correct |
33 |
Correct |
6 ms |
6748 KB |
Output is correct |
34 |
Correct |
7 ms |
7004 KB |
Output is correct |
35 |
Correct |
6 ms |
7004 KB |
Output is correct |
36 |
Correct |
8 ms |
6744 KB |
Output is correct |
37 |
Correct |
8 ms |
6748 KB |
Output is correct |
38 |
Correct |
6 ms |
7004 KB |
Output is correct |
39 |
Correct |
7 ms |
6900 KB |
Output is correct |
40 |
Correct |
6 ms |
6748 KB |
Output is correct |
41 |
Correct |
6 ms |
6744 KB |
Output is correct |
42 |
Correct |
4 ms |
6748 KB |
Output is correct |
43 |
Correct |
94 ms |
10080 KB |
Output is correct |
44 |
Correct |
159 ms |
10608 KB |
Output is correct |
45 |
Correct |
167 ms |
11212 KB |
Output is correct |
46 |
Correct |
114 ms |
8796 KB |
Output is correct |
47 |
Correct |
103 ms |
11280 KB |
Output is correct |
48 |
Correct |
101 ms |
12744 KB |
Output is correct |
49 |
Correct |
99 ms |
12740 KB |
Output is correct |
50 |
Correct |
159 ms |
11212 KB |
Output is correct |
51 |
Correct |
163 ms |
11080 KB |
Output is correct |
52 |
Correct |
103 ms |
12160 KB |
Output is correct |
53 |
Correct |
99 ms |
12744 KB |
Output is correct |
54 |
Correct |
167 ms |
11208 KB |
Output is correct |
55 |
Correct |
1012 ms |
37872 KB |
Output is correct |
56 |
Correct |
1278 ms |
58280 KB |
Output is correct |
57 |
Correct |
1614 ms |
46392 KB |
Output is correct |
58 |
Correct |
1106 ms |
33632 KB |
Output is correct |
59 |
Correct |
992 ms |
46016 KB |
Output is correct |
60 |
Correct |
928 ms |
58276 KB |
Output is correct |
61 |
Correct |
943 ms |
58364 KB |
Output is correct |
62 |
Correct |
1591 ms |
46012 KB |
Output is correct |
63 |
Correct |
1682 ms |
46136 KB |
Output is correct |
64 |
Correct |
937 ms |
58416 KB |
Output is correct |
65 |
Correct |
988 ms |
58272 KB |
Output is correct |
66 |
Correct |
1628 ms |
46008 KB |
Output is correct |
67 |
Correct |
980 ms |
58248 KB |
Output is correct |