#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 35100;
int n, k, a[N], last[N], pre[N], f[N];
struct node {
int t, val;
} seg[N * 4];
void update (int id) {
seg[id].val = min(seg[id * 2].val, seg[id * 2 + 1].val);
}
void settag (int id, ll t) {
seg[id].val = seg[id].val + t;
seg[id].t = seg[id].t + t;
}
void pushdown (int id) {
if (seg[id].t != 0) {
settag (id * 2, seg[id].t);
settag (id * 2 + 1, seg[id].t);
seg[id].t = 0;
}
}
void build (int id, int l, int r) {
seg[id].t = 0;
if (l == r) {
seg[id].val = f[l];
} else {
int mid = (l + r) >> 1;
build (id * 2, l, mid);
build (id * 2 + 1, mid + 1, r);
update (id);
}
}
void change (int id, int l, int r, int ql, int qr, ll t) {
if (l == ql && r == qr) {
settag(id, t);
return;
}
int mid = (l + r) / 2;
pushdown (id);
if (qr <= mid) change (id * 2, l, mid, ql, qr, t);
else if (ql > mid) change (id * 2 + 1, mid + 1, r, ql, qr, t);
else {
change (id * 2, l, mid, ql, mid, t);
change (id * 2 + 1, mid + 1, r, mid + 1, qr, t);
}
update (id);
}
ll query (int id, int l, int r, int ql, int qr) {
if (l == ql && r == qr) return seg[id].val;
int mid = (l + r) / 2;
pushdown (id);
if (qr <= mid) return query (id * 2, l, mid, ql, qr);
else if (ql > mid) return query (id * 2 + 1, mid + 1, r, ql, qr);
else {
return min(query (id * 2, l, mid, ql, mid), query (id * 2 + 1, mid + 1, r, mid + 1, qr));
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
last[i] = pre[a[i]];
pre[a[i]] = i;
}
f[0] = 0;
for (int i = 1; i <= n; i++) f[i] = 1 << 30;
for (int j = 1; j <= k; j++) {
build(1, 0, n);
for (int i = 1; i <= n; i++) {
if (last[i] != 0) change(1, 0, n, 0, last[i] - 1, i - last[i]);
f[i] = query(1, 0, n, 0, i - 1);
}
}
printf("%d\n", f[n]);
}
1302. Deepest Leaves Sum | 1209. Remove All Adjacent Duplicates in String II |
994. Rotting Oranges | 983. Minimum Cost For Tickets |
973. K Closest Points to Origin | 969. Pancake Sorting |
967. Numbers With Same Consecutive Differences | 957. Prison Cells After N Days |
946. Validate Stack Sequences | 921. Minimum Add to Make Parentheses Valid |
881. Boats to Save People | 497. Random Point in Non-overlapping Rectangles |
528. Random Pick with Weight | 470. Implement Rand10() Using Rand7() |
866. Prime Palindrome | 1516A - Tit for Tat |
622. Design Circular Queue | 814. Binary Tree Pruning |
791. Custom Sort String | 787. Cheapest Flights Within K Stops |
779. K-th Symbol in Grammar | 701. Insert into a Binary Search Tree |
429. N-ary Tree Level Order Traversal | 739. Daily Temperatures |
647. Palindromic Substrings | 583. Delete Operation for Two Strings |
518. Coin Change 2 | 516. Longest Palindromic Subsequence |
468. Validate IP Address | 450. Delete Node in a BST |