0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 21:48:48 +00:00

P2943 [USACO09MAR]Cleaning Up G

https://www.luogu.com.cn/record/77050304
This commit is contained in:
Baoshuo Ren 2022-06-07 11:26:19 +08:00
parent 4b4a4f1b2c
commit 8241ee6d51
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

53
Luogu/P2943/P2943.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <iostream>
#include <cmath>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 40005;
int n, m, t, p[N], pre[N], lst[N], nxt[N], pos[N], cnt[N], f[N];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
std::fill(f, f + 1 + n, 0x3f3f3f3f);
for (int i = 1; i <= n; i++) {
cin >> p[i];
pre[i] = lst[p[i]];
nxt[lst[p[i]]] = i;
lst[p[i]] = i;
nxt[i] = n + 1;
}
t = std::sqrt(n);
f[1] = 1;
for (int i = 1; i <= t; i++) {
pos[i] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= t; j++) {
if (pre[i] < pos[j]) cnt[j]++;
if (cnt[j] > j) {
cnt[j]--;
while (nxt[pos[j]++] < i) {}
}
f[i] = std::min(f[i], f[pos[j] - 1] + (int)std::pow(j, 2));
}
}
cout << f[n] << endl;
return 0;
}