제출 #776306

#제출 시각아이디문제언어결과실행 시간메모리
776306hugo_pmChorus (JOI23_chorus)C++17
61 / 100
930 ms1048576 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]);
}

vector<vector<int>> dp;
// cur et opt inclus
// mais cur représente right exclus
void calc(int nbCoup, int curLeft, int curRight, int optLeft, int optRight) {
	if (curLeft > curRight) return;
	int curMid = (curLeft + curRight) / 2;
	pair<int, int> p {INF, -1};
	rep(coupe, optLeft, optRight+1) {
		chmin(p, {dp[nbCoup-1][coupe] + coutInclus(coupe, curMid-1), coupe});
	}
	dp[nbCoup][curMid] = p.first;
	int optMid = p.second;
	calc(nbCoup, curLeft, curMid-1, optLeft, optMid);
	calc(nbCoup, curMid+1, curRight, optMid, optRight);
}
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);
			}
		}
	}

	dp = vector<vector<int>>(K+1, vector<int>(N+1, INF));
	dp[0][0] = 0;
	rep(nbCoup, 1, K+1) {
		calc(nbCoup, 0, N, 0, N-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...