题解:P13925 [POKATT 2024] 联合猫国 / The Paw-litical Game
_TwilightSnow_ · · 题解
DP 好题。
可以和 P3147 一起做来加深印象。
73 分解法
想看
发现
:::success[Code]
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, s, t) for (int i = s; i <= t; i++)
#define pre(i, s, t) for (int i = s; i >= t; i--)
#define all(x) begin(x), end(x)
const int N = 1e6 + 2;
int n, f[65][N], mx[N], g[N], res;
vector<int> st[N];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n, res = 2e9;
rep(i, 1, n) { int x; cin >> x; f[x][i] = i; }
rep(i, 1, 60) rep(j, 1, n) {
if (f[i - 1][j]) f[i][j] = f[i - 1][f[i - 1][j] + 1];
if (f[i][j]) st[f[i][j]].push_back(j);
}
rep(i, 1, n) sort(all(st[i])), st[i].erase(unique(all(st[i])), end(st[i]));
rep(i, 1, n) {
for (int j : st[i]) g[i] = max(g[i], mx[j - 1] + i - j);
mx[i] = max(mx[i - 1], g[i]);
}
return cout << n - mx[n], 0;
}
:::
100 分解法
发现我们想出
:::success[Code]
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, s, t) for (int i = s; i <= t; i++)
#define pre(i, s, t) for (int i = s; i >= t; i--)
#define all(x) begin(x), end(x)
const int N = 1e6 + 2;
int n, f[N], a[N], res;
vector<int> st[N];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
rep(i, 1, n) {
int p = i - 1, v; cin >> a[i], v = a[i];
f[i] = f[i - 1] + 1, st[i].push_back(i - 1);
while (p && v >= a[p] && v - a[p] < (int)st[p].size())
// v>=a[p]保证了a[p]可以合并出v,v-a[p]<st[p].size()保证了有方案可以合并
p = st[p][v - a[p]], v++, f[i] = min(f[i], f[p] + 1), st[i].push_back(p);
// 合并完之后v+1,p向前跳到左端点的位置的前一个位置
}
return cout << f[n], 0;
}
:::