This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 ] ;
int nxt[ 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 && nxt[ lo ] <= i ) {
++ lo , -- sz ;
}
int x = ord[ lo ] ;
dp[ i ] = dp[ x ] + calc ( x + 1 , i ) + sr ;
ctr[ i ] = ctr[ x ] + 1 ;
while ( sz >= 2 && nxt[ hi - 1 ] >= ( nxt[ hi ] = get_fst ( ord[ hi ] , i ) ) ) {
-- hi , -- sz ;
}
if ( sz == 1 ) { nxt[ hi ] = get_fst ( ord[ hi ] , i ) ; }
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 ;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |