Submission #889542

#TimeUsernameProblemLanguageResultExecution timeMemory
889542thunoproChorus (JOI23_chorus)C++14
40 / 100
60 ms116016 KiB
#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 (stderr)

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) ;
      |  ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...