#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector < int > vi ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
const int MAXN = 1e6 + 7 ;
int n , k ;
int a[ MAXN ] , b[ MAXN ] ;
int lw[ MAXN ] , ori[ MAXN ] ;
ll pref[ MAXN ] ;
inline ll calc ( int l , int r ) {
if ( l > r ) { return 0 ; }
if ( r <= ori[ l ] ) { return 0 ; }
int aux_st = max ( ori[ l ] , l - 1 ) ;
return ( pref[ r ] - pref[ aux_st ] ) - 1LL * ( r - aux_st ) * ( l - 1 ) ;
}
ll dp[ MAXN ] ;
int ctr[ MAXN ] ;
inline int get_fst ( int x , int y ) {
int l , r , mid ;
l = max ( x , y ) , r = n ;
if ( make_pair ( dp[ x ] + calc ( x + 1 , n ) , ctr[ x ] ) < make_pair ( dp[ y ] + calc ( y + 1 , n ) , ctr[ y ] ) ) { return n + 1 ; }
while ( l < r ) {
mid = ( l + r ) / 2 ;
if ( make_pair ( dp[ x ] + calc ( x + 1 , mid ) , ctr[ x ] ) < make_pair ( dp[ y ] + calc ( y + 1 , mid ) , ctr[ y ] ) ) { l = mid + 1 ; }
else { r = mid ; }
}
return l ;
}
int ord[ MAXN ] ;
pair < ll , int > f ( ll sr ) {
int lo = 1 , hi = 1 ;
ord[ lo ] = 0 ;
int sz = 1 ;
for ( int i = 1 ; i <= n ; ++ i ) {
while ( sz >= 2 && get_fst ( ord[ lo ] , ord[ lo + 1 ] ) <= i ) {
++ lo , -- sz ;
}
int x = ord[ lo ] ;
dp[ i ] = dp[ x ] + calc ( x + 1 , i ) + sr ;
ctr[ i ] = ctr[ x ] + 1 ;
while ( sz >= 2 && get_fst ( ord[ hi - 1 ] , ord[ hi ] ) >= get_fst ( ord[ hi ] , i ) ) {
-- hi , -- sz ;
}
ord[ ++ hi ] = i ;
++ sz ;
}
return { dp[ n ] , ctr[ n ] } ;
}
void solve ( ) {
cin >> n >> k ;
string s ; cin >> s ;
int pos1 = 0 , pos2 = 0 ;
for ( int i = 1 ; i <= 2 * n ; ++ i ) {
if ( s[ i - 1 ] == 'A' ) { a[ ++ pos1 ] = i ; }
else { b[ ++ pos2 ] = i ; }
}
int tp = 0 ;
for ( int i = 1 ; i <= n ; ++ i ) {
while ( tp < n && b[ tp + 1 ] < a[ i ] ) { ++ tp ; }
lw[ i ] = tp ;
pref[ i ] = pref[ i - 1 ] + lw[ i ] ;
}
pref[ n + 1 ] = pref[ n ] ;
tp = 0 ;
for ( int i = 1 ; i <= n ; ++ i ) {
while ( tp < n && lw[ tp + 1 ] < i ) { ++ tp ; }
ori[ i ] = tp ;
}
ll l , r , mid ;
l = -1 , r = ( 1LL << 32 ) ;
while ( l < r ) {
mid = ( l + r ) / 2 ;
auto [ ret , cnt ] = f ( mid ) ;
if ( cnt > k ) { l = mid + 1 ; }
else {
if ( mid == r ) { break ; }
r = mid ;
}
}
auto [ ret , cnt ] = f ( r ) ;
cout << ret - k * r << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10584 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10624 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10584 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10624 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
4 ms |
10588 KB |
Output is correct |
19 |
Correct |
4 ms |
10584 KB |
Output is correct |
20 |
Correct |
2 ms |
10584 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
4 ms |
10588 KB |
Output is correct |
25 |
Correct |
4 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10732 KB |
Output is correct |
27 |
Correct |
3 ms |
10584 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10584 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10624 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
4 ms |
10588 KB |
Output is correct |
19 |
Correct |
4 ms |
10584 KB |
Output is correct |
20 |
Correct |
2 ms |
10584 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
4 ms |
10588 KB |
Output is correct |
25 |
Correct |
4 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10732 KB |
Output is correct |
27 |
Correct |
3 ms |
10584 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
30 ms |
10840 KB |
Output is correct |
30 |
Correct |
38 ms |
10844 KB |
Output is correct |
31 |
Correct |
38 ms |
10840 KB |
Output is correct |
32 |
Correct |
14 ms |
10840 KB |
Output is correct |
33 |
Correct |
32 ms |
11100 KB |
Output is correct |
34 |
Correct |
29 ms |
10840 KB |
Output is correct |
35 |
Correct |
32 ms |
10844 KB |
Output is correct |
36 |
Correct |
40 ms |
10848 KB |
Output is correct |
37 |
Correct |
38 ms |
10844 KB |
Output is correct |
38 |
Correct |
30 ms |
10844 KB |
Output is correct |
39 |
Correct |
33 ms |
10852 KB |
Output is correct |
40 |
Correct |
31 ms |
10844 KB |
Output is correct |
41 |
Correct |
32 ms |
10844 KB |
Output is correct |
42 |
Correct |
18 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10584 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10624 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
4 ms |
10588 KB |
Output is correct |
19 |
Correct |
4 ms |
10584 KB |
Output is correct |
20 |
Correct |
2 ms |
10584 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
4 ms |
10588 KB |
Output is correct |
25 |
Correct |
4 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10732 KB |
Output is correct |
27 |
Correct |
3 ms |
10584 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
30 ms |
10840 KB |
Output is correct |
30 |
Correct |
38 ms |
10844 KB |
Output is correct |
31 |
Correct |
38 ms |
10840 KB |
Output is correct |
32 |
Correct |
14 ms |
10840 KB |
Output is correct |
33 |
Correct |
32 ms |
11100 KB |
Output is correct |
34 |
Correct |
29 ms |
10840 KB |
Output is correct |
35 |
Correct |
32 ms |
10844 KB |
Output is correct |
36 |
Correct |
40 ms |
10848 KB |
Output is correct |
37 |
Correct |
38 ms |
10844 KB |
Output is correct |
38 |
Correct |
30 ms |
10844 KB |
Output is correct |
39 |
Correct |
33 ms |
10852 KB |
Output is correct |
40 |
Correct |
31 ms |
10844 KB |
Output is correct |
41 |
Correct |
32 ms |
10844 KB |
Output is correct |
42 |
Correct |
18 ms |
10844 KB |
Output is correct |
43 |
Correct |
672 ms |
12156 KB |
Output is correct |
44 |
Correct |
1241 ms |
17120 KB |
Output is correct |
45 |
Correct |
1244 ms |
16860 KB |
Output is correct |
46 |
Correct |
352 ms |
17036 KB |
Output is correct |
47 |
Correct |
790 ms |
16868 KB |
Output is correct |
48 |
Correct |
864 ms |
18904 KB |
Output is correct |
49 |
Correct |
874 ms |
18912 KB |
Output is correct |
50 |
Correct |
1216 ms |
16924 KB |
Output is correct |
51 |
Correct |
1325 ms |
16872 KB |
Output is correct |
52 |
Correct |
912 ms |
18992 KB |
Output is correct |
53 |
Correct |
877 ms |
19164 KB |
Output is correct |
54 |
Correct |
1222 ms |
19052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10584 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10624 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
4 ms |
10588 KB |
Output is correct |
19 |
Correct |
4 ms |
10584 KB |
Output is correct |
20 |
Correct |
2 ms |
10584 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
4 ms |
10588 KB |
Output is correct |
25 |
Correct |
4 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10732 KB |
Output is correct |
27 |
Correct |
3 ms |
10584 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
30 ms |
10840 KB |
Output is correct |
30 |
Correct |
38 ms |
10844 KB |
Output is correct |
31 |
Correct |
38 ms |
10840 KB |
Output is correct |
32 |
Correct |
14 ms |
10840 KB |
Output is correct |
33 |
Correct |
32 ms |
11100 KB |
Output is correct |
34 |
Correct |
29 ms |
10840 KB |
Output is correct |
35 |
Correct |
32 ms |
10844 KB |
Output is correct |
36 |
Correct |
40 ms |
10848 KB |
Output is correct |
37 |
Correct |
38 ms |
10844 KB |
Output is correct |
38 |
Correct |
30 ms |
10844 KB |
Output is correct |
39 |
Correct |
33 ms |
10852 KB |
Output is correct |
40 |
Correct |
31 ms |
10844 KB |
Output is correct |
41 |
Correct |
32 ms |
10844 KB |
Output is correct |
42 |
Correct |
18 ms |
10844 KB |
Output is correct |
43 |
Correct |
672 ms |
12156 KB |
Output is correct |
44 |
Correct |
1241 ms |
17120 KB |
Output is correct |
45 |
Correct |
1244 ms |
16860 KB |
Output is correct |
46 |
Correct |
352 ms |
17036 KB |
Output is correct |
47 |
Correct |
790 ms |
16868 KB |
Output is correct |
48 |
Correct |
864 ms |
18904 KB |
Output is correct |
49 |
Correct |
874 ms |
18912 KB |
Output is correct |
50 |
Correct |
1216 ms |
16924 KB |
Output is correct |
51 |
Correct |
1325 ms |
16872 KB |
Output is correct |
52 |
Correct |
912 ms |
18992 KB |
Output is correct |
53 |
Correct |
877 ms |
19164 KB |
Output is correct |
54 |
Correct |
1222 ms |
19052 KB |
Output is correct |
55 |
Execution timed out |
7025 ms |
36680 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |