#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
const int INF = 3e12;
struct Line {
int m, p, cnt;
pii eval(int x) const {
return {m*x+p, cnt};
}
};
struct LineContainer {
vector<Line> fs;
int front = 0;
bool bad(Line a, Line b, Line c) {
// (cp-ap)/(am-cm) < (bp-ap)/(am-bm)
int v1 = (c.p - a.p) * (a.m - b.m);
int v2 = (b.p - a.p) * (a.m - c.m);
return v1 >= v2;
}
void add (Line l) {
int szFs = sz(fs);
while (szFs >= front+2 && bad(fs[szFs-2], fs[szFs-1], l)) {
fs.pop_back(), --szFs;
}
fs.push_back(l);
}
pii min(int x) {
if (fs.empty()) return {INF, INF};
chmin(front, sz(fs)-1);
while (front+1 < sz(fs) && fs[front].eval(x) >= fs[front+1].eval(x)) {
++front;
}
return fs[front].eval(x);
}
};
int N, K;
string S;
// ferme[i] = nombre d'ouvrants avant Fi
vector<int> ouvre, ferme;
vector<int> cumuFerme;
int coutExclus(int left, int right) {
if (left >= right) {
return 0;
}
int projExclus = min(right, ouvre[right-1]);
if (projExclus <= left) return 0;
return (projExclus - left)*(right) - (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);
}
}
}
auto calc = [&] (int lambda) -> pii {
vector<pii> dp(N+1, {INF, INF});
dp[N] = {0, 0};
LineContainer cont;
// [J EXCLUS]
// gright(left) = (projExclus - left)*(right) - (cumuFerme[projExclus] - cumuFerme[left])
// gj(x) = (pj - x)*j - (cf[pj] - cf[left])
// gj(x) = -jx + (j*pj - cf[pj]) + cf[x]
// mx + p + terme fixe
auto proj = [&] (int j) {
return min(j, ouvre[j-1]);
};
auto add = [&] (int j) {
int pj = proj(j);
// add -10 -9 -8.. pente croissante
cont.add({-j, dp[j].first + j*pj - cumuFerme[pj], dp[j].second});
};
int jToAdd = N;
for (int left = N-1; left >= 0; --left) {
while (jToAdd > 0 && left <= min(proj(jToAdd), jToAdd-1)) {
add(jToAdd--);
}
if (true) {
// min(10) min(9) .. x décroissant
pii prop = cont.min(left);
chmin(dp[left], make_pair(
prop.first + cumuFerme[left] + lambda,
prop.second + 1
));
}
if (jToAdd > 0) {
chmin(dp[left], make_pair(
dp[jToAdd].first + coutExclus(left, jToAdd) + lambda,
dp[jToAdd].second + 1
));
}
}
return dp[0];
};
int lo = 0, hi = INF;
while (lo <= hi) {
int mid = (lo+hi) / 2;
if(calc(mid).second > K) {
lo = mid+1;
} else {
hi = mid-1;
}
}
//assert(lo == hi);
int lambda = lo;
auto [score, groupsMade] = calc(lambda);
assert(groupsMade <= K);
cout << score - lambda*K << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
272 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
272 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
6 ms |
880 KB |
Output is correct |
30 |
Correct |
6 ms |
736 KB |
Output is correct |
31 |
Correct |
6 ms |
756 KB |
Output is correct |
32 |
Correct |
4 ms |
748 KB |
Output is correct |
33 |
Correct |
4 ms |
748 KB |
Output is correct |
34 |
Correct |
4 ms |
880 KB |
Output is correct |
35 |
Correct |
4 ms |
880 KB |
Output is correct |
36 |
Correct |
6 ms |
596 KB |
Output is correct |
37 |
Correct |
6 ms |
736 KB |
Output is correct |
38 |
Correct |
5 ms |
880 KB |
Output is correct |
39 |
Correct |
5 ms |
732 KB |
Output is correct |
40 |
Correct |
4 ms |
752 KB |
Output is correct |
41 |
Correct |
4 ms |
724 KB |
Output is correct |
42 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
272 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
6 ms |
880 KB |
Output is correct |
30 |
Correct |
6 ms |
736 KB |
Output is correct |
31 |
Correct |
6 ms |
756 KB |
Output is correct |
32 |
Correct |
4 ms |
748 KB |
Output is correct |
33 |
Correct |
4 ms |
748 KB |
Output is correct |
34 |
Correct |
4 ms |
880 KB |
Output is correct |
35 |
Correct |
4 ms |
880 KB |
Output is correct |
36 |
Correct |
6 ms |
596 KB |
Output is correct |
37 |
Correct |
6 ms |
736 KB |
Output is correct |
38 |
Correct |
5 ms |
880 KB |
Output is correct |
39 |
Correct |
5 ms |
732 KB |
Output is correct |
40 |
Correct |
4 ms |
752 KB |
Output is correct |
41 |
Correct |
4 ms |
724 KB |
Output is correct |
42 |
Correct |
3 ms |
596 KB |
Output is correct |
43 |
Correct |
98 ms |
5456 KB |
Output is correct |
44 |
Correct |
144 ms |
9580 KB |
Output is correct |
45 |
Correct |
137 ms |
9540 KB |
Output is correct |
46 |
Correct |
80 ms |
7452 KB |
Output is correct |
47 |
Correct |
80 ms |
7496 KB |
Output is correct |
48 |
Correct |
100 ms |
10212 KB |
Output is correct |
49 |
Correct |
99 ms |
10200 KB |
Output is correct |
50 |
Correct |
143 ms |
7132 KB |
Output is correct |
51 |
Correct |
144 ms |
7180 KB |
Output is correct |
52 |
Correct |
103 ms |
10148 KB |
Output is correct |
53 |
Correct |
113 ms |
10244 KB |
Output is correct |
54 |
Correct |
142 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
272 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
6 ms |
880 KB |
Output is correct |
30 |
Correct |
6 ms |
736 KB |
Output is correct |
31 |
Correct |
6 ms |
756 KB |
Output is correct |
32 |
Correct |
4 ms |
748 KB |
Output is correct |
33 |
Correct |
4 ms |
748 KB |
Output is correct |
34 |
Correct |
4 ms |
880 KB |
Output is correct |
35 |
Correct |
4 ms |
880 KB |
Output is correct |
36 |
Correct |
6 ms |
596 KB |
Output is correct |
37 |
Correct |
6 ms |
736 KB |
Output is correct |
38 |
Correct |
5 ms |
880 KB |
Output is correct |
39 |
Correct |
5 ms |
732 KB |
Output is correct |
40 |
Correct |
4 ms |
752 KB |
Output is correct |
41 |
Correct |
4 ms |
724 KB |
Output is correct |
42 |
Correct |
3 ms |
596 KB |
Output is correct |
43 |
Correct |
98 ms |
5456 KB |
Output is correct |
44 |
Correct |
144 ms |
9580 KB |
Output is correct |
45 |
Correct |
137 ms |
9540 KB |
Output is correct |
46 |
Correct |
80 ms |
7452 KB |
Output is correct |
47 |
Correct |
80 ms |
7496 KB |
Output is correct |
48 |
Correct |
100 ms |
10212 KB |
Output is correct |
49 |
Correct |
99 ms |
10200 KB |
Output is correct |
50 |
Correct |
143 ms |
7132 KB |
Output is correct |
51 |
Correct |
144 ms |
7180 KB |
Output is correct |
52 |
Correct |
103 ms |
10148 KB |
Output is correct |
53 |
Correct |
113 ms |
10244 KB |
Output is correct |
54 |
Correct |
142 ms |
7380 KB |
Output is correct |
55 |
Correct |
1012 ms |
51768 KB |
Output is correct |
56 |
Correct |
1412 ms |
89024 KB |
Output is correct |
57 |
Correct |
1469 ms |
85644 KB |
Output is correct |
58 |
Correct |
658 ms |
67448 KB |
Output is correct |
59 |
Correct |
1156 ms |
81328 KB |
Output is correct |
60 |
Correct |
1215 ms |
91496 KB |
Output is correct |
61 |
Correct |
1208 ms |
91540 KB |
Output is correct |
62 |
Correct |
1396 ms |
64904 KB |
Output is correct |
63 |
Correct |
1409 ms |
65416 KB |
Output is correct |
64 |
Correct |
1288 ms |
91476 KB |
Output is correct |
65 |
Correct |
1296 ms |
91528 KB |
Output is correct |
66 |
Correct |
1413 ms |
67156 KB |
Output is correct |
67 |
Correct |
1226 ms |
91604 KB |
Output is correct |