Submission #1149142

#TimeUsernameProblemLanguageResultExecution timeMemory
1149142monkey133Chorus (JOI23_chorus)C++20
100 / 100
1884 ms53704 KiB
#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i) 
#define RFOR(i, x, n) for (ll i =x; i>=n; --i) 
#pragma GCC optimize("O3","unroll-loops")
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> piii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct line
{
	ll first, second, ind;
	line(ll a, ll b, ll c)
	{
		first = a, second = b, ind = c;
	}
};
deque<line> ch;

ll fx(line eq , ll x)
{
	return eq.first*x + eq.second;
}
 

// This version returns where optimal point is reached also in a pair
pi query(ll x)
{
	while (ch.size()>1 && fx(ch[0], x)>fx(ch[1],x))
	{
		ch.pop_front();
	}
	return mp(fx(ch[0], x), ch[0].ind);
}
 
long double intersect(line a, line b)
{
	return (long double) (b.second-a.second)/(a.first-b.first);
}
 
void update(line eq)
{
	// This deal with edges case with same gradient inserted
	while (!ch.empty()&&ch.back().first==eq.first)
	{
		if (ch.back().second>eq.second){ch.pop_back();}
		else return;
	}
	while (ch.size()>1 && intersect(ch[ch.size()-1], eq) <= intersect(ch[ch.size()-2], eq))
	{
		ch.pop_back();
	}
	ch.push_back(eq); 
}

ll n, k;
string ss;
pi dp[1000005];
ll a[1000005], b[1000005], psa[1000005];

pi check(ll x)
{
	ch.clear();
	update(line(0, 0, 0));
	ll idx = 0;
	for (ll i=1; i<=n; ++i)
	{
		while (idx + 1 < i && b[idx + 1] < i)
		{
			idx++;
			update(line(-idx, dp[idx].f - psa[b[idx]]+ idx * b[idx], dp[idx].s));
		}
		dp[i] = query(i);
		dp[i].f += psa[i] + x;
		dp[i].s++;
		//show(dp[i].f);
		//show(dp[i].s);
	}
	return dp[n];
	
}


int main()
{
	cin >> n >> k >> ss;
	ll sum = 0, idx = 0;
	for (ll i=0; i<2*n; ++i) 
	{
		if (ss[i]=='A') a[++idx] = sum;
		else sum++;
	}
	sum = 0, idx = 0;
	for (ll i=0; i<2*n; ++i) 
	{
		if (ss[i]=='B') {idx++; b[idx] = max(idx, sum);}
		else sum++;
	}
	psa[0] = 0;
	for (ll i=1; i<=n; ++i) psa[i] = a[i] + psa[i-1];
	// alien trick
	ll lo = 0, hi = n * n;
	while (lo!=hi)
	{
		ll mid = (lo + hi) >> 1;
		if (check(mid).s <= k) hi = mid;
		else lo= mid + 1;
	}
	//show(lo);
	cout << check(lo).f - k*lo << endl;
	return 0;
}




#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...