#include<bits/stdc++.h>
using namespace std ;
#define maxn 200009
#define ll long long
#define fi first
#define se second
#define pb push_back
#define left id<<1
#define right id<<1|1
#define re exit(0);
const int mod = 1e9+7 ;
const int INF = 1e9 ;
const int LOG = 18 ;
typedef vector<int> vi ;
typedef vector<ll> vl ;
typedef pair<int,int> pii ;
typedef vector<pii> vii ;
typedef pair<ll,ll> pll ;
void add ( int &a , int b )
{
a += b ;
if ( a < 0 ) a += mod ;
if ( a >= mod ) a -= mod ;
}
template < typename T > void chkmin (T &a , T b) { if (a>b) a=b ;}
template < typename T > void chkmax (T &a , T b) { if (a<b) a=b ;}
void rf ()
{
freopen ("bai1.inp","r",stdin) ;
// freopen ("bai1.out","w",stdout) ;
}
int _pow ( int a , int n )
{
if ( n == 0 ) return 1 ;
int res = _pow(a,n/2) ;
if ( n % 2 ) return (1ll*res*res%mod*a%mod) ;
else return 1ll*res*res%mod ;
}
int n , k ;
int a [maxn] ;
int b [maxn] ;
int res = 0 ;
void init_array ()
{
int deg = 0 , cnt = 0 , num = 0 ;
for ( int i = 1 ; i <= n*2 ; i ++ )
{
if ( a [i] == 0 )
{
b [++num] = a [i] ; deg ++ ; res += cnt ;
if ( cnt > 0 && deg > 0 ) b [++num] = 1 , cnt -- , deg -- ;
}
else
{
if ( deg > 0 ) b [++num] = a [i] , deg -- ;
else cnt ++ ;
}
}
while ( cnt > 0 ) b[++num] = 1 , cnt -- ;
}
int point [5009][5009] ;
int sum [5009][5009] ;
int dp [5009][5009] ;
void sub1 ()
{
int x = 0 , y = 0 ;
for ( int i = 1 ; i <= n*2 ; i ++ )
{
if ( b [i] ) y ++ ;
else
{
for ( int r = 0 ; r <= y ; r ++ ) point [x][r] = 1 ;
x ++ ;
}
}
for ( int i = 0 ; i <= n ; i ++ )
{
for ( int j = n ; j >= 0 ; j -- )
{
sum [i][j] = sum [i][j+1] + point [i][j] ;
if ( i > 0 ) sum [i][j] = sum [i][j] + sum [i-1][j] - sum [i-1][j+1] ;
}
}
memset ( dp , 0x3f , sizeof dp ) ;
dp [0][0] = 0 ;
for ( int i = 1 ; i <= n ; i ++ )
{
for ( int j = 0 ; j < i ; j ++ )
{
int cost = sum [i-1][j+1] ;
if ( i == j+1 ) cost = 0 ;
for ( int t = 1 ; t <= k ; t ++ ) chkmin (dp[i][t],dp[j][t-1]+cost) ;
}
}
int Min = INF ;
for ( int i = 0 ; i <= k ; i ++ ) chkmin (Min,dp[n][i]) ;
cout << res + Min ;
}
int main ()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ; cout.tie(0) ;
// rf () ;
cin >> n >> k ;
for ( int i = 1 ; i <= n*2 ; i ++ )
{
char c ; cin >> c ;
a [i] = c - 'A' ;
}
init_array () ;
if ( n <= 500 ) sub1 () ;
}
Compilation message
chorus.cpp: In function 'void rf()':
chorus.cpp:34:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen ("bai1.inp","r",stdin) ;
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
102736 KB |
Output is correct |
2 |
Correct |
12 ms |
102748 KB |
Output is correct |
3 |
Correct |
12 ms |
102596 KB |
Output is correct |
4 |
Correct |
12 ms |
102748 KB |
Output is correct |
5 |
Correct |
12 ms |
102748 KB |
Output is correct |
6 |
Correct |
14 ms |
102744 KB |
Output is correct |
7 |
Correct |
12 ms |
102748 KB |
Output is correct |
8 |
Correct |
12 ms |
102704 KB |
Output is correct |
9 |
Correct |
12 ms |
102748 KB |
Output is correct |
10 |
Correct |
13 ms |
102744 KB |
Output is correct |
11 |
Correct |
13 ms |
102748 KB |
Output is correct |
12 |
Correct |
12 ms |
102624 KB |
Output is correct |
13 |
Correct |
12 ms |
102724 KB |
Output is correct |
14 |
Correct |
13 ms |
102676 KB |
Output is correct |
15 |
Correct |
13 ms |
102748 KB |
Output is correct |
16 |
Correct |
13 ms |
102748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
102736 KB |
Output is correct |
2 |
Correct |
12 ms |
102748 KB |
Output is correct |
3 |
Correct |
12 ms |
102596 KB |
Output is correct |
4 |
Correct |
12 ms |
102748 KB |
Output is correct |
5 |
Correct |
12 ms |
102748 KB |
Output is correct |
6 |
Correct |
14 ms |
102744 KB |
Output is correct |
7 |
Correct |
12 ms |
102748 KB |
Output is correct |
8 |
Correct |
12 ms |
102704 KB |
Output is correct |
9 |
Correct |
12 ms |
102748 KB |
Output is correct |
10 |
Correct |
13 ms |
102744 KB |
Output is correct |
11 |
Correct |
13 ms |
102748 KB |
Output is correct |
12 |
Correct |
12 ms |
102624 KB |
Output is correct |
13 |
Correct |
12 ms |
102724 KB |
Output is correct |
14 |
Correct |
13 ms |
102676 KB |
Output is correct |
15 |
Correct |
13 ms |
102748 KB |
Output is correct |
16 |
Correct |
13 ms |
102748 KB |
Output is correct |
17 |
Correct |
15 ms |
107612 KB |
Output is correct |
18 |
Correct |
25 ms |
115804 KB |
Output is correct |
19 |
Correct |
41 ms |
115800 KB |
Output is correct |
20 |
Correct |
16 ms |
115800 KB |
Output is correct |
21 |
Correct |
16 ms |
115804 KB |
Output is correct |
22 |
Correct |
60 ms |
115796 KB |
Output is correct |
23 |
Correct |
59 ms |
115792 KB |
Output is correct |
24 |
Correct |
17 ms |
115804 KB |
Output is correct |
25 |
Correct |
60 ms |
115996 KB |
Output is correct |
26 |
Correct |
53 ms |
115804 KB |
Output is correct |
27 |
Correct |
33 ms |
115792 KB |
Output is correct |
28 |
Correct |
35 ms |
116016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
102736 KB |
Output is correct |
2 |
Correct |
12 ms |
102748 KB |
Output is correct |
3 |
Correct |
12 ms |
102596 KB |
Output is correct |
4 |
Correct |
12 ms |
102748 KB |
Output is correct |
5 |
Correct |
12 ms |
102748 KB |
Output is correct |
6 |
Correct |
14 ms |
102744 KB |
Output is correct |
7 |
Correct |
12 ms |
102748 KB |
Output is correct |
8 |
Correct |
12 ms |
102704 KB |
Output is correct |
9 |
Correct |
12 ms |
102748 KB |
Output is correct |
10 |
Correct |
13 ms |
102744 KB |
Output is correct |
11 |
Correct |
13 ms |
102748 KB |
Output is correct |
12 |
Correct |
12 ms |
102624 KB |
Output is correct |
13 |
Correct |
12 ms |
102724 KB |
Output is correct |
14 |
Correct |
13 ms |
102676 KB |
Output is correct |
15 |
Correct |
13 ms |
102748 KB |
Output is correct |
16 |
Correct |
13 ms |
102748 KB |
Output is correct |
17 |
Correct |
15 ms |
107612 KB |
Output is correct |
18 |
Correct |
25 ms |
115804 KB |
Output is correct |
19 |
Correct |
41 ms |
115800 KB |
Output is correct |
20 |
Correct |
16 ms |
115800 KB |
Output is correct |
21 |
Correct |
16 ms |
115804 KB |
Output is correct |
22 |
Correct |
60 ms |
115796 KB |
Output is correct |
23 |
Correct |
59 ms |
115792 KB |
Output is correct |
24 |
Correct |
17 ms |
115804 KB |
Output is correct |
25 |
Correct |
60 ms |
115996 KB |
Output is correct |
26 |
Correct |
53 ms |
115804 KB |
Output is correct |
27 |
Correct |
33 ms |
115792 KB |
Output is correct |
28 |
Correct |
35 ms |
116016 KB |
Output is correct |
29 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
102736 KB |
Output is correct |
2 |
Correct |
12 ms |
102748 KB |
Output is correct |
3 |
Correct |
12 ms |
102596 KB |
Output is correct |
4 |
Correct |
12 ms |
102748 KB |
Output is correct |
5 |
Correct |
12 ms |
102748 KB |
Output is correct |
6 |
Correct |
14 ms |
102744 KB |
Output is correct |
7 |
Correct |
12 ms |
102748 KB |
Output is correct |
8 |
Correct |
12 ms |
102704 KB |
Output is correct |
9 |
Correct |
12 ms |
102748 KB |
Output is correct |
10 |
Correct |
13 ms |
102744 KB |
Output is correct |
11 |
Correct |
13 ms |
102748 KB |
Output is correct |
12 |
Correct |
12 ms |
102624 KB |
Output is correct |
13 |
Correct |
12 ms |
102724 KB |
Output is correct |
14 |
Correct |
13 ms |
102676 KB |
Output is correct |
15 |
Correct |
13 ms |
102748 KB |
Output is correct |
16 |
Correct |
13 ms |
102748 KB |
Output is correct |
17 |
Correct |
15 ms |
107612 KB |
Output is correct |
18 |
Correct |
25 ms |
115804 KB |
Output is correct |
19 |
Correct |
41 ms |
115800 KB |
Output is correct |
20 |
Correct |
16 ms |
115800 KB |
Output is correct |
21 |
Correct |
16 ms |
115804 KB |
Output is correct |
22 |
Correct |
60 ms |
115796 KB |
Output is correct |
23 |
Correct |
59 ms |
115792 KB |
Output is correct |
24 |
Correct |
17 ms |
115804 KB |
Output is correct |
25 |
Correct |
60 ms |
115996 KB |
Output is correct |
26 |
Correct |
53 ms |
115804 KB |
Output is correct |
27 |
Correct |
33 ms |
115792 KB |
Output is correct |
28 |
Correct |
35 ms |
116016 KB |
Output is correct |
29 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
102736 KB |
Output is correct |
2 |
Correct |
12 ms |
102748 KB |
Output is correct |
3 |
Correct |
12 ms |
102596 KB |
Output is correct |
4 |
Correct |
12 ms |
102748 KB |
Output is correct |
5 |
Correct |
12 ms |
102748 KB |
Output is correct |
6 |
Correct |
14 ms |
102744 KB |
Output is correct |
7 |
Correct |
12 ms |
102748 KB |
Output is correct |
8 |
Correct |
12 ms |
102704 KB |
Output is correct |
9 |
Correct |
12 ms |
102748 KB |
Output is correct |
10 |
Correct |
13 ms |
102744 KB |
Output is correct |
11 |
Correct |
13 ms |
102748 KB |
Output is correct |
12 |
Correct |
12 ms |
102624 KB |
Output is correct |
13 |
Correct |
12 ms |
102724 KB |
Output is correct |
14 |
Correct |
13 ms |
102676 KB |
Output is correct |
15 |
Correct |
13 ms |
102748 KB |
Output is correct |
16 |
Correct |
13 ms |
102748 KB |
Output is correct |
17 |
Correct |
15 ms |
107612 KB |
Output is correct |
18 |
Correct |
25 ms |
115804 KB |
Output is correct |
19 |
Correct |
41 ms |
115800 KB |
Output is correct |
20 |
Correct |
16 ms |
115800 KB |
Output is correct |
21 |
Correct |
16 ms |
115804 KB |
Output is correct |
22 |
Correct |
60 ms |
115796 KB |
Output is correct |
23 |
Correct |
59 ms |
115792 KB |
Output is correct |
24 |
Correct |
17 ms |
115804 KB |
Output is correct |
25 |
Correct |
60 ms |
115996 KB |
Output is correct |
26 |
Correct |
53 ms |
115804 KB |
Output is correct |
27 |
Correct |
33 ms |
115792 KB |
Output is correct |
28 |
Correct |
35 ms |
116016 KB |
Output is correct |
29 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |