Submission #776301

#TimeUsernameProblemLanguageResultExecution timeMemory
776301hugo_pmChorus (JOI23_chorus)C++17
40 / 100
7067 ms79496 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))

template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }

using pii = pair<int, int>;
using vi = vector<int>;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

int N, K;
string S;
const int INF = 1e12;
// ferme[i] = nombre d'ouvrants avant Fi
vector<int> ouvre, ferme;
vector<int> cumuFerme;
int coutInclus(int left, int right) {
	if (left > right) {
		return 0;
	}
	int projExclus = min(right+1, ouvre[right]);
	if (projExclus <= left) return 0;
	return (projExclus - left)*(right+1) - (cumuFerme[projExclus] - cumuFerme[left]);
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N >> K >> S;
	{
		int curOuvre = 0, curFerme = 0;
		cumuFerme.push_back(0);
		for (char c : S) {
			if (c == 'A') {
				++curOuvre;
				ouvre.push_back(curFerme);
			} else {
				++curFerme;
				ferme.push_back(curOuvre);
				cumuFerme.push_back(cumuFerme.back() + curOuvre);
			}
		}
	}

	vector<vector<int>> dp(K+1, vector<int>(N+1, INF));
	dp[0][0] = 0;
	rep(nbCoup, 1, K+1) {
		rep(prevRi, 0, N+1) {
			rep(nextRi, prevRi, N+1) {
				chmin(dp[nbCoup][nextRi], dp[nbCoup-1][prevRi] + coutInclus(prevRi, nextRi-1));
			}
		}
	}
	cout << dp[K][N] << '\n';
}
#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...