#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace lichao_tree{
ll midp(ll x){return (x - !!(x % 2)) / 2;}
struct node{
pair<ll, ll> line;
int l = -1, r = -1;
node(ll a, ll b): line(a, b) {}
ll eval(ll x){return line.first * x + line.second;}
};
vector<node> tree;
const ll MAXV = 1000000;
void insert(ll a, ll b){
node c(a, b);
if (tree.empty()){
tree.push_back(c);
return;
}
int tidx = 0;
ll tl = -MAXV, tr = MAXV;
while (tl <= tr){
ll tm = midp(tl + tr);
bool left = c.eval(tl) < tree[tidx].eval(tl);
bool mid = c.eval(tm) < tree[tidx].eval(tm);
if (mid) swap(c.line, tree[tidx].line);
if (tl == tr) return;
if (left != mid){
tr = tm-1;
if (tree[tidx].l == -1){
tree[tidx].l = tree.size();
tree.push_back(c);
return;
}
tidx = tree[tidx].l;
}
else{
tl = tm+1;
if (tree[tidx].r == -1){
tree[tidx].r = tree.size();
tree.push_back(c);
return;
}
tidx = tree[tidx].r;
}
}
}
ll query(ll x){
ll rs = LLONG_MAX;
if (tree.empty()) return rs;
int tidx = 0;
ll tl = -MAXV, tr = MAXV;
while (tl <= tr && tidx != -1){
rs = min(rs, tree[tidx].eval(x));
ll tm = midp(tl + tr);
if (x == tm) return rs;
if (x < tm) tr = tm - 1, tidx = tree[tidx].l;
else tl = tm + 1, tidx = tree[tidx].r;
}
return rs;
}
void clear(){
tree.clear();
}
};
vector<ll> a, pfb;
ll cost(int l, int r){
if (a[l] > r) return 0;
return pfb[r+1] - pfb[a[l]] - (r-a[l]+1) * l;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n, k; string s; cin >> n >> k >> s;
int ptra = 0, ptrb = 0;
a.resize(n+1), pfb.resize(n+1);
for (int i=0; i<s.size(); i++){
if (s[i] == 'A'){
ptrb++;
pfb[ptrb] = pfb[ptrb-1] + ptra;
}
else{
ptra++;
a[ptra] = ptrb;
}
}
// for (int i=0; i<=n; i++){
// cout << a[i] << " \n"[i == n];
// }
// for (int i=0; i<=n; i++){
// cout << pfb[i] << " \n"[i == n];
// }
// for (int i=0; i<n; i++){
// for (int j=i; j<n; j++){
// cout << i << " " << j << ": " << cost(i, j) << "\n";
// }
// }
vector<vector<ll>> dp(n, vector<ll> (k));
for (int i=0; i<n; i++){
for (int j=0; j<k; j++){
dp[i][j] = LLONG_MAX;
for (int x=0; x<=i; x++){
dp[i][j] = min(dp[i][j], (x ? (j ? dp[x-1][j-1] : ll(1e18)) : 0) + cost(x, i));
}
}
}
cout << dp[n-1][k-1];
}
Compilation message
chorus.cpp: In function 'int main()':
chorus.cpp:88:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int i=0; i<s.size(); i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |