正文
增加 4 行删除 4 行
17 行未改动18192021222324252620 行未改动47484950515253545519 行未改动75767778798081828384858687888990916 行未改动17 行未改动然后考虑优化。先给出结论,dp 状态具有决策单调性,即记 $p(i,j)$ 表示 $f_{i,j}$ 的最优决策点,那么保证 $p(i,j)\le p(i+1,j)$。于是我们按 $j$ 分层转移,每次转移用分治维护就好了。时间复杂度 $\mathcal O(n^2\log n)$。> 决策单调性的不严谨证明:>> 首先当 $p(i,j)$ 不存在,也就是 $i<j$ 的时候结论显然成立,不再讨论。>> 为了便于书写记 $p=p(i,j),q=p(i+1,j)$,考虑反证法,设 $p > q$,那么我们有:>20 行未改动#include <bits/stdc++.h>#define LL long longusing namespace std;const int N = 2e3 + 10;int n, m; char S[N][N];bool vis[N][N]; int cur, mn, mx;int dx[4] = {0, 1, 0, -1};int dy[4] = {1, 0, -1, 0};void DFS(int x, int y) {19 行未改动freopen(".in", "r", stdin); freopen(".out", "w", stdout);ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);cin >> n >> m; for (int i = 1; i <= n; i ++) cin >> (S[i] + 1);for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++)if (S[i][j] != '.' && !vis[i][j]) {mn = m + 1, cur = mx = 0; DFS(i, j); seg[++ tot] = {mn, mx, cur};vec[mn].push_back(tot);}for (int i = 1; i <= m; i ++)for (int x : vec[i]) for (int j = seg[x].l; j <= seg[x].r; j ++)W[i][j] += seg[x].k, DP[j][1] += seg[x].k;for (int i = m; i >= 1; i --)for (int j = i + 1; j <= m; j ++)cost[i][j] = cost[i + 1][j] + W[i + 1][j];for (int i = 2; i <= m; i ++) Solve(i, m, 1, m, i);for (int i = 1; i <= m; i ++) {int res = 0;6 行未改动17 行未改动18192021222324252620 行未改动47484950515253545519 行未改动75767778798081828384858687888990916 行未改动17 行未改动然后考虑优化。先给出结论,dp 状态具有决策单调性,即记 $p(i,j)$ 表示 $f_{i,j}$ 的最优决策点,那么保证 $p(i,j)\le p(i+1,j)$。于是我们按 $j$ 分层转移,每次转移用分治维护就好了。时间复杂度 $\mathcal O(n^2\log n)$。> 决策单调性的不严谨证明:>> 首先当 $p(i,j)$ 不存在,也就是 $i<j$ 的时候结论显然成立,不再讨论。>> 为了便于书写记 $p=p(i,j),q=p(i+1,j)$,考虑反证法,设 $p > q$,那么我们有:>20 行未改动#include <bits/stdc++.h>#define LL long longusing namespace std;const int N = 2e3 + 10;int n, m; char S[N][N];bool vis[N][N]; int cur, mn, mx;int dx[4] = {0, 1, 0, -1};int dy[4] = {1, 0, -1, 0};void DFS(int x, int y) {19 行未改动freopen(".in", "r", stdin); freopen(".out", "w", stdout);ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);cin >> n >> m; for (int i = 1; i <= n; i ++) cin >> (S[i] + 1);for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++)if (S[i][j] != '.' && !vis[i][j]) {mn = m + 1, cur = mx = 0; DFS(i, j); seg[++ tot] = {mn, mx, cur};vec[mn].push_back(tot);}for (int i = 1; i <= m; i ++)for (int x : vec[i]) for (int j = seg[x].l; j <= seg[x].r; j ++)W[i][j] += seg[x].k, DP[j][1] += seg[x].k;for (int i = m; i >= 1; i --)for (int j = i + 1; j <= m; j ++)cost[i][j] = cost[i + 1][j] + W[i + 1][j];for (int i = 2; i <= m; i ++) Solve(i, m, 1, m, i);for (int i = 1; i <= m; i ++) {int res = 0;6 行未改动